Provisioning Vagrant w/ Chef

Rob Wilkerson picture Rob Wilkerson · Jan 20, 2012 · Viewed 9.7k times · Source

I'm digging into what it will take to migrate my development environments to Vagrant and I'm having some trouble getting a handle on the VM provisioning process with chef. I've never used chef before and the Vagrant docs in this area intentionally weak (out of scope) so I could use a hand.

I'd like to make this as portable as possible, so I thought it made sense to load cookbooks from a URL so this is what I'm trying in my Vagrantfile:

config.vm.provision :chef_solo do |chef|
    chef.recipe_url = 'https://github.com/opscode/cookbooks/tarball/master'
    chef.add_recipe 'nginx'
    chef.add_recipe 'mysql'
    chef.add_role 'web'

    # You may also specify custom JSON attributes:
    # chef.json = { :mysql_password => '' }
end

I don't think that there's any question that I'm simply misunderstanding something, but I haven't found a documentation source that seems like it fits this Vagrant context.

Thanks.

Answer

Mitchell picture Mitchell · Jan 20, 2012

The way you show above will certainly work fine.

However, if you're going to use Chef solo, I recommend embedding the cookbooks directly within the repository that goes into version control. This makes it so that at every point in time when you commit, the Vagrantfile is aligned with the proper versions of cookbooks. So in 2 years when you check out code from today, the cookbooks should still technically work.

If you want to go this route, create a directory, such as cookbooks, which contains the cookbooks. Then configure Vagrant like so:

config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = "cookbooks"
    chef.add_recipe 'nginx'
    chef.add_recipe 'mysql'
    chef.add_role 'web'

    # You may also specify custom JSON attributes:
    # chef.json = { :mysql_password => '' }
end