Configuring Vagrant For Chef

This is the fourth installment of a series on the development of Chef Broiler Plate in which we go over setting up a robust, TDD framework for Chef cookbook development.

Having prepared our CI system and configured our Knife tool in the last post, it's now time to finish or basic setup procedures by setting up a virtual machine with Vagrant. This will give us a local server in which to develop and test our sample cookbook.

Creating a Vagrant Configuration

First, we need to upgrade to the latest version of Vagrant (1.2.2 at the time of writing). This, of course, assumes you have the older version of Vagrant. This 1.1+ version can no longer be installed as a Gem, so we must add it to our dependency list, checking for it with our Rake check task.

Next, run:

vagrant init

to create the configuration file.

Following along with this article, we will build out our Vagrant file to access the environment variables we specified when we set up Knife. This is necessary, because we can no longer access the Chef Gem from the Vagrant file due to the recent upgrades to the system (as the article suggests). As of this writing, there are some pull requests in process that could add more interactivity between Chef settings and Vagrant. I'm using a bentobox that is configured from Opscode to be ready to roll with Vagrant. Our Vagrant file should look something like this:

Vagrant.configure("2") do |config| = "opscode-ubuntu-12.04"
  config.vm.box_url = "" :forwarded_port, guest: 80, host: 8080
  config.vm.synced_folder "shared", "/shared"

  config.vm.provision :chef_client do |chef|
    chef.chef_server_url = ENV['KNIFE_CHEF_SERVER']
    chef.validation_key_path = "#{ENV['KNIFE_VALIDATION_KEY_FOLDER']}/#{ENV['OPSCODE_ORGNAME']}-validator.pem"
    chef.validation_client_name = "#{ENV['OPSCODE_ORGNAME']}-validator"
    chef.node_name = "#{ENV['OPSCODE_USER']}-vagrant"
    chef.run_list = [

Set up the Project Location

The files that will be shared between your virtual machine and local machine should be located in a shared folder; this way you can "develop locally".

mkdir shared
touch shared/.gitkeep

Launching the Virtual Box

I added a simple task to start the Vagrant server, rake start which really just wraps vagrant up.

Running either command will start your server, hook it into your Chef Server account and, well, do nothing right now because our run_list is empty.

Coming up…

Next up, we will (finally) start building a sample cookbook and in the process, begin outfitting our framework with great TDD tools (like "Chefspec").

If you enjoyed reading this or learned something, please consider sharing via , , or . Thanks!