A neat feature of Puppet's file resource is that you can specify multiple values for the source parameter. Puppet will search them in order. If the first source isn't found, it moves on to the next, and so on. You can use this to specify a default substitute if the particular file isn't present, or even a series of increasingly generic substitutes.
How to do it...
This example demonstrates using multiple file sources:
Create the file modules/greeting/files/hello.txt with the following contents:
Hello, world.
Create the file modules/greeting/files/universal.txt with the following contents:
Bah-weep-Graaaaagnah wheep ni ni bong
Add the class to a node:
node cookbook {
class {'greeting': }
}
Run Puppet:
[root@cookbook ~]# puppet agent -tInfo: Caching catalog for cookbook.example.comInfo: Applying configuration version '1413784347'Notice: /Stage[main]/Greeting/File[/tmp/greeting]/ensure: defined content as '{md5}54098b367d2e87b078671fad4afb9dbb'Notice: Finished catalog run in 0.43 seconds
The file hello.txt is first in the list, and is present, so Puppet uses that as the source for /tmp/greeting:
Hello, world.
On the second Puppet run, hello.txt is missing, so Puppet goes on to look for the next file, universal.txt. This is present, so it becomes the source for /tmp/greeting:
Bah-weep-Graaaaagnah wheep ni ni bong
There's more...
You can use this trick anywhere you have a file resource. A common example is a service that is deployed on all nodes, such as rsyslog. The rsyslog configuration is the same on every host except for the rsyslog server. Create an rsyslog class with a file resource for the rsyslog configuration file:
Then, you put the default configuration in rsyslog.conf. For your rsyslog server, logger, create an rsyslog.conf.logger file. On the machine logger, rsyslog.conf.logger will be used before rsyslog.conf because it is listed first in the array of sources.
See also
The Passing parameters to classes recipe in Chapter 3, Writing Better Manifests