Information Technology Reference
In-Depth Information
The lines in the ERB template
without
an equal sign
<% %>
are evaluated as
scriptlets
; the
expression is evaluated but not rendered in the output file as a string. Then we use the ERB
tag
with
an equals sign
<%= %>
to print out a line in the resulting output each time we run
through the conditional logic.
NOTE
In order to determine that
node['network']['interfaces']
was the correct variable
syntax, we inspected the output of
ohai
, looking for the values that we wanted to display.
In your cookbook, edit
index.html.erb
as shown in
Example 16-3
.
We'll use a variant of the
preceding logic that prints out each interface name on the node and its IP address, using more
idiomatic Ruby and some bare-bones HTML.
Example 16-3. chefdk/apache-test/templates/default/index.hmtl.erb
<html>
<html>
<body>
<body>
<pre><code>
<pre><code>
This site was set up by
<
%= node['hostname'] %>
My network addresses are:
<
% node['network']['interfaces'].keys.each do |iface_name| %>
*
<
%= iface_name %>:
<
%= node['network']['interfaces'][iface_name]['addresses'].keys[1] %>
<
% end %>
</code></pre>
</code></pre>
</body>
</body>
</html>
</html>
NOTE
This is admittedly horrible HTML! But it will render readable output, which is our only
goal. Thankfully, this is a book on Chef, not HTML.
Perform a final
kitchen converge
to ensure there are no syntax errors in your code:
$
kitchen converge