Pulling Strings With Puppet - James Turnbull [19]
Listing 3-3. Attribute and Value Structure
Tip - Each attribute and value pair should end with a comma. The last attribute should end in a comma or a semicolon.
Puppet has some native attribute values, for example, setting an attribute to true or false:
Values can also be user-supplied; for example, the owner attribute would be set to the name of the user who owns a file such as "root". User-supplied attribute values should always be in double quotes.
Note - Another reason to always quote resource titles and the values of user-supplied attributes is that Puppet has a number of reserved words that are used as part of the syntax, such as true, false, class, define, inherit, and so on. If you need to use one of these words in a title or attribute value, you must quote them.
Resource Style
When a resource has only one attribute, it can be declared on a single line as you can see here:
You can see that we've also dropped the trailing comma from the attribute. This style of declaration can be a useful shorthand but sometimes makes it hard to read resource definitions. For this reason, many people use the multiline structure for all resources.
Multiple resources can be configured using a single resource type as you can see in Listing 3-4.
In Listing 3-4, we've used the file resource type and specified two files. These are two separate resources, and we would refer to them in other Puppet resources as follows:
Lastly, Puppet resources support the use of sh-style comments. Comments prefixed by the # character can be placed on empty lines or at the end of lines.
Resource Defaults
You can also set default attribute values that will apply to all resources of that type. This means that you don't need to specify a particular attribute each time you use the resource. Let's look at an example of a default, using the exec resource type that allows you to execute external scripts or programs, on the following line:
In the exec resource type, the path attribute is used to specify the search path Puppet uses to find these external scripts. It is an attribute that is frequently specified and is hence a good candidate to be set as a default. We can see that it is a default because the resource type has been capitalized. A resource title does not need to be specified as we're not defining the resource, only the attribute or attributes for which you wish to set a default.
The default can be overridden in specific resources by specifying the attribute in the resource, for example:
On the previous line, we would override the resource default for the path attribute for this particular resource.
Collections of Resources
Resources can configure the characteristics of single configuration items on nodes, but most services and applications are made up of multiple resources. For example, a web server consists of the software package, users to run the software, and a variety of configuration, logging, and other files.
Additionally, individual resources configured in Puppet are applied to all Puppet clients that connect to the master. You can't select particular resources and apply them to specific nodes. So, how do we group together resources and apply resources to the appropriate nodes?
Puppet does this by using resource collections. Resource collections allow you to gather together resources, assign them to a collection, and then have that collection applied to one or more nodes. There are two types of resource collections: classes and definitions.
Note - Even inside collections, resources must be unique. You still can't manage the same resource more than once.
A class is a collection of resources that represents a single configuration item on your node, for example, the SSH service or the NFS package. Classes are only evaluated once per node because the configuration item being managed should only exist once.
A definition is similar but represents a collection of configuration items that have multiple representations on