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

Using templates


Configuration Management is all about configuring your hosts well. Usually, configuration is carried out by using configuration files. Chef's template resource allows you to recreate these configuration files with dynamic values that are driven by the attributes we discussed so far in this chapter.

You can retrieve dynamic values from data bags, attributes, or even calculate them on the fly before passing them into a template.

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 Creating and using cookbooks recipe in Chapter 1, Chef Infrastructure.

How to do it...

Let's see how to create and use a template to dynamically generate a file on your node:

  1. Add a template to your recipe:

    mma@laptop:~/chef-repo $ subl cookbooks/my_cookbook/recipes/default.rb
    
    template '/tmp/message' do
      source 'message.erb'
      variables(
        hi: 'Hallo',
        world: 'Welt',
        from: node['fqdn']
      )
    end
  2. Add the ERB template...