Vagrant Shell Provision脚本运行两次

3
我正在尝试从同一个Vagrant Base Box启动3个虚拟机,但只有2个虚拟机被创建。这是因为在第二个虚拟机的配置过程中,shell provisioner脚本会被执行两次。结果,该进程会因下面详细描述的错误而终止。
以下是我的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

第一个虚拟机通过运行部署程序 shell 一次性部署。虚拟机启动后,Vagrant 继续创建第二个虚拟机。
==> 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
...

第二个虚拟机已经部署,但由于某些原因,运行配置脚本时发生了两次,并导致失败:
==> 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

因此,该过程失败,第三个虚拟机未得到配置。 为什么配置脚本会运行两次?

1个回答

5

这是因为您混淆了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

谢谢@FrédéricHenri,这解决了问题。我还发现,我可以把config.vm.provision "shell",path:"vmscripts/bootstrap.sh"提前放置。这样脚本就会为每个实例执行一次。 - SSF

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接