官术网_书友最值得收藏!

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:

  1. 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')
        }
      }
  2. Create the file modules/base/templates/addresslist.erb with the following contents:
    <% @ipaddresses.each do |ip| -%>
    IP address <%= ip %> is present
    <% end -%>
  3. 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
    
  4. 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
主站蜘蛛池模板: 漳平市| 镇江市| 和顺县| 当雄县| 深水埗区| 龙胜| 玉溪市| 绥宁县| 仁化县| 蚌埠市| 阜康市| 邵阳市| 桦南县| 德清县| 沅陵县| 苏尼特右旗| 龙井市| 望江县| 玉田县| 阳泉市| 平江县| 申扎县| 泰兴市| 台前县| 电白县| 田林县| 巴中市| 黄大仙区| 耿马| 大城县| 宁城县| 阜新市| 张家界市| 宁津县| 宁德市| 阿荣旗| 隆德县| 施甸县| 威信县| 鄱阳县| 股票|