Chef Cookbook Templating with Cookiecutter

Konstantin Lysenko, Atalanta Systems' tech lead, gives an overview of his experiences with the handy Python templating tool, Cookiecutter.

If you are not python averse, you could find cookiecutter a pretty convenient tool for creating your cookbooks templates.

Cookiecutter in a Nutshell

  • cookiecutter is a python based software.
  • it uses Jinja templating engine
  • To install cookiecutter read: Installing Cookiecutter.
  • On my Ubuntu 12.04 workstation I used pip cookiecutter to install it.
  • variables defined in cookiecutter.json file in the root of template directories hierarchy
  • these variables might be used inside files by specifing {{cookiecutter.variable_name}}
  • these variables might be used as a file or directory names, if you call file or directory like: {{cookiecutter.variable_name}}
  • cookiecutter supports pre and post generate hooks.
  • hooks might be shell or python scripts, have names pre_gen_project.[sh,py] or post_gen_project.[sh,py], and should be in hooks dir.

Chef cookbook templating with cookiecutter

Create a directory for template

mkdir chef-cookbook-template
cd chef-cookbook-template

Create a file with template variables

emacs cookiecutter.json
    "author": "John Smith",
    "email": "",
    "cookbook_name": "",
    "company_name": "Example Ltd",
    "release_date": "2014-04-17",
    "year": "2014",
    "version": "0.0.1"

Create a templated directory for cookbook

mkdir "chef-{{cookiecutter.cookbook_name}}"

Create files for your cookbook.

On this stage you should fill your cookbooks with files that you need in your cookbook template. Use variables when it's suitable.

In my cookbook template I added variables to files:

  • attributes/default.rb
  • spec/default_spec.rb
  • metadata.rb
  • {{cookiecutter.cookbook_name}}.packer
  • Vagrantfile
  • .kitchen.yml
  • test/integration/default/serverspec/localhost/default_spec.rb
  • recipes/default.rb

Post-create hooks

It could be anything you want. For example I initialize my cookbook template with git.

In root directory of cookiecutter template add hooks directory.

mkdir hooks
cd hooks
command -v git >/dev/null 2>&1 && git init || { echo >&2 "git init is failed. Probably git is not installed. Install git if it's not installed."; exit 1; }


I presume that:

  • you finished your template
  • and uploaded it to github

Use template from github git repository


Use it again.

  • After first usage template will be copied to ~/.cookiecutter directory.
  • So to use it again you need to specify template name only:

cookiecutter my-chef-cookbook-template

Update template

cd ~/.cookiecutter/my-chef-cookbook-template
git pull

Use template from local dir

  • You can just store template in current directory and use it.
  • Let's presume that you have template in my-chef-cookbook-template dir.
  • Then to use template you just need to execute command:

cookiecutter my-chef-cookbook-template

Tips and tricks

  • If you want to include in resulting file {{ or }}, you can specify them like {{'{{'}} or {{'}}'}}.


Show Comments