Vagrant Shell 配置脚本运行两次

Vagrant Shell Provision Script Runs Twice

我正在尝试从同一个 Vagrant Base Box 启动 3 个虚拟机。但是,只创建了 2 个虚拟机。 这是因为 shell 配置程序脚本在配置第二个 VM 期间执行了两次。 结果,进程终止并出现下面详述的错误。

这是我的 Vagrantfile:

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

#The box name and the URL to Retrieve the Vagrant Box from
  config.vm.box = "eFx-Dev"
  config.vm.box_url = "http://web/provisioning/vagrant-boxes/centos7-basev0.1.box"
  config.ssh.insert_key = false


#Creating the first Dev Machine
#With network address being assigned via DHCP
#and bootstraped via a shell script.
#This script can be unique for each machine.
#But at the moment they are bootstarpped the same.
#The spects of the machine.
#Can be adjusted based on requirements.
  config.vm.define "eFxDev1" do |eFxDev1|
        eFxDev1.vm.hostname = "eFxDev1"
        eFxDev1.vm.box = "eFx-Dev"
        config.vm.network "public_network", type: "dhcp"
        config.vm.provision "shell", path: "vmscripts/bootstrap.sh"
        config.vm.provider "virtualbox" do |vb|
                vb.name = "eFx-Dev1"
                vb.memory = "10124"
                vb.customize ["modifyvm", :id, "--cpus", 4]
  end
end

  config.vm.define "eFxDev2" do |eFxDev2|
        eFxDev2.vm.hostname = "eFxDev2"
        eFxDev2.vm.box = "eFx-Dev"
        config.vm.network "public_network", type: "dhcp"
        config.vm.provision "shell", path: "vmscripts/bootstrap.sh"
        config.vm.provider "virtualbox" do |vb|
                vb.name = "eFx-Dev2"
                vb.memory = "10124"
                vb.customize ["modifyvm", :id, "--cpus", 4]
  end
end

  config.vm.define "eFxDev3" do |eFxDev3|
        eFxDev3.vm.hostname = "eFxDev3"
        eFxDev3.vm.box = "eFx-Dev"
        config.vm.network "public_network", type: "dhcp"
        config.vm.provision "shell", path: "vmscripts/bootstrap.sh"
        config.vm.provider "virtualbox" do |vb|
                vb.name = "eFx-Dev3"
                vb.memory = "10124"
                vb.customize ["modifyvm", :id, "--cpus", 4]
  end
 end
end

第一个 VM 通过 运行 配置器 shell 部署一次。 VM 启动,Vagrant 继续创建第二个 VM:

==> eFxDev1: Importing base box 'eFx-Dev'...
==> eFxDev1: Matching MAC address for NAT networking...
...
==> eFxDev1: Running provisioner: shell...
    eFxDev1: Running: /tmp/vagrant-shell20170201-60595-wpa6qn.sh
==> eFxDev1: + yum install dos2unix -y --disableplugin=fastestmirror
==> eFxDev1: + sudo groupadd Efx
==> eFxDev1: groupadd: group 'Efx' already exists
...

第二个 VM 已部署,但由于某种原因 运行 配置器脚本两次失败:

==> eFxDev2: Importing base box 'eFx-Dev'...
==> eFxDev2: Matching MAC address for NAT networking...
...
==> eFxDev2: Running provisioner: shell...
    eFxDev2: Running: /tmp/vagrant-shell20170201-60595-1fwit5t.sh
==> eFxDev2: + yum install dos2unix -y --disableplugin=fastestmirror
==> eFxDev2: + sudo groupadd Efx
==> eFxDev2: groupadd: group 'Efx' already exists
...
==> eFxDev2: Running provisioner: shell...
    eFxDev2: Running: /tmp/vagrant-shell20170201-60595-1mu7y6h.sh
==> eFxDev2: + yum install dos2unix -y --disableplugin=fastestmirror
==> eFxDev2: Nothing to do
==> eFxDev2: + sudo groupadd Efx
==> eFxDev2: groupadd: group 'Efx' already exists
The SSH command responded with a non-zero exit status. Vagrant
assumes that this means the command failed. The output for this command
should be in the log above. Please read the output to determine what

因此进程失败,第三个 VM 未配置。 为什么配置脚本运行两次?

这是因为您混合了 config 和特定的机器变量。

应用于 config.vm 的任何方法都将应用于您的所有机器(即使您将其放在特定机器块中)因此最好将所有 config.vm 属性放在任何特定机器之外块,您可以将脚本重写为

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

#The box name and the URL to Retrieve the Vagrant Box from
config.vm.box = "eFx-Dev"
config.vm.box_url = "http://web/provisioning/vagrant-boxes/centos7-basev0.1.box"
config.ssh.insert_key = false
config.vm.network "public_network", type: "dhcp"


#Creating the first Dev Machine
#With network address being assigned via DHCP
#and bootstraped via a shell script.
#This script can be unique for each machine.
#But at the moment they are bootstarpped the same.
#The spects of the machine.
#Can be adjusted based on requirements.
config.vm.define "eFxDev1" do |eFxDev1|
  eFxDev1.vm.hostname = "eFxDev1"
  eFxDev1.vm.provision "shell", path: "vmscripts/bootstrap.sh"
  eFxDev1.vm.provider "virtualbox" do |vb|
          vb.name = "eFx-Dev1"
          vb.memory = "10124"
          vb.customize ["modifyvm", :id, "--cpus", 4]
  end
end

config.vm.define "eFxDev2" do |eFxDev2|
  eFxDev2.vm.hostname = "eFxDev2"
  eFxDev2.vm.box = "eFx-Dev"
  eFxDev2.vm.provision "shell", path: "vmscripts/bootstrap.sh"
  eFxDev2.vm.provider "virtualbox" do |vb|
          vb.name = "eFx-Dev2"
          vb.memory = "10124"
          vb.customize ["modifyvm", :id, "--cpus", 4]
  end
end

config.vm.define "eFxDev3" do |eFxDev3|
  eFxDev3.vm.hostname = "eFxDev3"
  eFxDev3.vm.box = "eFx-Dev"
  eFxDev3.vm.provision "shell", path: "vmscripts/bootstrap.sh"
  eFxDev3.vm.provider "virtualbox" do |vb|
          vb.name = "eFx-Dev3"
          vb.memory = "10124"
          vb.customize ["modifyvm", :id, "--cpus", 4]
  end
 end
end