Raising and logging exceptions in recipes
Running your own cookbooks on your nodes might lead into situations where it does not make any sense to continue the current Chef run. If a critical resource is offline or a mandatory configuration value cannot be determined, it is time to bail out.
However, even if things are not that bad, you might want to log certain events while executing your recipes. Chef offers the possibility to write your custom log messages and exit the current run, if you choose to do so.
In this section, you'll learn how to add log statements and stop Chef runs using exceptions.
Getting ready
You need to have at least one cookbook you can modify and run on a node. The following example will use the ntp
cookbook.
How to do it...
Let's see how to add our custom log message to a recipe:
Add log statements to the
ntp
cookbook's default recipe:mma@laptop:~/chef-repo $ subl cookbooks/ntp/recipes/default.rb
Chef::Log.info('** Going to install the ntp service now...') service node...