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 create these configuration files with dynamic values that are driven by the attributes we've discussed so far in this chapter. You can retrieve dynamic values from data bags and 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 the 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:
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
Add the
ERB...