Online Book Reader

Home Category

Pulling Strings With Puppet - James Turnbull [23]

By Root 360 0
$master: : server. This combines the names of the class and variable being referenced, separated by two colons, ::. The resulting value of the $ms variable in the slave class would be "primary".

We can also refer to the top scope of our manifests (i.e., outside of any nodes, classes, or definitions) by using the following syntax:

This syntax will result in the $ms variable being assigned the value of a variable called $server that is set in the top scope.

Caution - Qualified variables are dependent on the order in which Puppet evaluates your configuration. All resources of the same type are evaluated in order of being parsed, in a top-down manner in your manifest files. For example, each class is evaluated in order from top to bottom. This means you can only qualify a variable that has been defined earlier in the manifest. If you try to evaluate a variable defined later in the manifest, a null value will be set.

Variables and Metaparameters

Using variable syntax, you can also set metaparameter defaults for all resources in a class, for example:

In the start_vhost class, we've specified the noop metaparameter as a variable and set it to true. In all the resources in the class, in this case the two exec resources, the noop metaparameter will be added and set to true.

Arrays

In addition to variables, Puppet also allows you to define arrays. In Listing 3-5, we defined an array for the $packagelist variable that contained three packages name: httpd, openssl, and mod_ssl; you can see a subset of that class here:

Caution - Don't quote titles, in this case $packagelist, that contain arrays. The array will become conflated.

When specified in the resource title of the package resource type (which manages package installation and removal on nodes), the array is expanded. The package resource attribute, ensure, then checks whether all three packages in the array are installed. Anyone familiar with Ruby will recognize Puppet's array syntax: each element in the array is quoted, separated by commas, and enclosed in square brackets.

Arrays can be used in two principal ways: as the value of a variable as we've done in Listing 3-5 or as the value for a number of resource attributes. For example, the user resource type allows multiple groups to be assigned to a user using an array:

Here you can see that the value of the groups attribute is an array containing three group names.

We could also use an array to call a definition multiple times.

Here we've created a definition called ruby:: libs that has no arguments but will install any package prefixed with ruby- and the value of the $name variable. We then call the definition and use an array as the value of the resource title. The array would be expanded and each array element assigned to the variable $name. The definition would then install each Ruby library package (e.g., ruby-ldap, ruby-mysgl, ruby-postgres, etc.).

Conditionals

Puppet also supports conditionals in resources, classes, definitions, and nodes. They are expressed in the form of a selector with a default option. In Listing 3-11, we can see a conditional selector inside a resource.

The conditional selector is inside the apache service resource. It is constructed using the ? symbol and followed by a list of selectors and values, each separated by the => symbols much like attributes. Finally, the conditional is enclosed in curly braces.

You can see we've also specified a value called default. This default value will be used if none of the other conditions are met.

In Listing 3-11, we've also used the name attribute. In addition to allowing you to create a symbolic name, as we discussed earlier in this chapter, the name attribute in combination with a conditional also allows you to take into consideration that different target nodes might name a resource differently. Here we are using the value of the $operatingsystem fact to select what name to use for the service. For example, if the $operatingsystem fact on a node returned a value of redhat, the service name would be set to httpd. When

Return Main Page Previous Page Next Page

®Online Book Reader