Iteration in templates
If we can generate parts of a file from Puppet expressions, and also include or exclude parts of the file depending on conditions, could we generate parts of the file with a Puppet loop? That is to say, could we iterate over an array or hash, generating template content for each element? Indeed we can. This is a very powerful mechanism which enables us to generate files of arbitrary size, based on Puppet variables, or Hiera and Facter data.
Iterating over Facter data
Our first example generates part of the config file for an application which captures network packet data. To tell it which interfaces to listen on, we need to generate a list of all the live network interfaces on the node.
How can we generate this output? We know Facter can give us a list of all the network interfaces available, with $facts['networking']['interfaces']
. This is actually a hash, where the key is the name of the interface, and the value is a hash of the interface's...