Create a Cookbook

Chef cookbooks contain a recipe that instructs chef what actions to perform, install packages, enable services, put files in place raw or as a template.

Version Control your Cookbooks

It’s highly recommended to create a version control repo of sorts to manage your cookbooks. We’ll use git, and initialize the repo under /gitrepos, and clone that into /var/chef.

cd /gitrepos/ && mkdir cookbooks
cd cookbooks
git init . --shared=true --bare
cd /var/chef
git clone /gitrepos/cookbooks
cd cookbooks

When we edit cookbooks, we can push the changes to the repo. We can also work on this repo from any workstation that is an authorized knife client which is actually used to push the cookbooks into chef.

Create a base cookbook

Now that we have a repo to work with, lets create a cookbook with knife.

knife cookbook create base

This will create a folder base, with several subdirs and a couple files.

[root@localhost cookbooks]# ls base/
attributes definitions libraries providers recipes templates files metadata.rb resources

Edit default recipe

You will see a file called default.rb that was created under base/recipes/. This is where we want to begin building out this cookbook. It is where you define packages, services, files, commands to run, etc. Lets open this file, and add to it.

vi base/recipes/default.rb

You will see some comments at the top, feel free to edit and customize. Below are the default comments followed by installation of nrpe packages for nagios monitoring, and we’ll ensure that service is running and enabled.

# Cookbook Name:: base
# Recipe:: default
# Copyright 2014, YOUR_COMPANY_NAME
# All rights reserved - Do Not Redistribute

package 'nagios-nrpe' do
action :install

package 'nagios-plugins-nrpe' do
action :install

service 'nrpe' do
action [ :enable, :start ]

You see above that we install two packages related to nrpe, and then enable the service and start it. You might hear ruby programmers talking about DRY which stands for Don’t Repeat Yourself. Above you see two package definitions that use the exact same syntax. We can change it to the following and use fewer lines of code. In this situation, it’s not entirely necessary as the recipe is very short, but as these recipe’s grow, it’s a good idea. So we’ll change it to look like this.

%w{ nagios-nrpe nagios-plugins-nrpe }.each do |pkg|
package "#{pkg}" do
action :install

service 'nrpe' do
action [ :enable, :start ]

It’s shorter, and might be a bit more confusing at first if you’re not used to coding in ruby. But it does the same thing as the first recipe in fewer lines. Once you get the hang of it, being able to write these recipes using ruby code is very useful.

Great, now save the file, and the next step is to upload the cookbook.

Upload cookbook

We will now use knife to upload to cookbook to the chef server.

[root@localhost base]# knife cookbook upload base
Uploading base [0.1.0]
Uploaded 1 cookbook.

Add recipe to node

Eventually we will create roles, and add recipes to them, and then add the roles to the nodes. But for now we will skip that step and add the recipe directly to the node’s run list – in this case localhost.

# if you don't have an env variable set for EDITOR, set it
export EDITOR=vi
# now we can edit the node with knife
# node definitions are in json format
knife node edit localhost

We want the “run_list” piece to look like this.

"run_list": [

We can also add it directly from the command line with knife, but it’s also good for familiarize yourself with the json.

Alternatively, you can add it directly from command line:

knife node run_list add localhost base

Run chef-client

We’ve created a cookbook, uploaded it with knife, and added it to the node’s run_list. Now we want to run chef-client on the node that we edited. In this case it’s localhost. It should look something like this.

[root@localhost base]# chef-client
Starting Chef Client, version 11.16.2
resolving cookbooks for run list: ["base"]
Synchronizing Cookbooks:
- base
Compiling Cookbooks...
Converging 3 resources
Recipe: base::default
* package[nagios-nrpe] action install
- install version 2.15-2.el6 of package nrpe
* package[nagios-plugins-nrpe] action install
- install version 2.15-2.el6 of package nagios-plugins-nrpe
* service[nrpe] action enable
- enable service service[nrpe]
* service[nrpe] action start
- start service service[nrpe]

Running handlers:
Running handlers complete
Chef Client finished, 4/4 resources updated in 33.021513168 seconds 

That’s it, next we’ll get into roles and environments and also adding static files and templates to your cookbook.