Book Image

Chef Infrastructure Automation Cookbook Second Edition

By : Matthias Marschall
Book Image

Chef Infrastructure Automation Cookbook Second Edition

By: Matthias Marschall

Overview of this book

Table of Contents (14 chapters)
Chef Infrastructure Automation Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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. Package names might be different and 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 take a 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:

  1. 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...