Simple Puppet Module – Iterate Through an Array

puppet-logo

OK, this is a very simple and quick one.

I was looking for a simple example of how to iterate through an array in a Puppet module. I basically needed an example of a “for loop” inside a Puppet module that I could, as someone who is not exactly fluent in Ruby, could modify to fit my needs.

I settled on the format that you see below.

The “define” code block is basically the work that we want the Puppet module to do. In this case, it’s create a file in /tmp. Think of this line as pretty much just a placeholder for more complex code.

The “class” code block is where we setup our array of items for the module to “loop” through.

 

#
#
# Test Module
#
#

define myarray_config {
notify { "Item ${name}": }
file {"/tmp/$name":
ensure => present,
mode => 0777,
}
}

class test-module {
$array = [ 'item1', 'item2', 'item3' ]
myarray_config { $array: }

}

Now add the following code to your site.pp to make this module part of your site manifest.

include test-module

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