Information Technology Reference
In-Depth Information
Create an ERB template for the
index.html
file.
Chef Development Kit:
$
chef generate template index.html
Chef Client:
$
touch templates/default/index.html.erb
Let's do a little something new with the
index.html.erb
template. In
Introducing the Template
files, evaluates the expression within the tag, and returns a string as output:
This site was set up by <%= node['hostname'] %>
If the tag does not have an equal sign, it is evaluated as a
scriptlet
instead of a string. Within
Chef templates, this mechanism is used to add conditional logic. For example, take a close
look at this ERB template:
<% for @interface in node['network']['interfaces'].keys %>
* <%= @interface %>
<% end %>
TIP
Pay close attention to where
<% %>
is used and where
<%= %>
is used.
On my test node, which has three network interfaces—
lo
,
eth0
, and
eth1
—the scriptlet
will render the following output:
* lo
* eth0
* eth1