Using array iteration in templates

In the preceding example we saw that you can use Ruby to interpolate different values in templates depending on the result of an expression. You can also use a loop to generate content based on; the elements of an array:

How to do it…

  1. Add the following to your manifest:
    $ipaddresses = [ '192.168.0.1',
                     '158.43.128.1',
                     '10.0.75.207' ]
    
    file { "/tmp/addresslist.txt":
        content => template("admin/addresslist.erb")
    }
  2. Create the file /etc/puppet/modules/admin/templates/addresslist.erb with the following contents:
    <% ipaddresses.each do |ip| -%>
    IP address <%= ip %> is present.
    <% end -%>
  3. Run Puppet:
    # puppet agent --test
    info: Retrieving plugin
    info: Caching catalog for cookbook.bitfieldconsulting.com
    info: Applying configuration ...

Get Puppet 2.7 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.