Vagrant Shell提供脚本运行两次

时间:2017-02-01 06:31:06

标签: ruby shell vagrant devops

我正在尝试从同一个Vagrant Base Box中启动3个VM。但是,只创建了2个VM。 这是因为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

通过运行配置程序shell ONCE来部署第一个VM。虚拟机启动并且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
...

部署了第二个虚拟机,但由于某种原因,运行配置程序脚本TWICE并失败:

==> 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未配置。 为什么配置脚本会运行两次?

1 个答案:

答案 0 :(得分:2)

因为你正在混合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