4

我有以下 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 时,根据 Multi-Machine 文档,它应该首先启动前端盒并运行 Provision.ps1,然后启动后端盒并在其上运行 ProvisionRemote.ps1(由外而内)。

然而,发生的事情是后端盒首先启动,然后它尝试在其上运行 Provision.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

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

4

2 回答 2

5

你应该强调你有两台机器

在这里您只需定义一台机器(配置并且您确实覆盖了后端块中定义的一些参数,但这实际上是相同的机器定义)所以 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.变量(注意:我没有尝试将虚拟盒提供程序放在 config 下,但只要它相同,它就应该工作) ,如果您需要机器特定参数,则必须在特定块内定义(如定义为的 IP front.vm.network "private_network", ip: "192.168.0.5"

于 2016-04-01T13:35:25.817 回答
0

这种方法以正确的顺序触发供应商......

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
于 2016-04-01T13:13:06.637 回答