根据我的说法,当我阅读原始问题时,不是如何找出它在哪个操作系统上自行运行,而是要配置的虚拟机具有哪个操作系统。这就是为什么您要根据新 VM 的不同操作系统运行不同的配置脚本的原因,例如:“/vagrant/provisioning/only_run_this_on_${OS_OF_NEW_VM}.sh”。
不幸的是,Vagrant 还没有这个功能,所以这是我的解决方案:我在我的 vagrant 文件之上定义我的虚拟机:
cluster = {
"control.ansible.RHEL76" => { :ip => "192.168.1.31", :type => 0, :cpus => 1, :mem => 1024, :box_image => "centos/7" },
"app01.ansible.RHEL76" => { :ip => "192.168.1.32", :type => 1, :cpus => 1, :mem => 1024, :box_image => "centos/7" },
"app02.ansible.RHEL76" => { :ip => "192.168.1.33", :type => 1, :cpus => 1, :mem => 1024, :box_image => "centos/7" },
"winserver" => { :ip => "192.168.1.34", :type => 2, :cpus => 1, :mem => 1024, :box_image => "mwrock/Windows2016" },
}
然后我的代码中的这些条件可以根据 VM 的操作系统提供不同的方式:
if "#{info[:box_image]}" == "mwrock/Windows2016" then
puts "is_windows_host: #{info[:box_image]}"
config.vm.provision : shell, inline => "/vagrant/provisioning/only_run_this_on_windows.psl"
end
if "#{info[:box_image]}" == "centos/7" then
puts "is_linux_host: #{info[:box_image]}"
config.vm.provision : shell, inline => "/vagrant/provisioning/only_run_this_on_linux.sh"
end
顺便说一句,只有 ansible 控制器应该安装 ansible,因为在现实生活中(是的办公室)我不使用 vagrant 而是一个 ansible 控制器,我也想要在我的实验室中(在我的两个 Windows 上都可以使用 Virtual Box 10 台式机以及我的 Ubuntu 笔记本电脑)。我使用条件来测试“type = 0”(我将其用于 ansible“控制器”)。只有 ansible 控制器启动 ansible_local 以使用 ansible 配置 VM 集群。
if info[:type] == 0 then
cfg.vm.provision "shell", inline: "if [ `which ansible` ] ; then echo \"ansible available\"; else sudo yum -y update; sudo yum -y install epel-release; sudo yum -y install ansible; fi"
cfg.vm.provision "ansible_local" do |ansible|
ansible.extra_vars = { ansible_ssh_user: 'vagrant' }
ansible.inventory_path = "./production"
ansible.playbook = "rhelhosts.yml"
ansible.limit = "local"
end # ansible_local
这是在流浪者提供期间显示的:
PS D:\Documents\vagrant\top> vagrant 提供 control.top.RHEL76
is_linux_host: centos/7
is_linux_host: centos/7
is_linux_host: centos/7
is_windows_host: mwrock/Windows2016
这是在流浪者提供期间显示的:
PS D:\Documents\vagrant\ansible> vagrant provision control.ansible.RHEL76
is_linux_host: centos/7
is_linux_host: centos/7
is_linux_host: centos/7
is_windows_host: mwrock/Windows2016
--- many more lines, not relevant ---
尽情体验和部署您的多机/多操作系统实验室!