Vagrant: ShellProvisioner vs. Chef
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:
#!/bin/sh apt-get update apt-get install -y php5 php5-cli php-pear hash -r pear upgrade-all pear install -f HTTP_Request2
Ruby
Create a cookbooks directory and create structure for your first cookbook in it:
$ mkdir -p my-cookbooks/first/recipes/
Create a default.rb
file with the following content:
# my-cookbooks/first/recipes/default.rb execute "apt-get update" packages = ["php5", "php5-cli", "php-pear"] packages.each|p| do package p end execute "pear upgrade-all" execute "pear install -f HTTP_Request2"
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
- I run
apt-get update
using Chef's execute resource. - 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.)
- I run
pear upgrade-all
using the execute resource. - I run
pear install
using the execute resource.
Vagrantfile
The Vagrantfile
looks slightly different when you provision with chef-solo:
Vagrant::Config.run do |config| config.vm.define :web do |web_config| web_config.vm.box = "lucid64" web_config.vm.host_name = "web" web_config.vm.provision :chef_solo do |chef| chef.cookbooks_path = "PATH-TO-YOUR-COOKBOOKS" chef.add_recipe "first" chef.log_level = :debug end end end
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
.