Iterate resources in a puppet template

Solution 1:

You could use R.I.Pienaar's puppet-concat module where you build a single file out of many smaller files or templates.

The define would then look something like this:

define host($address, $names) {
  concat::fragment{"hosts-$address":
    target  => "/etc/hosts",
    content => template("network/hosts_single.erb")
  }
}

The hosts_single.erb template would represent a single line in the file. You'd probably also add a fragment for a header too and set order => "01" to ensure it's at the top of the generated file (10 is the default).

Solution 2:

I'd look at the augeas library for managing the entries in your /etc/hosts file. It's a prerequisite for Puppet, so it's already installed. You may need to download an additional augeas package to gain access to the augtool command line utility. This is useful for testing before Puppet integration.

The hosts file entry an example listed on the main site: http://augeas.net/tour.html

or

http://honglus.blogspot.com/2012/01/augeas-quick-start.html

I suppose you could also distribute a standard hosts file and just modify the line that needs to be unique per host...