Posted by & filed under Information Technology, infrastructure, IT, Tech, tools.

If you’ve used Chef, you’ve probably used a community cookbook. Community cookbooks are helpful because someone else has figured out how to solve your problem, be it installing nginx or configuring postgresql. While community cookbooks are great, they sometimes don’t include everything that you need. That’s where wrapper cookbooks come in. If you want to change or extend the functionality of a cookbook without having to rewrite it from scratch, a Chef wrapper cookbook is the way to go.

Do you really need a wrapper cookbook?

When I was new to Chef, I didn’t always review the recipe source code in community cookbooks. Instead, I simply tried out the cookbook that I thought might work, and it often did exactly what I wanted. If I needed expanded functionality, my first thought was to use a wrapper cookbook. I realize now that I was taking the wrong approach to the problem, as I will show in a real-world example that follows.

Our example

At Safari, we use Nagios for monitoring. We’d like to add to Nagios an AddOn called NRPE (Nagios Remote Plugin Executor), which provides checks on hosts that Nagios can query for server status. (Don’t worry, understanding how NRPE works is not necessary for this example.) Fortunately, a community cookbook for NRPE exists, so we’ll use that.

1. Read the docs

Let’s start by checking the README in the repository, which will provide background on what the cookbook does. After reviewing the README, we’ll turn to the “Attributes” section, where we can learn what variables we can set.

2. Read the recipes

One wonderful thing about Chef is that good recipes read like documentation. To read a recipe in a chef cookbook, let’s look in the recipes directory, where we’ll find a default.rb file, the default recipe that is run with the cookbook. Unfortunately, the NRPE default recipe isn’t the easiest recipe to follow, but hang in there. We’re most interested in the last three stanzas of the code, which create a directory, create a file, and start the NRPE service. (As you’d expect, the more recipes you read, the easier they are to follow.)

3. Check the version

One of the best things about community cookbooks is that they are being developed continuously, which means that the version of the cookbook we have on our Chef server may be behind the latest community version. New variables to control different attributes may have been added. Before we begin to extend the wrapper cookbook, we’ll make doubly sure that the newest community cookbook version doesn’t already solve our problem.

Adding a wrapper cookbook

Ok, so we’ve done our homework and checked the cookbook. We’ve determined that we want the cookbook to do what it already does in addition to something else, which we’ll call “Feature A.” We’ve double checked the source code and README and know that some attribute does not already control Feature A. Let’s write our wrapper cookbook to include Feature A. In our real-world case, we want to customize NRPE checks and configuration to put on all of the nodes in our fleet. We’ll need to update the NRPE configuration (beyond what we can control with attributes) and add new files (our checking scripts) to the system.

Feature A consists of two things: one configuration file and one bash script, neither of which could not be added via attributes. We want all nodes to contain this bash script that checks NFS mounts on a server. NRPE will know how to call this script because we’ll add it to a custom configuration file. In our nrpe_wrapper recipe, we call this stanza:

which writes the file

from this template. The recipe then

writes that file to disk and

restarts the nrpe service.

Tags: chef, devops, IT, operations, wrapper cookbooks,

One Response to “When to use Chef wrapper cookbooks (and when not to)”

  1. Linda M Lloyd

    This morning I joined the “new” safari website and paid my 1st month subscription. However how do I log into it ? Every time I try to log into “onlinesafaribooks.com” it tells me I’m inactive, (well, that’s to your Old Safari). Plus I do not see anywhere on the home page where to link to “new” Safari..Please send me the link,

    Thank you