24

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.

1
  • In case anyone has tried this lately, the git repo has changed to a message to download the individual repos so this pattern of downloading the master opscode cookbooks branch no longer works. Aug 22, 2013 at 1:16

4 Answers 4

21

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
16

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.

1
  • Another option to librarian-chef is Berkshelf. berkshelf.com. Berkshelf's minor advantage is support for developing cookbooks, and using it to manage them. This doesn't seem to be a recommended/supported use case for librarian-chef.
    – Mikezx6r
    Jul 5, 2012 at 19:14
0

You can also use Berkshelf to manage your cookbooks for you. Berkshelf really does make managing cookbooks easy.

All you need to do is install berkshelf gem, and run berks init to generate an empty Berksfile.

There are couple of different ways to add cookbooks to the berksfile to resolve dependencies.

Example :

To resolve cookbooks from supermarket use:

source 'http://chefsupermarket.com'

cookbook 'mycookbook', '~> 1.3.6'

To resolve from git use:

cookbook "mycookbook", git: "https://github.com/cookbooks/mycookbook.git"

you can also keep cookbooks in your local directory and resolve them using berksfile using:

cookbook 'mycookbook', path: '/path_to_my/chef/cookbook'
0

I prefer a large monolitic repository with all my cookbooks. So I don't use Librarian or Berkshelf, just Vagrant and Chef. I use Chef to create VM's using VirtualBox but also to "bake" Docker containers.

I don't see any portability issues with this approach. For local development all my Chef stuff is in one place ~/chef. I have a folder ~/chef/vagrant which has all my Vagrant files.

My Vagrantfiles will always have something similar to the following.

  config.vm.provision 'chef_zero' do |chef|
    chef.environment = 'some_environment'
    chef.cookbooks_path = '~/chef/cookbooks'
    chef.roles_path = '~/chef/roles'
    chef.data_bags_path = '~/chef/data_bags'
    chef.encrypted_data_bag_secret_key_path = '~/chef/chef_key'
    chef.environments_path = '.'
    chef.nodes_path = '~/chef/nodes'
    chef.add_recipe 'motd'
    chef.add_recipe '..'
    chef.version = '12.14.89'
    # chef.log_level = 'debug'
  end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.