At this point, you should have followed the Installation process and created your own Chef Repository. If you haven't done that yet, go do it now - we'll wait.
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.
If you want more details about Cookbooks, Nodes, etc., you want to check out the Cooking with Chef section. If you want more details about how Chef is structured architecturally, check out the Chef Architecture section.
Create a new Cookbook called "quick_start"
From the top of your Chef Repository, run the following:
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. |
Commit your work to your Chef Repository, and update the Chef Server
Commit your work using your favorite version control system, and update any remote repository you may have created earlier. Now, log in to your Chef server, and get a copy of your Chef Repository in your home directory. From the top of your repository:
Register a node with chef-client
Next, you should run chef-client.
If you get a 401 Unauthorized error, that's perfectly normal. Follow the instructions in the Node section to register your node.
| Why do I need to register a client? It's good to make it easy for new systems to appear in your configuration management system. It's not good to just give any old system that shows up whatever it asks for. The registration process makes you think about what's happening. You can bypass it, however - see the Nodes section for more detail. |
Add the quick_start recipe to your new Node
Follow the instructions on managing a node, and add the quick_start to the recipes list.
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 Cooking with Chef, 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
Dikang Gu says:
yes, you should.yes, you should.