
Chef Infrastructure Automation Cookbook Second Edition
By :

Quite often, you might want to use features of other cookbooks in your own cookbooks. For example, if you want to make sure that all packages required for compiling software written in C
are installed, you might want to include the build-essential
cookbook, which does just that. Chef server needs to know about such dependencies in your cookbooks. You declare them in a cookbook's metadata.
Make sure you have a cookbook named my_cookbook
, and the run_list
command of your node includes my_cookbook
, as described in the Creating and using cookbooks recipe in this chapter.
Edit the metadata of your cookbook in the file cookbooks/my_cookbook/metadata.rb
to add a dependency to the build-essential
cookbook:
mma@laptop:~/chef-repo $ subl cookbooks/my_cookbook/metadata.rb
...
depends 'build-essential'
depends 'apache2', '>= 1.0.4'
If you want to use a feature of another cookbook inside your cookbook, you will need to include the other cookbook in your recipe using the include_recipe
directive:
include_recipe 'build-essential'
To tell the Chef server that your cookbook requires the build-essential
cookbook, you need to declare that dependency in the metadata.rb
file. If you uploaded all the dependencies to your Chef server either using berks install
and berks upload
or knife cookbook upload ...
, the Chef server will then send all the required cookbooks to the node.
The first depends
call tells the Chef server that your cookbook depends on the latest version of the build-essential
cookbook.
The second depends
call tells the Chef server that your cookbook depends on a version of the apache2
cookbook, which is greater or equal to the version 1.0.4
. You may use any of these version constraints with your depends calls:
If you include another recipe inside your recipe, without declaring the cookbook dependency in your metadata.rb
file, Foodcritic
will warn you:
mma@laptop:~/chef-repo $ foodcritic cookbooks/my_cookbook
FC007: Ensure recipe dependencies are reflected in cookbook metadata: cookbooks/my_cookbook/recipes/default.rb:9
Foodcritic
will just return an empty line, if it doesn't find any issues.
Additionally, you can declare conflicting cookbooks through the conflicts
call:
conflicts "nginx"
Of course, you can use version constraints exactly the way you did with depends
.
foodcritic
in the Flagging problems in your Chef cookbooks recipe in Chapter 2, Evaluating and Troubleshooting Cookbooks and Chef RunsChange the font size
Change margin width
Change background colour