A hash is like an array, but each of the elements can be stored and looked up by name (referred to as the key), for example (hash.pp
):
When we run Puppet on this, we see the following notify in the output:
Hash values can be anything that you can assign to variables, strings, function calls, expressions, and even other hashes or arrays. Hashes are useful to store a bunch of information about a particular thing because by accessing each element of the hash using a key, we can quickly find the information for which we are looking.
Creating arrays with the split function
You can declare literal arrays using square brackets, as follows:
Now, when we run Puppet on the preceding code, we see the following notice messages in the output:
However, Puppet can also create arrays for you from strings, using the split
function, as follows:
Running puppet apply
against this new manifest, we see the same messages in the output:
Note that split
takes two arguments: the first argument is the string to be split. The second argument is the character to split on; in this example, a single space. As Puppet works its way through the string, when it encounters a space, it will interpret it as the end of one item and the beginning of the next. So, given the string 'egg beans chips'
, this will be split into three items.
The character to split on can be any character or string:
The character can also be a regular expression, for example, a set of alternatives separated by a |
(pipe) character: