How do I use the new opscode repo with Vagrant?

chef-infra, vagrant

Solution

First, my advice is to not use anyone's cookbooks directly from the master branch on Git. If the individual authors/maintainers of cookbook repositories - individual or monolithic - have Git tags for each "version", use that. Otherwise, use a specific commit that you know works for your infrastructure. While GitHub is a useful site for sharing code, it is important to keep in mind that those are development repositories. To compare, most people wouldn't use the master branch of a library core to their application, they'd use the released version of that library (whether it's a gem, cpan module, package, whatever).

Second, Opscode's cookbooks have released versions that are tested to make sure they do what their README says they do. Those releases are published to the Chef Community Site. While they have repositories on GitHub, we recommend using the releases from the site.

To get the cookbooks from the site to your local system, there are multiple tools to support local development workflow. Internally, Opscode is using Berkshelf, as are many people in the community. It's a fairly complete workflow management tool that includes dependency resolver similar to RubyGems Bundler. Berkshelf also has integration with Vagrant, which means all the cookbooks in your "berksfile" get copied to the Vagrant machine for use in the Chef run.

A sample Berksfile in the top-level of the chef-repo looks like this;

site :opscode
cookbook "nginx"
cookbook "mysql"
cookbook "my_application", :git => "https://git.example.com/cookbooks/my_application.git"

Then use `berks install` to download the cookbooks. Berkshelf will use the metadata in the cookbooks to resolve their dependencies, too. This will use the community site API to download the nginx and mysql cookbooks, and then retrieve the "my_application" cookbook from an internal Git repository. Then, to use berkshelf in vagrant, put this at the top of your vagrantfile;

require 'berkshelf/vagrant'

Then when you "vagrant up", berkshelf will copy all the cookbooks into the Vagrant machine if you're using Chef Solo. If you're using Chef Client/Server, then you use `berks upload` to upload all the cookbooks to the Server, and the client provisioner in vagrant will download from the server.

Jamie Winsor, one of the primary developers of Berkshelf at Riot Games has written a thorough guide.

An alternative tool to Berkshelf is Librarian Chef. It is purely a dependency management tool and not a workflow tool. It "takes over" your `cookbooks` directory managing all the content there, and your customized cookbooks go into `site-cookbooks`. You then use Vagrant with the solo provisioner as normal, or client/server with `knife cookbook upload`.

Finally, `knife` as it ships with the Chef Client RubyGem has a plugin, `cookbook site install`. While this is supported and maintained by Opscode as part of the Chef gem, many folks are shifting their workflow to use Berkshelf or Librarian. The Chef documentation has more information on this plugin.

If you have further questions, the #chef irc channel or Chef mailing list are great places to ask questions and discuss workflow.

Problem

I used Vagrant awhile ago and was able to use the following: ``` 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 ``` Now it seems that's deprecated. How the heck do I use the new repo?

Original source