While working on a puppet module I had to iterate over a bunch of values and manage some services based on those values. Because of my background in procedural programming I immediately started looking for a loop construct in the documentation without realizing that puppet is not imperative in nature.
With some help from google, I found out that create_resources can be used to achieve what I wanted. This function takes a hash and converts it into resources.
My hash was stored in hiera, so it was very easy to pass it to the create_resources function. Here is a simplified version of how this was achieved:
$managefiles = { '/some/path/to/my/file' => { ensure => file, }, '/some/dir/path' => { ensure => directory }, } create_resources(file, $managefiles)
Optionally a third parameter can also be passed to create_resources which takes a hash with default values for the resource type you are creating. While the example above uses the build in resource type ‘file’ to demonstrate the usage of create_resources, this function can take any user defined resource type too.
Some excellent blog posts I found on this topic:
https://tobrunet.ch/2013/01/iterate-over-datastructures-in-puppet-manifests/