Online Book Reader

Home Category

Pulling Strings With Puppet - James Turnbull [21]

By Root 356 0
instance on a node, like a package or a service.

This reuse is also the key difference between classes and definitions. Classes contain single instances of resources; for example, a class could contain a package resource that defined the httpd package. This package will only exist once on a node and hence would be installed, removed, or managed using a class. But some configuration exists multiple times on your nodes, for example, the httpd server may have multiple virtual hosts defined. You would then create a definition to configure virtual hosts and pass in appropriate arguments to configure each. As long as each set of arguments was different, Puppet would configure the new virtual host every time the definition was evaluated.

Tip - Don't underestimate the power of definitions! They are highly flexible, powerful, and effective and often represent the best method for defining configuration on your nodes. I recommend examining the Puppet recipes at http: //reductivelabs. com/trac/puppet/wiki/PuppetRecipes and http: //reductivelabs. com/trac/puppet/wiki/CompleteConfiguration to see how definitions are and can be used.

A definition is created by using the define keyword, specifying a title for the definition, and then listing any arguments in brackets. The definition itself is specified next and is enclosed in curly braces. In Listing 3-7, I've demonstrated a definition that runs a script to configure a new virtual host.

In Listing 3-7, we've created a definition called newip that has an argument of a variable called $ip. Inside the definition we've used the exec resource type that executes an external binary, in this case the ifconfig command. We've specified the variable $ip and used another variable, $title, that contains the resource title.

Note - The $title variable is available in all definitions and contains the title of the resource.

On the next lines we have actually called the newip definition. It is called much like we define a resource type. We've specified the name of the definition being called, the title, in this case etho (which is also the value of the $title variable). We then specify the remaining variable in the same format as we would specify attributes in a resource.

If we then use this definition, we'll see a log message on the client much like this one:

We can see that the definition has executed the exec resource and the appropriate arguments have been passed into the definition to reconfigure our interface with the new IP address.

We can also specify defaults for each of the arguments passed to our definition like so:

Here we've created the config_file definition. We specify a series of default values for some of the arguments we intend to pass to the definition. We then specify a file resource that will accept the arguments. We then invoke the definition for a sample file, /etc/vnc.conf. We specify the source attribute and then we override one of the default arguments we specified, mode, for the definition.

Qualifying Definitions

Qualified definitions allow you to perform a number of useful functions. First, you can refer to definitions that are defined in classes:

We've defined a class called virtuals and placed the definition we created in Listing 37 into it. We've then used the newip definition and referenced it by qualifying its location. The qualification is done by specifying the name of the class the definition is in and the name of the definition, and separating each with two colons, ::. The definition is then used normally.

We can also use this syntax to set defaults for a definition.

This would set the noop metaparameter for all resources contained in the virtuals: : newip definition. The noop metaparameter tells Puppet whether an operation should occur or not in the same manner as the - - noop command-line option.

Lastly, we can use definition qualification to specify dependencies.

On the previous line, we've defined a file resource with the notify metaparameter. The notify metaparameter is a trigger; if the file /etc/hosts.conf changes, the network:

Return Main Page Previous Page Next Page

®Online Book Reader