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.

 

[sourcecode language=”bash”]
#
#
# 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: }

}
[/sourcecode]

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

[sourcecode language=”bash”]
include test-module
[/sourcecode]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.