This post first appeared on The Wandering IT Consultant blog.
What is a Wrapper Cookbook?
Simply put, a wrapper cookbook is just a regular cookbook that includes recipes from other cookbooks.
Common use cases for wrapper cookbooks include:
- Modifying the behavior of a community cookbook from the Chef Supermarket
- Bundling several base cookbooks into a single cookbook
- Version controlling a node’s run list and attribute definitions
Writing a Wrapper Cookbook
To include another cookbook in your wrapper cookbook you must do a minimum of two things:
- Add dependencies to your wrapper cookbook’s
- Add an
include_recipeline to your wrapper cookbook’s
Including dependencies is a simple as adding the following to your
You can also optionally perform version pinning like so:
depends 'public_cookbook', '= 1.4.5'
For more information about version pinning see the
metadata.rb page on the Chef Docs site.
Setting attributes in your wrapper cookbook is a common way to modify the behavior of the cookbook you are wrapping. Well written community cookbooks support modifying their behavior in this manner and will document its attributes within their
These attributes can be added in your wrapper cookbook’s
attributes/default.rb and/or in your default recipe before your
I decide where to place the attributes as follows: If the attributes are computed using other attributes or set via logic (e.g
unless) place them in
recipes/default.rbotherwise place them in
Completing the Wrap
In order to add the functionality from your wrapped cookbook, you will need to include that cookbook in your wrapper cookbook’s default recipe.
This is usually done with the help of the
include_recipe method, like so:
Once you have completed this your cookbook is ready for use.
Sample Use Cases
Creating the wrapper
By running the following
chef command we can generate our wrapper cookbook:
chef generate cookbook my_company_iis
From here we add the following to our
Then we can add the necessary
include_recipe line to our
Doing the actions above will create a wrapper cookbook that will use the IIS cookbook to:
- Install IIS
- Ensure the
w3svcis enabled and started
- Serve the
Default Web Site
Modifying Public Cookbook Behavior
The above example is great, but let’s assume that your company hosts its websites on
D: instead of
C:. We can change this by modifying the attributes that the IIS cookbook consumes.
To host websites out of
D: add the following to your wrapper cookbook’s
default['iis']['pubroot'] = 'D:\\inetpub' default['iis']['docroot'] = 'D:\\inetpub\\wwwroot' default['iis']['log_dir'] = 'D:\\inetpub\\logs\\LogFiles' default['iis']['cache_dir'] = 'D:\\inetpub\\temp'
Adding this to your wrapper cookbook’s attributes file will modify the behavior of the IIS cookbook.
By completing the above you have now created a base cookbook that will install IIS in the fashion that your company desires. Now we can expand on this by utilizing an additional wrapper cookbook.
Let’s say we did the same as above but also created a
my_company_app cookbook and included our
my_company_iis cookbook with a hard version pinning. By doing this we can give the developer of
my_company_app the freedom to have IIS installed to company specifications, but without worrying about how things work behind the scenes.
This allows one team to focus on coding the logic that deploys their web application without also having to code the logic for installing IIS to company specifications.
Eventually you will have multiple base cookbooks and you may want to combine them into a single logical unit, so that can be tested together. Take for example a cookbook called
role_my_company_website. This cookbook’s default recipe might look like the following:
include_recipe 'my_company_windows_base::default' include_recipe 'my_company_audit::default' include_recipe 'my_company_iis::default' include_recipe 'my_company_website::default'
Then in this cookbook’s
metadata.rb you would have hard version pinnings for each of the dependant cookbooks.
By doing this you can now apply
role_my_company_website to a node and test it as a cumulative collection of all its underlying cookbooks. Then, if all the dependant cookbooks have proper tests, you only have to worry about testing the output of
role_my_company_website without having to test each of its underlying components.
This reduces the amount of repeated work and produces an artifact that is:
- Easy to understand
- Version controlled
- Independently testable
This also leads to a cookbook that succinctly describes a particular node in your Chef managed ecosystem. You could use this succinct description of node function to your advantage. For example, your load balancer cookbook could find all nodes that have the
recipes['role_my_company_website']and automatically add them to it’s backend server list.
Advanced Use Cases
You may run into a scenario where a community cookbook performs 99% of what you require, but also does something you don’t want it to do, as well.
Ideally, you should submit a pull request to the cookbook’s maintainer that makes the default behavior tunable, but in a pinch you can do things such as modifying the resource collection during the compile phase.
Modifying the Resource Collection
Take for example, a cookbook that lays down a file on the file system via a template, but that cookbook’s template doesn’t suit your needs.
In your recipe you can use the
edit_resource helper method provided by Chef’s Recipe DSL to modify their template resource to point to a template in your wrapper cookbook instead.
In practice it looks like this:
include_recipe 'bad_cookbook::default' edit_resource(:template, 'C:\\important\\template\\path.ini') do source 'my_beautiful_template.erb' cookbook 'my_awesome_wrapper' end
Adding this to your wrapper cookbook’s default recipe would allow you to use their cookbook as intended with the exception that your template will be used and not theirs.
While simple on the surface, wrapper cookbooks provide critical functions to healthy Chef adoption. With the building blocks of attributes, includes, and version pinning you are able to perform many functions in a safe, repeatable, and efficient manner.
With wrapper cookbooks, you can modify the default behavior of community cookbooks without writing any additional code than a mere variable assignment. You can create reusable base cookbooks that can be consumed by developers’ application cookbooks without requiring them to have in-depth operations knowledge. You can even condense multiple independent cookbooks into a single version controlled, and independently testable, artifact that succinctly describes a specific node’s function in your ecosystem.
Now take this knowledge of wrapper cookbooks and go create something beautiful!