Online Book Reader

Home Category

Pulling Strings With Puppet - James Turnbull [24]

By Root 351 0
configuring this resource on that node, this is the name Puppet would use for the service.

In Listing 3-12, we have set the value of a variable using a conditional selector.

We've specified that the value of the $environment variable will depend on what is returned using the domain fact, which returns the domain name of the node. In Listing 3-12, if Facter returns testing. com as the value of the domain fact, it should set the $environment variable to test and so on. The last value, default, is the default value to specify for the $environment variable in the event the fact does not return one of the other values.

Tip -► Conditionals can be nested. You can put conditional structures inside other conditional structures.

If you don't set a default value and no other value is matched, a parse error is returned by Puppet.

Note -► Puppet selectors are case insensitive when matching. You'll also notice that we've quoted the values being tested and the result. This is good practice to ensure the values are interpreted and returned correctly.

There are also two other conditional statements we can use in our manifests: case and if. The case statement allows you to conditionally select larger stanzas of configuration such as resources or classes depending on a value. You can see a case statement in Listing 3-13.

The case statement in Listing 3-13 uses the operatingsystem fact we introduced earlier in this chapter to return the operating system of the node the configuration is being evaluated on. Each potential value is specified in curly braces and terminated with a colon, for example, redhat:.

Tip -* You should enclose some values in double quotes to ensure they are parsed correctly.

For each value, we can then specify a resource that should be defined if that particular operating system is returned. Lastly, we've specified a default value that tells the case statement what resource to apply if the fact doesn't return any of the previously specified values. As with selectors, if you do not specify a default and no values are matched by the statement, Puppet will return a parse error.

The case statement can also test for the presence of a variable, for example:

Here if the $definedvariable is present, or true, a class is included. Otherwise, the default action is to do nothing.

Note -* Like selectors, case statements perform case-insensitive matching.

The last conditional statement, if, is a very simple if/else statement. The statement only supports Boolean operations (true or false) and Puppet is not currently capable of using comparison operators (such as greater than, less than, not equal, etc.). You can see an if statement in Listing 3-14.

Listing 3-14 is a simple Boolean evaluation: If the variable $server is defined, evaluate the first file resource and ensure that the /etc/server.conf file is present. If it is not defined, evaluate the second file resource, which ensures that the /etc/client.conf file is present.

Creating Nodes

We've looked at defining resources and collections of resources in the form of classes and definitions. So how do we assign these resources and collections to particular clients? Puppet does this by defining each client on the master as a node. Resources and resource collections are then assigned to these nodes.

As mentioned in Chapter 2, when no nodes are defined, all resources not in a specific class or definition are applied to the node; for example, if we had a file type resource defined in our site. pp manifest, that would be applied to the node, but a class would not. With nodes defined, you can define nodes on the master and assign classes and definitions to those nodes.

Note - Ultimately, as Puppet develops, node definitions inside your manifests will disappear, and all nodes will be contained externally in a database or a directory like LDAP. We discuss the initial capability to do this in Chapter 6 when we look at external nodes. This move reflects the growing push for centralized asset and configuration management stores in many organizations.

When the Puppet

Return Main Page Previous Page Next Page

®Online Book Reader