David Echo
2 min readDec 9, 2014

Creating Your Own Chef Cookbook Generator

The Chef Development Kit (Chef DK) comes with an awesome tool: chef. This command line utility can do a number of things to streamline chef development for you, so let’s look at one of the more valuable things you can do: create your own cookbook generator.

If you’re not already familiar, there are a number of generators in the Chef DK, in a number of different tools. You might create a new cookbook with:

berks cookbook my_cookbook_name

or

chef generate cookbook my_cookbook_name

or even initialize a test kitchen setup with

kitchen init

Of course, if you do this, then you’re probably going to spend the next 30 minutes fixing tiny mistakes, adding standard organization boilerplate, or configuring your .kitchen.yml files. This isn’t a great experience — especially for folks in your organization who might be new to Chef.

Instead, you can create your own generator cookbook and have people in your organization use this instead. You can reference your code generator cookbook on the command line with the -g operator. The command looks like this:

chef generate cookbook my_cookbook_name -g ~/chef/pan

And you’ll get something like this:

What’s the first thing you do when you start cooking? Well, you probably get out a pan. So I call my generator cookbook “pan”. The name is short and easy to remember, and there aren’t any existing metaphors in the chef toolkit for pans. Calling it “code_generator” is a little too verbose for my tastes.

The benefits for your organization for creating this code generator are pretty huge:

  • You establish a standard way to lay out cookbooks.
  • You can include opinionated examples, that you can’t include in a public cookbook.
  • You can evolve the standards at your organization by simply updating the code generator.
  • You can include important, organization specific information in your .kitchen.yml so developers can use kitchen to test new cookbooks right away.
  • You can even use the code generator as a code review tool. “This is the way the generator does it, so please do it this way.”
  • It can be an important way to bring up inconsistencies in workflow in your organization.

And so on. And really creating one isn’t difficult. Here is a link to an example generator that I’ve created for you to start with: https://github.com/echohack/pan

Or, if you don’t like mine, you can start by using the one including in the ChefDK at:

(ChefDK dir)/embedded/apps/chef-dk/lib/chef-dk/skeletons/code_generator

(For me on Mac, this is at /opt/chefdk/embedded/apps/chef-dk/lib/chef-dk/skeletons/code_generator Your milage may vary depending on where you installed ChefDK.)

No matter what you do you’re going to have to change this to be opinionated for you and your organization. If you’re familiar with Chef, this should be a no brainer. Simple edit the recipes/cookbook.rb to stub out anything you’d like by default!

Enjoy cooking with pan.