Pulling Strings With Puppet - James Turnbull [18]
The type and title of a resource combine to uniquely identify that resource to Puppet. This is very important because Puppet practices configuration normalization and hence only allows you to manage a resource in one place. In Listing 3-1, we've defined a resource that manages /etc/passwd. If we defined another resource that also managed /etc/passwd, the Puppet client would return a parse error like so (the server also returns a similar error):
err: Could not retrieve configuration: Duplicate definition: File[/etc/passwd] r► is already defined in file /etc/puppet/manifests/site.pp at line 6; cannot r► redefine at /etc/puppet/manifests/site.pp:12
This control ensures that your configuration does not overlap or duplicate anywhere.
Note - But this seems to create a problem-how do you define a resource that needs to be different on different platforms? What if a file needs different permissions on different platforms? Well, Puppet has an answer to this, and you'll see how that is achieved later in this chapter when we look at conditional statements.
But this error message also reveals something interesting. In the error message the resource is referred to as File[/etc/passwd]. What does this mean? In Puppet, the combination of the capitalized resource type and the title of the resource is a unique identifier for that resource. Let's look at this in Listing 3-2.
In Listing 3-2, we've defined another file resource, /etc/group. We've also specified a metaparameter, require. The require metaparameter allows you to specify objects that the resource depends on. Here the resource File[ '/etc/passwd" ] must exist in order to configure /etc/group. Puppet knows we're citing another resource because the resource type has been capitalized, and the title of the resource has been specified in square brackets. This unique identifier tells Puppet precisely which resource is being referenced.
At the start of this section I mentioned that the title in Listing 3-1, /etc/passwd, represented the literal name of the configuration item being managed. You don't always have to use the literal name though. To make it easier to refer to resources, we can specify a symbolic name for our resource as you can see on the following lines:
On the previous lines we've done two new things. First, we've not used the literal name as the title but rather specified a symbolic name, group. Second, we've specified an attribute called name. The name attribute is common to a number of resource types; for example, both the file and service types can use it. It allows us to specify the literal name, path, or other identifying information of a configuration item. We can then title the resource symbolically to make it easier to reference.
It is important to remember that Puppet only knows what you tell it about resources. If you define a symbolic name, you should always refer to resources by that symbolic name. If you use both literal and symbolic names, Puppet may assume you are managing different resources, for example:
The title of the first resource is passwd and the second resource /etc/passwd, but they both manage the file /etc/passwd. But from Puppet's point of view, these are two different resources as they have different titles.
Resource Attributes
After the title, the attributes of the resource are specified. The attributes tell Puppet how to configure resources. We've already seen a few attributes, and each resource type has its own set of attributes; for example, the owner, group, and mode attributes we saw in Listing 31 all belong to the file resource type.
Earlier I also mentioned some special attributes, called metaparameters, which can be applied to all types of resources. The require attribute we used in Listing 3-2 is such a metaparameter: it allows you to specify a resource that the resource depends on. You can see a full list of metaparameters at http://reductivelabs.com/trac/puppet/wiki/TypeReference#metaparameters.
Tip - We'll discuss other metaparameters through this chapter.
In Listing 3-3, you can see how attributes