4

I have the following vagrantfile, which specifies 2 machines - the frontend and the backend box.

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

When I type vagrant up, according to the Multi-Machine documentation it should first boot the front end box and run Provision.ps1 and then boot the backend box and run ProvisionRemote.ps1 on it (outside in).

However, instead what happens is that the backend box boots first, and then it attempts to run Provision.ps1 (which is for the front end box) on it.

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

Why is it doing this? What am I doing wrong?

BlackSpy
  • 5,563
  • 5
  • 29
  • 38

2 Answers2

5

you should highlight that you have 2 machines

here you just define one machine (config and you did override some parameters as defined in the backend block but this is really the same machine definition) so vagrant is booting the one machine you define and tries to run all provisioner

The following works and defines 2 machines

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

the config* parameter applys for the 2 machines (like config.vm.communicator) so all parameter that are common should be applied against the config. variable (note: I did not try to put the virtual box provider under config but it should work as long as it is the same) , and if you need machine specific parameter you must define within the specific block (like the IP defined as front.vm.network "private_network", ip: "192.168.0.5")

Frederic Henri
  • 51,761
  • 10
  • 113
  • 139
  • Thank you so much Frederic. Your explanation is much more explicit and easy to understand than the documentation. – BlackSpy Apr 01 '16 at 14:00
0

this approach fires provisioners in correct order ...

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
danday74
  • 52,471
  • 49
  • 232
  • 283