Online Book Reader

Home Category

Pulling Strings With Puppet - James Turnbull [30]

By Root 366 0
resource, you can also remove files larger than a certain size by using the size attribute like so:

You can specify size by bytes, kilobytes, or megabytes using b, k, and m, respectively, or if you don't specify a type, the resource will default to kilobytes.

If you specify both a size and an age, they are combined together as if with an OR statement. So if you specify a file to be tidied if it is older than 15 minutes or bigger than 1 megabyte, and the file fits one or both criteria, it will be tidied.

You can also back up files before they are tidied and recurse through directories to delete multiple files.

Tip - You can see a full list of types and their attributes at http://reductivelabs.com/trac/puppet/wiki/TypeReference.

Functions

Functions are the last part of Puppet's language we're going to examine. Functions come in two forms: statements, which do not return values, and rvalues, which do return values. You've already met two of these functions in this chapter: the include statement we used in our node definitions to include classes, and the realize function we use to realize virtual resources. Statements are used to perform jobs, like including classes or logging messages to the server, from which we don't expect a response. The rvalue functions return values and can only be used when a resulting value is expected such as a conditional statement or a variable assignment.

Tip - Functions only run on the Puppet master. They are executed when the configuration on the master is parsed and compiled. So when you execute functions, you only have access to the resources on the master plus any Facter facts gathered from your nodes.

You can see a list of the currently available functions in Table 3-3.

(Continued)

The first two functions in Table 3-3, include and realize, we've already looked at earlier in this chapter.

Logging Functions

The functions alert, crit, debug, emerg, error, info, notice, and warning allow you to send messages to the Puppet master server. Each log level is a separate function, so to send a message to the server at the notice level, you would use the notice function like so: notice("This is log message sent at notice log level")

Checking for Existence with defined

The next function, defined, ascertains whether a given resource or class is defined. You could use this to determine whether a class exists before including it, as you can see in Listing 3-26.

In Listing 3-26, if the webservices class is defined, the apache class should be included. If not, the lighttpd class is included.

Note - You'll note we've just specified the name of the class, rather than prefixing it with its proper title, Class[classname]. This is the behavior in version 0.23.2. Later versions refer to classes using the correct title (i.e., if defined(Class[webservices]) { ... }).

You can also use the defined function to check whether a resource is defined:

Here we would perform an action if the sshd service is defined.

Note - Like qualified variables, the defined function relies on the order that your resources are evaluated. If a resource hasn't been evaluated yet, you can't test its definition.

Generating Errors with fail

The fail function forces a parse error in your code and returns an error message to the server. You can make use of it like so:

This will result in an error similar to the following line being generated on the server:

Adding External Data with file

The file function returns the contents of a specified file or files. You can use this function to populate the contents of file type resources as you can see in Listing 3-27.

In Listing 3-27, we have created a new file resource and then set its owner and group. We've used the content attribute of the file resource type to provide the contents of our new file. Inside the content attribute, we've called the file function and populated the file with the contents of the /var/puppet/file/resoly. conf file (which would have to be located on the master server). We can also specify multiple files like so:

Using generate

Return Main Page Previous Page Next Page

®Online Book Reader