官术网_书友最值得收藏!

  • Puppet 3 Cookbook
  • John Arundel
  • 466字
  • 2021-04-09 23:52:28

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:

  1. 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 }
  2. 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 very compact way of instantiating lots of similar resources.

There's more…

Although arrays will take you a long way with Puppet, it's also useful to know about an even more flexible data structure: the hash.

Using hashes

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:

$interface = {'name' => 'eth0',
              'address' => '192.168.0.1'}
notify { "Interface ${interface['name']} has address 
  ${interface['address']}": }

Interface eth0 has address 192.168.0.1

Hash values can be anything that you can assign to a variable: strings, function calls, expressions, even other hashes or arrays.

Creating arrays with the split function

You can declare literal arrays using square brackets, as follows:

define lunchprint() {
  notify { "Lunch included ${name}": }
}

$lunch = ['egg', 'beans', 'chips']
lunchprint { $lunch: }

Lunch included egg
Lunch included beans
Lunch included chips

But Puppet can also create arrays for you from strings, using the split function, as follows:

$menu = 'egg beans chips'
$items = split($menu, ' ')
lunchprint { $items: }

Lunch included egg
Lunch included beans
Lunch included chips

Note that split takes two arguments: the first is the string to be split. The second 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 a string:

$menu = 'egg and beans and chips'
$items = split($menu, ' and ')

It can also be a regular expression, for example, a set of alternatives separated by a | (pipe) character:

$lunch = 'egg:beans,chips'
$items = split($lunch, ':|,') 
主站蜘蛛池模板: 龙井市| 大方县| 进贤县| 霍山县| 木里| 高州市| 临泉县| 贺兰县| 三江| 安福县| 桐梓县| 靖州| 东阿县| 宁河县| 临桂县| 扎囊县| 高清| 永济市| 阳信县| 黄平县| 永登县| 仲巴县| 凯里市| 松阳县| 顺昌县| 南召县| 丹江口市| 临泽县| 维西| 手游| 卢龙县| 太仆寺旗| 达拉特旗| 华池县| 武宣县| 南阳市| 宁都县| 石狮市| 大方县| 蚌埠市| 富蕴县|