Pulling Strings With Puppet - James Turnbull [20]
Classes and Subclasses
Let's start by looking at a class in Listing 3-5.
A class is defined by specifying the class statement followed by the name of the class being defined, which in Listing 3-5 is apache. We then enclose the class in curly braces. Inside the class we can specify a number of resources. The class would then be included in a node's configuration and all resources applied to the target node.
Tip - We'll look at how to configure nodes in the "Creating Nodes" section later in this chapter.
Classes Relationships
Like resources, we can also specify relationships between classes and other resources (as of release 0.23.1 of Puppet). We can see in Listing 3-5 that we've used the require metaparameter to make the httpd service resource dependent on the httpd package resource like so:
Here the httpd service resource requires that the resource Package[ "httpd" ] exist. We can also refer to classes in the same manner using a similar syntax:
This specifies the relationship between a class and a resource. In this instance, the squid service requires that the apache class exist. You'll notice that like resource relationships, we've capitalized the class statement to indicate we're referring to another class and encapsulated the name of the class in block brackets.
Class Inheritance
Classes also have a simple inherit-and-override model using parent classes and subclasses. A subclass can inherit the values of a parent class and potentially override one or more of the values contained in the parent. This allows you to specify a generic class and override specific values in subclasses that are designed to suit some nodes; for example, you could have a generic Red Hat Linux class and override certain values in subclasses for different Red Hat versions. You can see an example of inheritance in Listing 3-6.
Here, class redhat is the parent class and defines a service that controls the mdmdp service. It uses the service resource type to enable the mdmdp service at boot time and specify the service must be stopped. We then specify a new class, a subclass called rhel5, that inherits the redhat class but overrides the ensure attribute and specifies that the mdmdp service must be running for all nodes with the rhe15 subclass included.
In addition to inheritance, we can also use the include function to include the contents of a class in another class like so:
Here the proxy class would include the proxysquidguard class in addition to whatever other configuration you wish to define.
From Puppet version 0.23.1 a new feature is also available that allows you to add values to attributes in subclasses like so:
Here we have defined the proxy class containing the squid service, which in turn requires that the squid package be installed. We have then created a subclass called proxysquidguard that references the squid service but adds an additional package, squidguard, to the require attribute. To do this, we use the +> operator. After this addition, the squid service would now functionally look like this:
We can also unset particular values in subclasses using the undef attribute value.
Here we again have the proxy class with the squid service, which requires the squid package. In the subclass, we have removed the require attribute using the undef attribute value.
Definitions
The second type of Puppet resource collection is a definition. Definitions should be used for configuration items that have multiple instances on a node, for example, virtual machines or multiple instances of a web service. Definitions are created using the define keyword and support arguments but not inheritance. The best way to think about a definition is as a reusable snippet of configuration that you can call with arguments. As they are designed to be reused, they cannot contain any resources that will have only one