Posted on by & filed under programming.

When first entering the Chef (an open-source, automated infrastructure framework put out by Opscode) world there is a lot to learn. The cooking metaphor introduces new concepts and the vocabulary can be a stumbling block at first. Unfortunately, there are no shortcuts here.  I find the Architecture Introduction wiki page to be helpful in building a mental image of how Chef works. Once the Chef architecture feels comfortable, it might seem like most of the work will be creating a good cookbook. But how do you test a cookbook without having to worry about setting up a chef-server, chef-client and workstation?

Enter Vagrant. Vagrant is a tool that allows developers to create and configure virtual environments and share the configuration via a file aptly named, Vagrantfile. Vagrant will be your Chef Solo.

There are only a few Vagrant commands needed in order to test a cookbook.

  • vagrant init: Initializes (creates) the Vagrantfile
  • vagrant up: Starts the virtualized environment and runs the configuration code (Chef cookbooks or Puppet scripts)
  • vagrant reload: Re-runs the configuration code after an edit to Vagrantfile
  • vagrant ssh: Gives ssh access to the virtualized environment
  • vagrant destroy: Completely destroys the virtualized environment

The Vagrantfile is where all the Chef configuration will happen. There is a section inside the Vagrantfile that can be uncommented to use Chef (or Puppet) as the provisioning tool.

Before continuing, make sure you have a cookbook or two to test. Cookbooks can be found all over Github, but the opscode-cookbooks are the standard cookbooks to get started with.

Inside the Vagrantfile, a few configuration steps need to happen. First, Vagrant has to know where the cookbooks are. Secondly, Vagrant needs to know which cookbooks to run. Once these two pieces of information are in place, run vagrant up and you will see the virtual environment’s output as it runs the cookbooks along with any errors and stacktraces that might happen.

Overriding attributes is a common task when installing cookbooks. Attributes are used to specify a software version and other customizations that take place during software installation. Here is an example Vagrantfile that shows the use of overriding attributes to install Ruby 1.9.3 (using the chef-rvm cookbook) and make it the default Ruby on the virtual environment’s system.

The testing cycle for me goes something like this:

  1. Make changes to Vagrantfile
  2. vagrant reload
  3. vagrant ssh
  4. See if stuff is working how I want it to
  5. Repeat 1-4 until satisfied

Here is the SSH part. It shows that the default ruby interpreter is indeed running Ruby 1.9.3.

So that’s pretty much how I work on cookbooks I’m writing. I find it to be a nice, simple way to get started writing cookbooks.

Tags: chef,

Comments are closed.