Chef Infrastructure Automation Cookbook

Chef Infrastructure Automation Cookbook
eBook: $26.99
Formats: PDF, PacktLib, ePub and Mobi formats
save 15%!
Print + free eBook + free PacktLib access to the book: $71.98    Print cover: $44.99
save 6%!
Free Shipping!
UK, US, Europe and selected countries in Asia.
Also available on:
Table of Contents
Sample Chapters
  • Configure, deploy, and scale your applications
  • Automate error prone and tedious manual tasks
  • Manage your servers on-site or in the cloud
  • Solve real world automation challenges with task-based recipes
  • The book is filled with working code and easy-to-follow, step-by-step instructions

Book Details

Language : English
Paperback : 276 pages [ 235mm x 191mm ]
Release Date : August 2013
ISBN : 1849519226
ISBN 13 : 9781849519229
Author(s) : Matthias Marschall
Topics and Technologies : All Books, Networking and Servers, Cookbooks, Open Source

Table of Contents

Chapter 1: Chef Infrastructure
Chapter 2: Evaluating and Troubleshooting Cookbooks and Chef Runs
Chapter 3: Chef Language and Style
Chapter 4: Writing Better Cookbooks
Chapter 5: Working with Files and Packages
Chapter 6: Users and Applications
Chapter 7: Servers and Cloud Infrastructure
  • Chapter 1: Chef Infrastructure
    • Introduction
    • Using version control
    • Installing Chef on your workstation
    • Using the Hosted Chef platform
    • Managing virtual machines with Vagrant
    • Creating and using cookbooks
    • Inspecting files on your Chef Server with Knife
    • Defining cookbook dependencies
    • Managing cookbook dependencies with Berkshelf
    • Downloading and integrating cookbooks as vendor branches into your Git repository
    • Using custom Knife plugins
    • Changing organizations based on the current Git branch
    • Deleting a node from the Chef Server
    • Running Chef Solo
    • Using roles
    • Using environments
    • Freezing cookbooks
    • Running Chef Client as a daemon
    • Using the Chef console (Chef Shell)
    • Chapter 2: Evaluating and Troubleshooting Cookbooks and Chef Runs
      • Introduction
      • Testing your Chef cookbooks
      • Flagging problems in your Chef cookbooks
      • Test Driven Development for cookbooks using ChefSpec
      • Integration testing your cookbooks with Test Kitchen
      • Showing affected nodes before uploading cookbooks
      • Overriding a node's run list to execute a single recipe
      • Using why-run mode to find out what a recipe might do
      • Debugging Chef Client runs
      • Inspecting results of your last Chef Client run
      • Raising and logging exceptions in recipes
      • Diffing cookbooks with knife
      • Using community exception and report handlers
      • Creating custom handlers
      • Chapter 3: Chef Language and Style
        • Introduction
        • Using community Chef style
        • Using attributes to dynamically configure recipes
        • Using templates
        • Mixing plain Ruby with Chef DSL
        • Installing Ruby gems and using them in recipes
        • Using libraries
        • Using definitions
        • Creating your own Light Weight Resource Providers (LWRP)
        • Extending community cookbooks by using application wrapper cookbooks
        • Creating custom Ohai plugins
        • Creating custom Knife plugins
        • Chapter 4: Writing Better Cookbooks
          • Introduction
          • Setting environment variables
          • Passing arguments to shell commands
          • Overriding attributes
          • Using search to find nodes
          • Using data bags
          • Using search to find data bag items
          • Using encrypted data bag items
          • Accessing data bag values from external scripts
          • Getting information about the environment
          • Writing cross-platform cookbooks
          • Finding the complete list of operating systems you can use in cookbooks
          • Making recipes idempotent by using conditional execution
          • Chapter 5: Working with Files and Packages
            • Introduction
            • Creating configuration files using templates
            • Using pure Ruby in templates for conditionals and iterations
            • Installing packages from a third-party repository
            • Installing software from source
            • Running a command when a file is updated
            • Distributing directory trees
            • Cleaning up old files
            • Distributing different files based on the target platform
            • Chapter 6: Users and Applications
              • Introduction
              • Creating users from data bags
              • Securing the Secure Shell Daemon (SSHD)
              • Enabling passwordless sudo
              • Managing NTP
              • Managing nginx
              • Creating nginx sites
              • Creating MySQL databases and users
              • Managing WordPress sites
              • Managing Ruby on Rails applications
              • Managing Varnish
              • Managing your workstation
              • Chapter 7: Servers and Cloud Infrastructure
                • Introduction
                • Creating cookbooks from a running system with Blueprint
                • Running the same command on many machines at once
                • Setting up SNMP for external monitoring services
                • Deploying a Nagios monitoring server
                • Building high-availability services using Heartbeat
                • Using HAProxy to load-balance multiple web servers
                • Using custom bootstrap scripts
                • Managing firewalls with iptables
                • Managing fail2ban to ban malicious IP addresses
                • Managing Amazon EC2 instances
                • Loading your Chef infrastructure from a file with spiceweasel and Knife

                Matthias Marschall

                Matthias Marschall is a software engineer "Made in Germany". His four children make sure that he feels comfortable in lively environments, and stays in control of chaotic situations. A lean and agile engineering lead, he's passionate about continuous delivery, infrastructure automation, and all things DevOps. In recent years, Matthias has helped build several web-based businesses, first with Java and then with Ruby on Rails. He quickly grew into system administration, writing his own configuration management tool before moving his whole infrastructure to Chef in its early days. In 2008, he started a blog ( together with Dan Ackerson. There they shared their ideas about DevOps since the early days of the continually emerging movement. You can find him on Twitter as @mmarschall. Matthias is a CTO at GmbH, helping run Germany's biggest Q&A site among other high-traffic sites. He holds a Master's degree in Computer Science (Dipl.-Inf. (FH)) and teaches courses on Agile Software Development at the University of Augsburg. When not writing or coding, Matthias enjoys drawing cartoons and playing Go. He lives near Munich, Germany.

                Code Downloads

                Download the code and support files for this book.

                Submit Errata

                Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.


                - 13 submitted: last submission 16 May 2014

                Errata type: Typo | Page number: 188

                Instead of


                it should be


                Errata type: Typo | Page number: 63: Step 7

                Instead of
                mma@laptop:~/chef-repo $ subl cookbooks/my_cookbook/recipes/default.rb

                it should be

                mma@laptop:~/chef-repo $ subl cookbooks/my_cookbook/templates/default/greeting.txt.erb

                Errata type: Typo | Page number: 20 | Step 3

                Instead of 
                mma@laptop:~/chef-repo $ knife node run_list add server recipe[my_cookbook]

                it should be

                mma@laptop:~/chef-repo $ knife node run_list add server 'recipe[my_cookbook]'

                Errata type: Typo | Page number: 25 (Step 2) , 62 (Step 2)

                Instead of 
                mma@laptop:~/chef-repo $ bundler install 

                it should be

                mma@laptop:~/chef-repo $ bundle install



                Errata type: Code | Page number: 62,64

                Instead of

                let(:chef_run) {
                platform:'ubuntu', version:'12.04'

                it should be

                let(:chef_run) {
                platform:'ubuntu', version:'12.04'

                Errata type: Code | Page number: 62,65

                instead of


                it should be


                Errata type: Code | Page number: 9 (Step 2)

                instead of

                tar zvf master

                it should be

                tar xzvf master

                Errata type: Code | Page number: 40

                How to do it... section

                Step 2:


                instead of 

                mma@laptop:~/chef-repo $ knife node client my_node


                it should be

                mma@laptop:~/chef-repo $ knife client delete my_node

                Errata type: Code | Page number: 21

                Recipe : Inspecting files on your Chef Server with Knife

                Getting ready section

                Step 2:

                'knife cookbook'

                should be

                'knife cookbook upload iptables'

                Errata type: Code | Page number: 26

                Recipe : Managing cookbook dependencies with Bershelf

                Step number : 5

                mma@laptop:~/chef-repo/cookbooks/my_cookbook $ subl Berksfile

                instead of

                mma@laptop:~/chef-repo $ subl Berksfile

                Errata type: Code | Page number: 16

                In step number 3:

                Fourth line of code is
                config.vm.box_url =

                It should be
                config.vm.box_url = ""


                In the same step:
                Ninth line is
                chef.validation_key_path = "/.chef/-validator.pem"

                It should be
                chef.validation_key_path = ".chef/-validator.pem"

                Errata type: Code | Page number: 17

                There really should be a couple of extra steps after step #5, which reads
                "Log in to your virtual node using SSH". The problem is that stopping here
                doesn't show that the VM was provisioned on the Chef Server of Hosted Chef.
                There should be these additional steps:

                6. Log out of vagrant by typing "exit".
                7. Run "knife client list". Note that "server" is now present in the list.
                8. Go to your server (I'm using Hosted Chef, so it's
       and see that
                "server" is now listed. If it is not, it may take a minute or two. Just
                refresh the page.

                This demonstrates that "vagrant up" will also provision the node and push it
                to the Chef server. (Note that for some reason the node did not appear for me
                until I also ran "vagrant provision", so this may happen to others if they've
                "upped" the box previously. 

                Errata type: Code | Page number: 23/24

                "foodcritic my_cookbook" should be "foodcritic cookbooks/my_cookbook", and he
                should include a note that nothing but empty space is displayed if it runs
                without issues. To get his error message I had to add "include_recipe
                'build-essential'" to /my_cookbook/recipes/default.rb and omit "depends
                'build-essential'" from metadata.rb. 

                Sample chapters

                You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

                Frequently bought together

                Chef Infrastructure Automation Cookbook +    Haskell Data Analysis Cookbook =
                50% Off
                the second eBook
                Price for both: $41.55

                Buy both these recommended eBooks together and get 50% off the cheapest eBook.

                What you will learn from this book

                • Define your infrastructure as code with Chef
                • Set up your local development and testing environment for Chef with cookbooks and Vagrant
                • Debug your cookbooks and Chef runs by using the numerous inspection and logging facilities of Chef
                • Write clean and maintainable cookbooks by following established coding standards and patterns
                • Drive your cookbooks from external data or node specific attributes by using data bags and search
                • Manage and scale your cloud infrastructure by automating your configuration management
                • Extend Chef to meet your advanced needs by creating custom plugins for Knife and Ohai
                • Test your Chef cookbooks and infrastructure by writing examples using Chef's testing infrastructure

                In Detail

                Irrespective of whether you're a systems administrator or a developer, if you're sick and tired of repetitive manual work and not knowing whether you may dare to reboot your server, it's time for you to get your infrastructure automated.

                Chef Infrastructure Automation Cookbook has all the required recipes to configure, deploy, and scale your servers and applications, irrespective of whether you manage 5 servers, 5,000 servers, or 500,000 servers.

                Chef Infrastructure Automation Cookbook is a collection of easy-to-follow, step-by-step recipes showing you how to solve real-world automation challenges. Learn techniques from the pros and make sure you get your infrastructure automation project right the first time.

                Chef Infrastructure Automation Cookbook takes you on a journey through the many facets of Chef. It teaches you simple techniques as well as fully fledged real-world solutions. By looking at easily digestible examples, you'll be able to grasp the main concepts of Chef, which you'll need for automating your own infrastructure. Instead of wasting time trying to get existing community cookbooks running in your environment, you'll get ready made code examples to get you started.

                After describing how to use the basic Chef tools, the book shows you how to troubleshoot your work and explains the Chef language. Then, it shows you how to manage users, applications, and your whole cloud infrastructure. The book concludes by providing you additional, indispensable tools and giving you an in-depth look into the Chef ecosystem.

                Chef Infrastructure Automation Cookbook will help you learn the techniques of the pros by walking you through a host of step-by-step guides to solve real-world infrastructure automation challenges.


                Chef Infrastructure Automation Cookbook contains practical recipes on everything you will need to automate your infrastructure using Chef. The book is packed with illustrated code examples to automate your server and cloud infrastructure.

                The book first shows you the simplest way to achieve a certain task. Then it explains every step in detail, so that you can build your knowledge about how things work. Eventually, the book shows you additional things to consider for each approach. That way, you can learn step-by-step and build profound knowledge on how to go about your configuration management automation.

                Who this book is for

                This book is for system engineers and administrators who have a fundamental understanding of information management systems and infrastructure. It helps if you've already played around with Chef; however, the book covers all the important topics you will need to know. If you don't want to dig through a whole book before you can get started, this book is for you, as it features a set of independent recipes you can try out immediately.

                Code Download and Errata
                Packt Anytime, Anywhere
                Register Books
                Print Upgrades
                eBook Downloads
                Video Support
                Contact Us
                Awards Voting Nominations Previous Winners
                Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
                Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software