如何在不更改.rb文件的情况下通过Vagrantfile扩展流浪插件



我在Vagrant中遇到了这样的场景,我想使用ansible_local插件修补ansible如何安装在RHEL 8上(因为存储库设置导致缺少包的某些问题(。事情是这样的。相反,我想使用pip3(尽管我知道我可以使用ansible_local模块使用pip,但由于缺乏特定的存储库,它仍然会出错,所以我想办法修复它(。

在我的流浪者档案中,我有这样的行,

.....
node.vm.provision "ansible_local" do |ansible|
ansible.playbook = ansible_playbook
ansible.verbose = true
ansible.install = true
## Actually this line doesn't suffice my problem since it still errs as pip requires other packages. Please check the *.rb file below
if i == 100
ansible.install_mode = "pip"
ansible.version = "2.9"
#ansible.ansible_rpm_install = Foo
end
end
...
.....

但因为使用pip仍然失败,所以我最终更改了文件/opt/vagrant/embedded/gems/2.2.9/gems/vagrant-2.2.9/plugins/provisioners/ansible/cap/guest/redhat/ansible_install.rb,我所做的就是这样,


cap/guest/redhat/ansible_install.rb
require_relative "../facts"
require_relative "../pip/pip"
module VagrantPlugins
module Ansible
module Cap
module Guest
module RedHat
module AnsibleInstall
def self.ansible_install(machine, install_mode, ansible_version, pip_args, pip_install_cmd = "")
case install_mode
when :pip
pip_setup machine, pip_install_cmd
Pip::pip_install machine, "ansible", ansible_version, pip_args, true
when :pip_args_only
pip_setup machine, pip_install_cmd
Pip::pip_install machine, "", "", pip_args, false
else

// My added part as a quick fix/solution
if machine.config.vm.box == "generic/rhel8"
ansible_rpm_install_rhel8 machine
elsif
ansible_rpm_install machine
end

end
end
private
def self.ansible_rpm_install(machine)
rpm_package_manager = Facts::rpm_package_manager(machine)
epel = machine.communicate.execute "#{rpm_package_manager} repolist epel | grep -q epel", error_check: false
if epel != 0
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'
end
machine.communicate.sudo "#{rpm_package_manager} -y --enablerepo=epel install ansible"
end
def self.pip_setup(machine, pip_install_cmd = "")
rpm_package_manager = Facts::rpm_package_manager(machine)
machine.communicate.sudo("#{rpm_package_manager} -y install curl gcc libffi-devel openssl-devel python-crypto python-devel python-setuptools")
Pip::get_pip machine, pip_install_cmd
end
def self.pip_setup(machine, pip_install_cmd = "")
rpm_package_manager = Facts::rpm_package_manager(machine)
machine.communicate.sudo("#{rpm_package_manager} -y install curl gcc libffi-devel openssl-devel python-crypto python-devel python-setuptools")
Pip::get_pip machine, pip_install_cmd
end

// My added part as a quick fix/solution
def self.ansible_rpm_install_rhel8(machine)
rpm_package_manager = Facts::rpm_package_manager(machine)
epel = machine.communicate.execute "#{rpm_package_manager} repolist epel | grep -q epel", error_check: false
if epel != 0
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'
end
machine.communicate.sudo "dnf -y update; dnf -y install python3 python3-pip; pip3 install ansible"
end


end
end
end
end
end
end

So I added a methodself.ansible_rpm_install_rhel8(machine)and did anif..elsifas a solution when box name is"generic/rhel8". This works perfect actually on my end. However, I don't like this approach i.e. changing the file/opt/vagrant/embedded/gems/2.2.9/gems/vagrant-2.2.9/plugins/provisioners/ansible/cap/guest/redhat/ansible_install.rbbecause once it changes or version upgrades, this will be gone. Is there any better way that I can just do this only inside Vagrantfile such that perhaps overriding or extending the class itself? I have no idea how to do this.

Any ideas are welcome.

Thank you!

You can use multiple provisioning sections next one another which will be executed in the order as you defined them in yourVagrantfile.

Instead of patching a plugin, try to prepare your ansible execution accordingly.

Before executing the provisioning part for ansible, execute ashell provisioningas follows:

Vagrant.configure("2") do |node|
# ...
node.vm.provision :shell, path: "fix_repo_and_add_packages.sh"
node.vm.provision "ansible_local" do |ansible|
ansible.playbook = ansible_playbook
ansible.verbose = true
ansible.install = true
## Actually this line doesn't suffice my problem since it still errs as pip requires other packages. Please check the *.rb file below
if i == 100
ansible.install_mode = "pip"
ansible.version = "2.9"
#ansible.ansible_rpm_install = Foo
end
end
end

您的fix_repo_and_add_packages.sh包含丢失的repo的设置,您也可以在此处添加包。

有关shell配置的更多信息,您可以在文档中找到

最新更新