Vagrant provider specific variables

518 views Asked by At

In a vagrantfile I would like to set some provider specific variables. After realising that I cannot set the values in these sections (because of this):

config.vm.provider "virtualbox" do |vb, override|
    ...
end

... this is my workaround - Basically I set an environment variable that I can check and then set the provider settings accordingly:

  if ENV['VAGRANT_PROVIDER'] == 'virtualbox'

    config.hostmanager.enabled = true
    tld = "local"
    dbadmin_pass = "vagrant"

  elsif ENV['VAGRANT_PROVIDER'] == 'aws'

    config.hostmanager.enabled = false
    tld = "com"
    dbadmin_pass = "myprodpass" 

  else

    raise Vagrant::Errors::VagrantError.new, "Missing environment variable or invalid value: VAGRANT_PROVIDER [virtualbox|aws]"

  end

This is really hacky though and requires me to set the environment variable as well.

I'm not a ruby expert at all - Is there a better way to set provider specific variables?

1

There are 1 answers

1
Liviu Damian On

You can define providers like:

config.vm.provider "virtualbox" do |vb|
    config.hostmanager.enabled = true
    tld = "local"
    dbadmin_pass = "vagrant"
end

config.vm.provider :aws do |aws|
    config.hostmanager.enabled = false
    tld = "com"
    dbadmin_pass = "myprodpass" 
end

Then start your vagrant with:

vagrant up --provider=virtualbox

or

vagrant up --provider=aws

Make sure you override any provider specific variables, see the Vagrant Docs - Overriding Configuration section.