Looping and puppet

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:




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s