Writing cross-platform cookbooks
Imagine you have written a great cookbook for your Ubuntu node and now you need to run it on that CentOS server. Ouch! It will most probably fail miserably. The package names might be different and the configuration files are in different places.
Luckily, Chef provides you with a host of features to write cross-platform cookbooks. With just a few simple commands, you can make sure that your cookbook adapts to the platform that your node is running on. Let's look at how to do this...
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…
Retrieve the node's platform and execute the conditional logic in your cookbook depending on the platform:
Log a message only if your node is on Ubuntu:
mma@laptop:~/chef-repo $ subl cookbooks/my_cookbook/recipes/default.rb Log.info("Running on ubuntu") if node...