Using array iteration in templates
In the previous example, we saw that you can use Ruby to interpolate different values in templates depending on the result of an expression. But you're not limited to getting one value at a time. You can put lots of them in a Puppet array and then have the template generate some content for each element of the array using a loop.
How to do it…
Follow these steps to build an example of iterating over arrays:
- Modify your
site.pp
file as follows:node 'cookbook' { $ipaddresses = ['192.168.0.1', '158.43.128.1', '10.0.75.207' ] file { '/tmp/addresslist.txt': content => template('base/addresslist.erb') } }
- Create the file
modules/base/templates/addresslist.erb
with the following contents:<% @ipaddresses.each do |ip| -%> IP address <%= ip %> is present <% end -%>
- Run Puppet:
[root@cookbook ~]# puppet agent -t Info: Caching catalog for cookbook.example.com Info: Applying configuration version '1412141917' Notice: /Stage[main]/Main/Node[cookbook]/File[/tmp/addresslist.txt]/ensure: defined content as '{md5}073851229d7b2843830024afb2b3902d' Notice: Finished catalog run in 0.30 seconds
- Check the contents of the generated file:
[root@cookbook ~]# cat /tmp/addresslist.txt IP address 192.168.0.1 is present. IP address 158.43.128.1 is present. IP address 10.0.75.207 is present.
How it works…
In the first line of the template, we reference the array ipaddresses
, and call its each
method:
<% @ipaddresses.each do |ip| -%>
In Ruby, this creates a loop that will execute once for each element of the array. Each time round the loop, the variable ip
will be set to the value of the current element.
In our example, the ipaddresses
array contains three elements, so the following line will be executed three times, once for each element:
IP address <%= ip %> is present.
This will result in three output lines:
IP address 192.168.0.1 is present. IP address 158.43.128.1 is present. IP address 10.0.75.207 is present.
The final line ends the loop:
<% end -%>
Note
Note that the first and last lines end with -%>
instead of just %>
as we saw before. The effect of the -
is to suppress the new line that would otherwise be generated on each pass through the loop, giving us unwanted blank lines in the file.
There's more…
Templates can also iterate over hashes, or arrays of hashes:
$interfaces = [ {name => 'eth0', ip => '192.168.0.1'}, {name => 'eth1', ip => '158.43.128.1'}, {name => 'eth2', ip => '10.0.75.207'} ] <% @interfaces.each do |interface| -%> Interface <%= interface['name'] %> has the address <%= interface['ip'] %>. <% end -%> Interface eth0 has the address 192.168.0.1. Interface eth1 has the address 158.43.128.1. Interface eth2 has the address 10.0.75.207.
See also
- The Using ERB templates recipe in this chapter