|
At this point, you should have completed the installation process and have a working Chef server. This page is a quick guide to writing Chef Cookbooks. It isn't comprehensive, but it will show how some basic components work and we'll explain some terms along the way To learn more about Chef's terminology, check out the Chef Basics section. If you want more details about how Chef is structured architecturally, check out the Chef Architecture section. Get a Chef RepositoryA Chef repository is just a directory structure where you keep your cookbooks and other files. If you haven't done so already, clone Opscode's Chef repository from github: Clone the Chef Repository from Opscode |
Table of Contents
|
Create a new Cookbook called "quick_start"
Change your working directory to the cookbooks/ directory of your Chef repository. If you cloned the repository into /tmp/sandbox/ as shown above:
Now create the cookbook with knife cookbook create:
This will create the cookbook's directory structure:
| What is a Cookbook? Cookbooks are how things are distributed and shared in Chef. Most of your time building infrastructure in Chef will be spent writing Cookbooks. |
This cookbook is going to have a simple recipe that renders a simple text file using some Attributes we will define.
Add an attribute file called "quick_start.rb"
With your favorite text editor, create a file in cookbooks/quick_start/attributes called quick_start.rb, with the following contents.
| What is an Attribute? Attributes in a Cookbook allow you create settings on a node, which you can then access from within a recipe. Attributes are persistent between Chef runs, and edit-able through the Chef Server web interface! |
Add a template resource to the default recipe
Next, edit cookbooks/quick_start/recipes/default.rb and add the following:
| What is a Recipe? (or a Resource, for that matter!) Recipes allow you to specify Resources to manage, in the order they should be managed. In this case, you are creating a recipe called "quick_start", which has a single template resource called template["/tmp/deep_thought.txt"]. |
Add the template file itself
In the template resource we defined above, we referenced a source parameter. This is the actual template file, and it is placed in cookbooks/quick_start/templates/default/deep_thought.txt.erb. Lets create it now:
| What kind of Template is that? Chef uses Erubis, a fast version of ERB, to render templates. Read more about it in the Templates section. |
Upload the Cookbook to the Server
You should be keeping your cookbooks in version control, and you should start getting in the habit now, so commit your work using your favorite version control system.
Now that your work is in version control, upload the cookbook to the server.
The output will look like the following, though your file checksums will be different:
Confirm that the server has the cookbook you just uploaded:
Register a node with chef-client
For this step, you'll need a new host to run chef-client. We recommend you use a virtual machine with snapshots for the maximum effectiveness when developing cookbooks. If you'd like to follow this pattern, simply create a VM with the base OS of your choice and install chef client on it. We also recommend you take a snapshot of the VM in this pristine state—if trouble strikes, you'll be able to roll back and try again.
Once you have chef-client installed on the host, you'll need to copy the validation key, /etc/chef/validation.pem, from your Chef server box to the same location on the chef-client box. The validation key is used by the chef-client as a temporary identity so it can register itself; after it registers it will have its own key in /etc/chef/client.pem (read more about Chef's authentication system).
With the validation key in place, run chef client:
Add the quick_start recipe to your new Node
From your development machine (where you run knife from) run the knife node run_list add command. Use the name of the client machine for HOSTNAME (If you're not sure what to put here, check the output of knife node list).
You can see that your change was made on the server using knife node show:
Run chef-client again.
This time, executing chef-client will cause /tmp/deep_thought.txt to appear!
If you run the command again, you'll notice that it does nothing at all, because the deep_thought.txt file has not changed, either on disk or on the Chef Server.
Congratulations!
You have just written your first Chef Cookbook! You'll be managing your entire infrastructure in no time!
Next Steps
We recommend you move on to Chef Basics, which will give you an in-depth overview of the different Chef components.
Comments (4)
Apr 28, 2009
Devin Ben-Hur says:
Tried to find an Edit link, but this is the best I could see. On the line that ...Tried to find an Edit link, but this is the best I could see.
On the line that reads "This time, executing chef-client will cause /tmp/deep_though.txt to appear!", s/though.txt/thought.txt/
Apr 29, 2009
Adam Jacob says:
You should be able to edit if you are logged in. Hit Ctrl-E, or the Edit link a...You should be able to edit if you are logged in. Hit Ctrl-E, or the Edit link at the top of the page.
Jul 30, 2009
Jin-young Heo says:
I have to run chef-client as root, don't I?I have to run chef-client as root, don't I?
Sep 29, 2009
Dikang Gu says:
yes, you should.yes, you should.