Vagrant 在启动多台机器时运行错误的配置文件

Vagrant runs wrong provisioning file when booting multiple machines

我有以下 vagrantfile,它指定了 2 台机器 - 前端和后端框。

Vagrant.configure(2) do |config|
  config.vm.box = "frontend"
  config.vm.network "private_network", ip: "192.168.0.5"  
  config.vm.provider "virtualbox" do |vb|
    vb.gui = true
    vb.memory = "4096"
  end
  config.vm.communicator = "winrm"
  config.vm.provision "shell", path: "Provision.ps1"
  config.vm.define "db" do |db|
    db.vm.box = "backend"
    db.vm.network "private_network", ip: "192.168.0.10"  
    db.vm.provider "virtualbox" do |vb|
        vb.gui = true
        vb.memory = "4096"
    end
    db.vm.communicator = "winrm"
    db.vm.provision "shell", path: "ProvisionRemote.ps1"
  end
end

当我输入 vagrant up 时,根据多机文档,它应该首先启动前端框和 运行 Provision。ps1 然后启动后端框和 运行 ProvisionRemote.ps1就可以了(外在).

然而,实际情况是后端盒先启动,然后它会尝试运行提供。ps1(用于前端盒)。

Bringing machine 'db' up with 'virtualbox' provider...
==> db: Importing base box 'backend'...
==> db: Matching MAC address for NAT networking...
==> db: Checking if box 'backend' is up to date...
==> db: Setting the name of the VM: RemoteBox_db_1459513634410_78500
==> db: Clearing any previously set network interfaces...
==> db: Preparing network interfaces based on configuration...
    db: Adapter 1: nat
    db: Adapter 2: hostonly
    db: Adapter 3: hostonly
==> db: Forwarding ports...
    db: 5985 => 55985 (adapter 1)
    db: 5986 => 55986 (adapter 1)
==> db: Running 'pre-boot' VM customizations...
==> db: Booting VM...
==> db: Waiting for machine to boot. This may take a few minutes...
    db: WinRM address: 127.0.0.1:55985
    db: WinRM username: vagrant
    db: WinRM transport: plaintext
==> db: Machine booted and ready!
==> db: Checking for guest additions in VM...
    db: The guest additions on this VM do not match the installed version of
    db: VirtualBox! In most cases this is fine, but in rare cases it can
    db: prevent things such as shared folders from working properly. If you see
    db: shared folder errors, please make sure the guest additions within the
    db: virtual machine match the version of VirtualBox you have installed on
    db: your host and reload your VM.
    db:
    db: Guest Additions Version: 4.3.28
    db: VirtualBox Version: 5.0
==> db: Configuring and enabling network interfaces...
==> db: Mounting shared folders...
    db: /vagrant => E:/_workingSource/project/env/
==> db: Running provisioner: shell...
    db: Running: Provision.ps1 as c:\tmp\vagrant-shell.ps1

为什么要这样做?我做错了什么?

这种方法以正确的顺序触发配置器...

config.vm.provision "docker" do |d|
  # installs docker
end

config.vm.provision :shell do |sh|
  sh.privileged = false
  sh.inline = $provision
end

config.vm.provision :shell do |sh|
  sh.privileged = false
  sh.path = "generateWebserverInstallEnvironment.sh"
end

您应该突出显示您有 2 台机器

这里你只定义了一台机器(配置并且你确实覆盖了后端块中定义的一些参数,但这实际上是相同的机器定义)所以 vagrant 正在启动你定义的一台机器并尝试 运行 所有供给者

以下工作并定义了 2 台机器

Vagrant.configure(2) do |config|

  config.vm.communicator = "winrm"

  config.vm.define "front" do |front|
    front.vm.box = "frontend"
    front.vm.network "private_network", ip: "192.168.0.5"  
    front.vm.provider "virtualbox" do |vb|
      vb.gui = true
      vb.memory = "4096"
    end
    front.vm.provision "shell", path: "Provision.ps1"
  end  

  config.vm.define "db" do |db|
    db.vm.box = "backend"
    db.vm.network "private_network", ip: "192.168.0.10"  
    db.vm.provider "virtualbox" do |vb|
        vb.gui = true
        vb.memory = "4096"
    end
    db.vm.provision "shell", path: "ProvisionRemote.ps1"
  end
end

config* 参数适用于 2 台机器(如 config.vm.communicator),因此所有常见参数都应针对 config. 变量应用(注意:我没有尝试将虚拟配置下的框提供程序,但只要相同就应该可以工作),如果您需要机器特定参数,则必须在特定块中定义(例如 IP 定义为 front.vm.network "private_network", ip: "192.168.0.5")