Skip to main content

About Templates

[edit on GitHub]



To use a template, two things must happen:

  1. A template resource must be added to a recipe


template '/etc/sudoers' do
  source 'sudoers.erb'
  mode '0440'
  owner 'root'
  group 'root'
  variables(sudoers_groups: node['authorization']['sudo']['groups'],
            sudoers_users: node['authorization']['sudo']['users'])


# /etc/sudoers
# Generated by Chef for <%= node['fqdn'] %>

Defaults        !lecture,tty_tickets,!fqdn

# User privilege specification
root          ALL=(ALL) ALL

<% @sudoers_users.each  do |user| -%>
<% end -%>

# Members of the sysadmin group may gain root privileges

<% @sudoers_groups.each do |group| -%>
<% end -%>

And then set the default attributes in attributes/default.rb:

default['authorization']['sudo']['groups'] = %w(sysadmin wheel admin)
default['authorization']['sudo']['users'] = %w(jerry greg)




A statement is delimited by a modifier, such as if, elsif, and else. For example:

if false
# this won't happen
elsif nil
      # this won't either


When a template is rendered, Ruby expressions and statements are evaluated by Chef Infra Client. The variables listed in the template resource’s variables parameter and in the node object are evaluated. Chef Infra Client then passes these variables to the template, where they will be accessible as instance variables within the template. The node object can be accessed just as if it were part of a recipe, using the same syntax.

For example, a simple template resource like this:

node['fqdn'] = 'latte'
template '/tmp/foo' do
  source 'foo.erb'
  variables(x_men: 'are keen')

And a simple Embedded Ruby (ERB) template like this:


Would render something like:

The node latte thinks the x-men are keen


File Specificity


The pattern for template specificity depends on two things: the lookup path and the source. The first pattern that matches is used:

  1. /host-$fqdn/$source
  2. /$platform-$platform_version/$source
  3. /$platform/$source
  4. /default/$source
  5. /$source


To specify a particular Windows version, use the operating system version number. For example, a template in templates/windows-6.3 will be deployed on systems installed with Windows 8.1.

Use an array with the source property to define an explicit lookup path. For example:

template '/test' do
  source ["#{node.chef_environment}.erb", 'default.erb']

The following example emulates the entire file specificity pattern by defining it as an explicit path:

template '/test' do
  source %W(

A cookbook may have a /templates directory structure like this:


and a resource that looks something like the following:

  source 'text_file.txt'
  mode '0755'
  owner 'root'
  group 'root'

This resource would be matched in the same order as the /templates directory structure. For a node named host-node-desktop that is running Windows 8.1, the second item would be the matching item and the location:


Host Notation

The naming of folders within cookbook directories must literally match the host notation used for template specificity matching. For example, if a host is named, then the folder must be named

Transfer Frequency

The Chef Infra Client caches a template when it is first requested. On each subsequent request for that template, the Chef Infra Client compares that request to the template located on the Chef Infra Server. If the templates are the same, no transfer occurs.

Partial Templates


  • By using the render method in the template file
  • By using the template resource and the variables property.

variables Attribute

The variables property of the template resource can be used to reference a partial template file by using a Hash. For example:

template '/file/name.txt' do
  variables partials: {
    'partial_name_1.txt.erb' => 'message',
    'partial_name_2.txt.erb' => 'message',
    'partial_name_3.txt.erb' => 'message',

where each of the partial template files can then be combined using normal Ruby template patterns within a template file, such as:

<% @partials.each  do |partial, message| %>
  Here is <%= partial %>
  <%= render partial, :variables => {:message => message} %>
<% end %>

render Method


<%= render 'partial_name.txt.erb', :option => {} %>



For example:

<%= render 'simple.txt.erb', :variables => {:user => Etc.getlogin }, :local => true %>

Was this page helpful?


Search Results