Iterating over multiple items
Arrays are a powerful feature in Puppet; wherever you want to perform the same operation on a list of things, an array may be able to help. You can create an array just by putting its contents in square brackets:
$lunch = [ 'franks', 'beans', 'mustard' ]
How to do it…
Here's a common example of how arrays are used:
Add the following code to your manifest:
$packages = [ 'ruby1.8-dev', 'ruby1.8', 'ri1.8', 'rdoc1.8', 'irb1.8', 'libreadline-ruby1.8', 'libruby1.8', 'libopenssl-ruby' ] package { $packages: ensure => installed }
Run Puppet and note that each package should now be installed.
How it works…
Where Puppet encounters an array as the name of a resource, it creates a resource for each element in the array. In the example, a new package
resource is created for each of the packages in the $packages
array, with the same parameters (ensure => installed
). This is a...