In my last blog entry, I demo’d how to get started with Vagrant and the ShellProvisioner.

To further illustrate how amazingly simple it is to get started on some Ruby, I’ll convert the shell script from my last blog post to a little recipe for chef. Same objective, we install a PEAR package — but it could be anything really.

Follow me.

Shell

This is the shell script from before:

Ruby

Create a cookbooks directory and create structure for your first cookbook in it:

Create a default.rb file with the following content:

The recipe is later referred to as first or first::default (name of the recipe directory, name of the .rb).

It’s so simple it hurts. ;)

Step by step

  1. I run apt-get update using Chef’s execute resource.
  2. I create an array of the packages (Arrays are ordered in Ruby, hashes are not (up until Ruby 1.9.x). Order is important here.)
  3. I run pear upgrade-all using the execute resource.
  4. I run pear install using the execute resource.

Vagrantfile

The Vagrantfile looks slightly different when you provision with chef-solo:

The important bit: the path to the location of your cookbooks — could be an /absolute/path or ./../relative/path.

Because a Vagrantfile is essentially Ruby code, anything goes here.

Further reading

Getting started with Chef and Ruby can be intimidating or even frustrating at times. Google “chef cookbooks” and you know what I mean.

These links are what you need:

Fin

Save, and enjoy — vagrant up.