1

I am trying to write a Vagrantfile with multiple machines backed up by multiple providers. I specifically want to be able to spawn more than one of those machines in one go. Basically I want to run the command: vagrant up vb_vm aws_vm

I am aware of the --provider flag, but this would apply to all machines being spawned, so not applicable in my case.

This is my (very trimmed down but still valid) Vagrantfile:

Vagrant.configure(2) do |config|

  config.vm.define 'vb_vm' do |vb_vm|
    vb_vm.vm.box='ubuntu/trusty64' # from hashicorp

    vb_vm.vm.provider :virtualbox do |v|
    end
  end

  config.vm.define 'aws_vm' do |aws_vm|
    aws_vm.vm.box = "aws/dummy"
    aws_vm.vm.box_url = 'https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box'

    aws_vm.vm.provider :aws do |a, override|
      a.access_key_id = 'something'
      a.secret_access_key = 'something'
      a.ami='something'
    end
  end
end

A vagrant box list shows that the boxes used for each definitions are of the right type:

aws/dummy        (aws, 0)
ubuntu/trusty64  (virtualbox, 20150928.0.0)

But a vagrant status gives me (note that I do have the lxc plugin available, which became the default)

Current machine states:

aws_vm                    not created (aws)
vb_vm                     not created (lxc)

So this shows that spawning multiple machine with multiple provider is indeed possible, but the choice of provider is wrong.

I am aware of the tricks to set up the default provider, but this only makes things worse (virtualbox used everywhere, aws not used at all...)

I am aware of old stackoverflow questions as well, but they are related to a much older version of Vagrant.

So the question is: how do I make sure that each box defined uses its proper provider?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Guillaume
  • 2,325
  • 2
  • 22
  • 40

1 Answers1

0

The trick will be to create the VM with their own provider.

example: I've defined a quick Vagrantfile (minimized) with boxes for each provider

Vagrant.configure(2) do |config|
  config.vm.define "db" do |db|
    db.vm.box = "..."
    db.vm.hostname = "db"
  end

  config.vm.define "app", primary: true do |app|
    app.vm.box = "..."
    app.vm.hostname = "app"

    app.ssh.forward_agent = true
    app.ssh.forward_x11 = true

    app.vm.provider "vmware_fusion" do |vm|
      vm.vmx["memsize"] = "4096"
    end
  end
end

I create each VM separately

fhenri@machine:~/project/examples/vagrant/multimachine$ vagrant up db --provider=virtualbox
Bringing machine 'db' up with 'virtualbox' provider...
....
fhenri@machine:~/project/examples/vagrant/multimachine$ vagrant up app
Bringing machine 'app' up with 'vmware_fusion' provider...
....

then I halt everything and next time I do vagrant up

fhenri@machine:~/project/examples/vagrant/multimachine$ vagrant up
Bringing machine 'db' up with 'virtualbox' provider...
Bringing machine 'app' up with 'vmware_fusion' provider...

and status looks good

fhenri@machine:~/project/examples/vagrant/multimachine$ vagrant status
Current machine states:

db                        running (virtualbox)
app                       running (vmware_fusion)

This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.
Frederic Henri
  • 51,761
  • 10
  • 113
  • 139
  • This is what I was afraid of. At one point, even if the box is well defined for the right provider, you do need to add --provider. But useful trick with the halt/up, thanks! – Guillaume Nov 06 '15 at 07:07