Book Image

Chef Infrastructure Automation Cookbook

By : Matthias Marschall
Book Image

Chef Infrastructure Automation Cookbook

By: Matthias Marschall

Overview of this book

Table of Contents (14 chapters)
Chef Infrastructure Automation Cookbook Second Edition
About the Author
About the Reviewers

Using version control

Do you manually back up every file before you change it? And do you invent creative file name extensions such as _me and _you when you try to collaborate a file? If you answer yes to any of these, it's time to rethink your processes.

A version control system (VCS) helps you stay sane when dealing with important files and collaborating with them.

Using version control is a fundamental part of any infrastructure automation. There are multiple solutions (some free, some paid) to manage source version control, including Git, SVN, Mercurial, and Perforce. Due to its popularity among the Chef community, we will be using Git. However, you could easily use any other version control system with Chef.


Don't even think about building your infrastructure as code without using a version control system to manage it!

Getting ready

You'll need Git installed on your local workstation. Either use your operating system's package manager (such as Apt on Ubuntu or Homebrew on OS X), or simply download the installer from

Git is a distributed version control system. This means that you don't necessarily need a central host to store your repositories. However, in practice, using GitHub as your central repository has proven to be very helpful. In this book, I'll assume that you're using GitHub. Therefore, you need to go to and create an (free) account to follow the instructions given in this book. Make sure that you upload your Secure Shell (SSH) key by following the instructions at, so that you're able to use the SSH protocol to interact with your GitHub account.

As soon as you have created your GitHub account, you should create your repository by visiting while you're still logged in and using chef-repo as the repository name.

Make sure you have wget installed on your local workstation, in order to be able to download the required files from public servers.

How to do it...

Before you can write any cookbooks, you need to set up your initial Git repository on your development box. Chef Software, Inc. provides an empty Chef repository to get you started. Let's see how you can set up your own Chef repository with Git, using Chef's skeleton.

  1. Download Chef's skeleton repository as a tarball:

     $ wget
    2014-11-30 22:00:43 (1.30 MB/s) - 'master' saved [9309/9309]
  2. Extract the downloaded tarball:

    mma@laptop $ tar xzvf master
  3. Rename the directory:

    mma@laptop:~ $ mv opscode-chef-repo-* chef-repo
  4. Change to your newly created Chef repository:

    mma@laptop:~ $ cd chef-repo/
  5. Initialize a fresh Git repository:

    mma@laptop:~/chef-repo $ git init .
    Initialized empty Git repository in /Users/mma/work/chef-repo/.git/
  6. Connect your local repository to your remote repository on Make sure to replace mmarschall with your own GitHub username:

    mma@laptop:~/chef-repo $ git remote add origin [email protected]:mmarschall/chef-repo.git
  7. Configure Git with your user name and e-mail address:

    mma@laptop:~/chef-repo $ git config --global "[email protected]"
    mma@laptop:~/chef-repo $ git config --global "Your Name"
  8. Add and commit Chef's default directory structure:

    mma@laptop:~/chef-repo $ git add .
    mma@laptop:~/chef-repo $ git commit -m "initial commit"
    [master (root-commit) 6148b20] initial commit
     11 files changed, 545 insertions(+), 0 deletions(-)
     create mode 100644 .gitignore
    create mode 100644 roles/
  9. Push your initialized repository to GitHub. This makes it available to all your co-workers to collaborate on:

    mma@laptop:~/chef-repo $ git push -u origin master
    To [email protected]:mmarschall/chef-repo.git
     * [new branch]      master -> master

How it works...

You have downloaded a tarball containing Chef's skeleton repository. Then, you initialized chef-repo and connected it to your own repository on GitHub.

After that, you added all the files from the tarball to your repository and committed them. This makes Git track your files and the changes you make later.

Finally, you pushed your repository to GitHub, so that your co-workers can use your code too.

There's more...

Let's assume you're working on the same chef-repo repository, together with your co-workers. They cloned your repository, added a new cookbook called other_cookbook, committed their changes locally, and pushed their changes back to GitHub. Now, it's time for you to get the new cookbook downloaded on to your own laptop.

Pull your co-workers' changes from GitHub. This will merge their changes into your local copy of the repository. Use the pull subcommand:

mma@laptop:~/chef-repo $ git pull --rebase
 * branch            master     -> FETCH_HEAD
create mode 100644 cookbooks/other_cookbook/recipes/default.rb

In case of any conflicting changes, Git will help you merge and resolve them.

See also