Skip to content

Commit a241e79

Browse files
Fix Ansible Install capability for RHEL like system with version >= 10 (#13701)
On RHEL-like systems with major version >= 10, the current Ansible installation logic incorrectly parses the version from rpm -E %dist. This is because the current inline sed command that only extracts the first character of the version string—so for version 10, it extracts only 1, resulting in an incorrect download URL.
1 parent 161d559 commit a241e79

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

plugins/provisioners/ansible/cap/guest/redhat/ansible_install.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def self.ansible_rpm_install(machine)
3131

3232
epel = machine.communicate.execute "#{rpm_package_manager} repolist epel | grep -q epel", error_check: false
3333
if epel != 0
34-
machine.communicate.sudo 'sudo rpm -i https://dl.fedoraproject.org/pub/epel/epel-release-latest-`rpm -E %dist | sed -n \'s/.*el\([0-9]\).*/\1/p\'`.noarch.rpm'
34+
machine.communicate.sudo "sudo rpm -i #{ansible_epel_download_url(machine)}"
3535
end
3636
machine.communicate.sudo "#{rpm_package_manager} -y --enablerepo=epel install ansible"
3737
end
@@ -67,6 +67,15 @@ def self.pip_setup(machine, pip_install_cmd = "")
6767
})
6868
end
6969

70+
def self.ansible_epel_download_url(machine)
71+
dist = ""
72+
machine.communicate.execute("rpm -E %dist") do |type, data|
73+
dist << data if type == :stdout
74+
end
75+
dist.strip!
76+
dist_major_version = dist.match(/.*el(\d+).*/)&.captures&.first
77+
"https://dl.fedoraproject.org/pub/epel/epel-release-latest-#{dist_major_version}.noarch.rpm"
78+
end
7079
end
7180
end
7281
end
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright (c) HashiCorp, Inc.
2+
# SPDX-License-Identifier: BUSL-1.1
3+
4+
require_relative "../../../../../../base"
5+
6+
require Vagrant.source_root.join("plugins/provisioners/ansible/cap/guest/redhat/ansible_install")
7+
8+
describe VagrantPlugins::Ansible::Cap::Guest::RedHat::AnsibleInstall do
9+
include_context "unit"
10+
11+
subject { VagrantPlugins::Ansible::Cap::Guest::RedHat::AnsibleInstall }
12+
13+
let(:iso_env) do
14+
env = isolated_environment
15+
env.vagrantfile("")
16+
env.create_vagrant_env
17+
end
18+
let(:machine) { iso_env.machine(iso_env.machine_names[0], :dummy) }
19+
let(:communicator) { double("comm") }
20+
let(:dist) { ".el8" }
21+
let(:epel) { 1 }
22+
23+
before do
24+
allow(machine).to receive(:communicate).and_return(communicator)
25+
allow(communicator).to receive(:execute).with("rpm -E %dist").and_yield(:stdout, dist)
26+
end
27+
28+
describe "#ansible_epel_download_url" do
29+
it "returns the correct EPEL download URL for RHEL-like versions below 10" do
30+
expect(subject.ansible_epel_download_url(machine)).to eq("https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm")
31+
end
32+
33+
context "for RHEL-like versions 10 and above" do
34+
let(:dist) { ".el10" }
35+
it "returns the correct EPEL download URL" do
36+
out = subject.ansible_epel_download_url(machine)
37+
expect(out).to eq("https://dl.fedoraproject.org/pub/epel/epel-release-latest-10.noarch.rpm")
38+
end
39+
end
40+
end
41+
42+
describe "#ansible_rpm_install" do
43+
before do
44+
expect(communicator).to receive(:test).with("/usr/bin/which -s dnf").and_return(false)
45+
expect(communicator).to receive(:execute).with("yum repolist epel | grep -q epel", error_check: false).and_return(epel)
46+
expect(communicator).to receive(:sudo).with("yum -y --enablerepo=epel install ansible")
47+
end
48+
49+
it "installs ansible package, when epel is not installed" do
50+
expect(communicator).to receive(:sudo).with("sudo rpm -i https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm")
51+
subject.ansible_rpm_install(machine)
52+
end
53+
54+
context "when the EPEL repository is already installed" do
55+
let(:epel) { 0 }
56+
it "installs ansible package" do
57+
expect(communicator).to_not receive(:sudo).with("sudo rpm -i https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm")
58+
subject.ansible_rpm_install(machine)
59+
end
60+
end
61+
62+
end
63+
64+
end

0 commit comments

Comments
 (0)