Agile Testing: Working with Chef cookbooks and roles
I deleted the cookbooks directory and cloned the Opscode cookbooks in its place:
# cd /srv/chef/repos/chef-repo
# git clone git://github.com/opscode/cookbooks
Uploading the cookbooks to the Chef server
Just downloading the cookbooks somewhere on the file system is not enough. The Chef server needs to be made aware of their existence. You do that with the following knife command (which I ran on the Chef server box):
# knife cookbook upload -a -o /srv/chef/repos/chef-repo/cookbooks
BTW, if you specified a non-default configuration file location for knife when you configured it (I specified /etc/chef/knife.rb for example) then you need to make a symlink from that file to ~/.chef/knife.rb, otherwise knife will complain about not finding a config file. At least it complained to me in its strange Swedish accent.
To go back to the knife command above: it says to upload all (-a) cookbooks it finds under the directory specified with -o.
Modifying an existing cookbook
If you look closely under the Opscode cookbooks, there’s one called python. A cookbook contains one or more recipes, which reside under COOKBOOK_NAME/recipes. Most cookbooks have only one recipe which is a file called default.rb. In the case of the python cookbook, this recipe ensures that certain python packages such as python-dev, python-imaging, etc. get installed on the node running Chef client. To add more packages, simply edit default.rb (there’s a certain weirdness in modifying a Ruby file to make a node install more Python packages….) and add your packages of choice.
Again, modifying a cookbook recipe on the file system is not enough; you need to let the Chef server know about the modification, and you do it by uploading the modified cookbook to the Chef server via knife:
# knife cookbook upload python -o /srv/chef/repos/chef-repo/cookbooks
Note the modified version of the ‘knife cookbook upload’ command. In this case, we don’t specify ‘-a’ for all cookbooks, but instead we specify a cookbook name (python). However, the directory remains the same. Do not make the mistake of specifying /srv/chef/repos/chef-repo/cookbooks/python as the target of your -o parameter, because it will not work. Trust me, I tried it until I was enlightened by @kallistec on the #chef IRC channel.
Creating your own cookbook
It’s time to bite the bullet and create your own cookbook. Chef makes it easy to create all the files needed inside a cookbook. Run this command when in the top-level chef repository directory (/srv/chef/repos/chef-repo in my case):

July 17, 2010 | Posted by
Categories:
Tags: