Making recipes idempotent by using conditional execution
Chef manages the configuration of your nodes. It is not simply an installer for new software but you will run the Chef client on the existing nodes, as well as new ones.
To speed up your Chef client runs on the existing nodes, you should make sure that your recipes do not try to re-execute resources that have already reached the desired state.
Running resources repeatedly will be a performance issue at best and will break your servers at worst. Chef offers a way to tell resources not to run or only to run if a certain condition is met. Let's look at how conditional execution of resources works.
Getting ready
Make sure you have a cookbook called my_cookbook
and that the run_list
of your node includes my_cookbook
, as described in the Creating and using cookbooks recipe in Chapter 1, Chef Infrastructure.
How to do it…
Let's see how to use conditional execution in our cookbooks:
Edit your
default
recipe to trigger a callback only if you have...