Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify provisioner defaults in Vagrant for multi-machine environments?

I'm wondering if there is a way to specify defaults for provisioners when creating a multi-machine environment with Vagrant?

I was trying to do something like the following:

Vagrant.configure("2") do |config|
  config.vm.box = "andyshinn/ubuntu-precise64"

  config.vm.provision :chef_client do |chef|
    chef.chef_server_url = "https://api.opscode.com/organizations/myorg"
    chef.validation_key_path = "~/.chef/myorg-validator.pem"
    chef.delete_node = true
    chef.delete_client = true
    chef.validation_client_name = "myorg-validator"
  end

  config.vm.define :app do |app|
    app.vm.network "private_network", ip: "172.16.64.61"
    app.vm.host_name = "vagrant-app-#{ENV['USER']}"

    app.vm.provision :chef_client do |chef|
      chef.add_recipe "myrecipe1"
      chef.add_recipe "myrecipe2"
      chef.add_recipe "sudo"
    end
  end

  config.vm.define :web do |web|
    web.vm.network "private_network", ip: "172.16.64.62"
    web.vm.host_name = "vagrant-web-#{ENV['USER']}"

    web.vm.provision :chef_client do |chef|
      chef.add_recipe "myrecipe3"
      chef.add_recipe "myrecipe4"
      chef.add_recipe "sudo"
    end
  end
end

But each VM block does not appear to pick up any of the main config block settings. I get this error:

There are errors in the configuration of this machine. Please fix
the following errors and try again:

chef client provisioner:
* Chef server URL must be populated.
* Validation key path must be valid path to your chef server validation key.

Possible via another method?

like image 816
Andy Shinn Avatar asked Mar 21 '14 20:03

Andy Shinn


People also ask

How do I change my default provider on Vagrant?

In fact, this is quite common. To make this experience better, Vagrant allows specifying the default provider to use by setting the VAGRANT_DEFAULT_PROVIDER environmental variable. Just set VAGRANT_DEFAULT_PROVIDER to the provider you wish to be the default.

What is the Provisioner that used in Vagrant by default?

By default, VirtualBox is the default provider for Vagrant.

What is Provisioner in Vagrant?

Provisioners in Vagrant allow you to automatically install software, alter configurations, and more on the machine as part of the vagrant up process. This is useful since boxes typically are not built perfectly for your use case.


1 Answers

Your issue is one of scope. The variable chef has block scope in the config.vm.provision do block.

once this block exits your changes disappear. You should follow the example in the multi-machine docs on the vagrant site.

config.vm.provision "shell", inline: "echo Hello"

Vagrant.configure("2") do |config|
  config.vm.box = "andyshinn/ubuntu-precise64"


  config.vm.provision "chef_client", chef_server_url: "https://api.opscode.com/organizations/myorg"
  config.vm.provision "chef_client", validation_key_path:  "~/.chef/myorg-validator.pem                
  config.vm.provision "chef_client", delete_node: true
  config.vm.provision "chef_client", delete_client: true
  config.vm.provision "chef_client", validation_client_name: "myorg-validator"


  config.vm.define :app do |app|
    app.vm.network "private_network", ip: "172.16.64.61"
    app.vm.host_name = "vagrant-app-#{ENV['USER']}"

    app.vm.provision :chef_client do |chef|
      chef.add_recipe "myrecipe1"
      chef.add_recipe "myrecipe2"
      chef.add_recipe "sudo"
    end
  end

Should do the trick

like image 135
nsfyn55 Avatar answered Oct 20 '22 22:10

nsfyn55