Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Provisioning Vagrant w/ Chef

Tags:

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.

like image 391
Rob Wilkerson Avatar asked Jan 20 '12 11:01

Rob Wilkerson


People also ask

What does Vagrant provision do?

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.

What is chef vagrant?

The Vagrant Chef Solo provisioner allows you to provision the guest using Chef, specifically with Chef Solo. Chef Solo is ideal for people who are already experienced with Chef, already have Chef cookbooks, or are looking to learn Chef.

What is Chef_zero?

Chef Zero is a simple, easy-install, in-memory Chef server that can be useful for Chef Client testing and chef-solo-like tasks that require a full Chef Server. It IS intended to be simple, Chef 11+ compliant, easy to run and fast to start. It is NOT intended to be secure, scalable, performant or persistent.


2 Answers

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 
like image 154
Mitchell Avatar answered Oct 05 '22 20:10

Mitchell


Unfortunately, there isn't a lot of documentation on Vagrant in combination with Chef. I recommend having a look into using librarian to manage your cookbooks for you. I typically have a block similar to this in my Vagrantfile:

config.vm.provision :chef_solo do |chef|   chef.cookbooks_path = "chef/cookbooks"   chef.roles_path     = "chef/roles"    # ... end 

I create a new directory in the root of my project called chef and add the following lines to my .gitignore:

# Ignore librarian bundled cookbooks and cache /chef/cookbooks /chef/tmp 

In the chef directory, you can run librarian-chef init and begin adding cookbooks to the created Cheffile in a way you would with a Gemfile:

cookbook "apt",   :git => "git://github.com/fnichol/chef-apt.git",   :ref => "cacher-client-solo-support"  cookbook "imagemagick", "0.2.2"  cookbook "mongodb",   :git => "git://github.com/edelight/chef-cookbooks.git",   :ref => "0.11.0" 

You get the benefits of being able to specify git repositories, file paths, and versions to bundle which is extremely helpful when you're using cookbooks from multiple sources. When you run librarian-chef install librarian will fetch all of the specified cookbooks and store them in the cookbooks directory, relative to the location of your Cheffile.

like image 33
Ivy Evans Avatar answered Oct 05 '22 22:10

Ivy Evans