Iteration and loops
Puppet’s approach to iteration and loops is influenced by the fact that its variables are immutable, meaning that once they are set, they cannot be changed. This makes many normal approaches used with loop
or do
keywords to transform data impossible. In early versions of the language, this was worked around by passing an array to defined types, as discussed in Chapter 3, or using inline ERB templates with Ruby code to manipulate arrays and hashes.
However, the issue with the defined type approach was that the code doing the work was abstracted away and not visible. Furthermore, every time a different type of iteration was required, it would require its own defined type, bloating the code. Therefore, it is important to review heritage code and refactor these patterns to the approaches that will be discussed.
In modern Puppet, the approach taken is to use iterative functions that pass data from arrays and hashes to lambdas. A lambda is a function with...