我有一个如下所示的 Vagrantfile,我希望在创建所有虚拟机后仅运行一次 ansible provision,但实际上它在每次创建虚拟机后都会运行,我不知道为什么?我怎样才能让 ansible provision 只运行一次?提前致谢。
IMAGE_NAME = "bento/debian-10.5"
N = 2
Vagrant.configure("2") do |config|
config.ssh.insert_key = false
(1..N).each do |i|
config.vm.define "node-#{i}" do |node|
node.vm.box = IMAGE_NAME
node.vm.network "private_network", ip: "10.0.1.#{10 + i}"
node.vm.hostname = "node-#{i}"
end
if i == N
# only run provision after all vm created
config.vm.provision "ansible" do |ansible|
ansible.playbook = "ansible/site.yml"
end
end
end
end
我尝试将 if 条件中的 config.vm.provision 代码移到 config.vm.define 块中,但每次创建虚拟机后它仍然会运行 ansible provision
(1..N).each do |i|
config.vm.define "node-#{i}" do |node|
node.vm.box = IMAGE_NAME
node.vm.network "private_network", ip: "10.0.1.#{10 + i}"
node.vm.hostname = "node-#{i}"
if i == N
# only run provision after all vm created
config.vm.provision "ansible" do |ansible|
ansible.playbook = "ansible/site.yml"
end
end
end
end