Ask Your Question

Revision history [back]

For Puppet 3 you can use a defined type to do this. Due to lack of looping and lack of the Puppet 4 anonymous function parameter unpacking you have to unpack the hash in two steps.

The easiest is to use the built-in $title variable on a define or class. This works with the array syntax for declaring a class or resource, like a defined type, to index into the hash.

This is how the community has faked looping for many years.

This was tested with Puppet 3.8.1.

define myfile($contents) {
  $mycontent = $contents[$title]
  file { "/path/${title}": content => $mycontent, }
}
$output = {
  'key1' => 'first value',
  'key2' => 'second value',
  'key3' =>'third value',
}
$outputkeys = keys($output)
myfile { [ $outputkeys ]: contents => $output, }

Puppetlabs-stdlib has the keys() function to get the key from a hash. You will need to require that in any module using the code.

The file resource requires a fully-qualified path, hence using "/path/${fifle}" to make this valid and apply-able Puppet 3 code.

Puppet style checks will still complain about 'the module is not in module loader format' and 'no documentation on the defined type' if you check the code.