Simple Chef Example

Chef is a tool for the automated deployment of servers, workstations and services. The idea is pretty neat in that once you’ve built your scripts you can tell Chef “I want a new Redhat server running apache, it needs to have Ruby etc. and my web app installed” and Chef will go away and figure everything out and do it.

I’m just learning to use Chef and I wanted to put together a simple example. Getting Chef installed on Ubuntu 10.04 was harder than I expected! The install process here has a LOT of redundant steps. At some point I’ll try this again on a clean server and correct this.

So here’s the process to run an example simple Chef “recipe”. It uses chef-solo, which is a serverless version of Chef.

1. Install Chef. On Ubuntu I did the following. I had some issues during install, and some of the following packages may not be required, but this is exactly what I did:

sudo apt-get install chef ruby-dev build-essential ruby-gems1.9.1 libxml2-dev libxslt1-dev ruby1.9.1-dev
sudo apt-get install libopenssl-ruby1.9.1
sudo apt-get install ruby-gems
sudo apt-get install libmixlib-log-ruby
sudo gem install net-shh net-ssh-multi fog highline # THIS PROBABLY ISN'T REQUIRED
sudo gem install chef                               # THIS PROBABLY ISN'T REQUIRED
sudo gem install knife-ec2
sudo gem install mixlib-authentication
sudo apt-get install libhighline-ruby1.9.1
sudo apt-get install ruby-full
sudo gem install json
sudo gem install highline

2. Create a chef-solo config file and place it in ~/solo.rb (note, you must use the full path to your home directory here):

file_cache_path "/home/YOURUSERNAME/chef-solo"
cookbook_path "/home/YOURUSERNAME/chef-solo/cookbooks"

This just tells Chef where to store cached files and where your “cookbook” lives.

You need to create these directories for Chef:

mkdir chef-solo
mkdir chef-solo/cookbooks

3. Use knife to create a new “cookbook” a “cookbook” is where your “recipes” (install scripts) go. For some reason, knife isn’t anywhere on the path…

/var/lib/gems/1.9.1/gems/chef-0.10.4/bin/knife cookbook create MYCOOKBOOK -c ~/solo.rb

4. Create you recipe file, create a new file called:

~/chef-solo/cookbooks/MYCOOKBOOK/recipes/myrecipe.rb

With the following contents, edit YOURUSERNAME and YOURUSERGROUP accordingly:

if platform?("redhat", "centos", "fedora")
  # code for only redhat based systems.
  file "/tmp/rpm_based" do
    owner "YOURUSERNAME"
    group "YOURUSERGROUP"
    mode "0755"
    action :create
  end
end

if platform?("ubuntu", "debian")
  # code for debian based systems
  file "/tmp/deb_based" do
    owner "YOURUSERNAME"
    group "YOURUSERGROUP"
    mode "0755"
    action :create
  end
end

5. Create a JSON run specification. Create a new file called ~/this.json with the following contents:

{
  "run_list": [
    "recipe[MYCOOKBOOK::myrecipe]"
  ]
}

6. Run the recipe!

chef-solo -c ~/solo.rb MYCOOKBOOK -j ./this.json

This very simple example recipe just checks if the server is running a debian based distribution or a redhat one and writes a file to /tmp accordingly.