如何在Vagrantfile中只运行一次命令块。尝试使用如果的情况,但没有成功。
Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
$hosts = {
"node-1" => { memory: 1024, cpus: 2 },
"node-2" => { memory: 4096, cpus: 2 }
}
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
$hosts.each_with_index do |(hostname, parameters), index|
config.vm.define vm_name = hostname do |vm_config|
vm_config.vm.hostname = hostname
config.vm.provision "shell", inline: "echo index=#{index}"
if index == 0
config.vm.provision "shell", path: "./bootstrap-scripts/node-1.sh"
end
end
end
end控制台结果:
==> node-1: Importing base box 'generic/ubuntu1804'...
...
==> node-1: Running provisioner: shell...
node-1: Running: inline script
node-1: index=0
==> node-2: Importing base box 'generic/ubuntu1804'...
...
==> node-2: Running provisioner: shell...
node-2: Running: inline script
node-2: index=0
==> node-2: Running provisioner: shell...
node-2: Running: inline script
node-2: index=1目标是只为第一台机器运行“./bootstrap- script /control.sh.sh”脚本。
发布于 2019-01-31 19:48:09
来自流浪文献
多机器定义和提供程序重写的内部部分是延迟加载的。如果您更改在信任中使用的变量的值,这可能会导致问题.
each_with_index的内部行为可能会触发记录在案的问题,这就是为什么节点-2循环遍历两个索引的原因。因此,尝试在if中使用节点名。
另外,您需要使用vm_config,而不是config,您希望提供程序为每个节点申请。
https://stackoverflow.com/questions/54466525
复制相似问题