You can declare literal arrays using square brackets, as follows:
define lunchprint() {
notify { "Lunch included ${name}":}
}
$lunch = ['egg', 'beans', 'chips']
lunchprint { $lunch: }
Now, when we run Puppet on the preceding code, we see the following notice messages in the output:
t@cookbook:~$ puppet apply lunchprint.pp
Notice: Compiled catalog for cookbook.strangled.net in environment production in 0.02 seconds
Notice: Lunch included egg
Notice: Lunch included beans
Notice: Lunch included chips
Notice: Applied catalog in 0.04 seconds
However, Puppet can also create arrays for you from strings, using the split function, as follows:
$menu = 'egg beans chips'
$items = split($menu, ' ')
lunchprint { $items: }
Running puppet apply against this new manifest, we see the same messages in the output:
t@cookbook:~$ puppet apply lunchprint2.pp
Notice: Compiled catalog for cookbook.strangled.net in environment production in 0.02 seconds
Notice: Lunch included egg
Notice: Lunch included beans
Notice: Lunch included chips
Notice: Applied catalog in 0.21 seconds
The 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, it's 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, and chips, this will be split into three items.
The character to split on can be any character or string:
$menu = 'egg and beans and chips' $items = split($menu, ' and ')
The character can also be a regular expression, for example, a set of alternatives separated by a | (pipe) character:
$lunch = 'egg:beans,chips'
$items = split($lunch, ':|,')