Iterating over arrays
Iteration (doing something repeatedly) is a useful technique in your Puppet manifests to avoid lots of duplicated code. For example, consider the following manifest, which creates several files with identical properties (iteration_simple.pp
):
file { '/usr/local/bin/task1': content => "echo I am task1\n", mode => '0755', } file { '/usr/local/bin/task2': content => "echo I am task2\n", mode => '0755', } file { '/usr/local/bin/task3': content => "echo I am task3\n", mode => '0755', }
You can see that each of these resources is identical, except for the task number: task1
, task2
, and task3
. Clearly, this is a lot of typing and should you later decide to change the properties of these scripts (for example, moving them to a different directory), you'll have to find and change each one in the manifest. For three resources, this is already annoying...