Building config files using snippets
Sometimes you can't deploy a whole config file in one piece, yet making line by line edits isn't enough. Often, you need to build a config file from various bits of configuration managed by different classes. You may run into a situation where local information needs to be imported into the file as well. In this example, we'll build a config file using a local file as well as snippets defined in our manifests.
Getting ready
Although it's possible to create our own system to build files from pieces, we'll use the puppetlabs supported concat
module. We will start by installing the concat
module, in a previous example we installed the module to our local machine. In this example, we'll modify the Puppet server configuration and download the module to the Puppet server.
In your Git repository create an environment.conf
file with the following contents:
modulepath = public:modules manifest = manifests/site.pp
Create the public directory and download the module into that directory as follows:
t@mylaptop ~/puppet $ mkdir public && cd public t@mylaptop ~/puppet/public $ puppet module install puppetlabs-concat --modulepath=. Notice: Preparing to install into /home/thomas/puppet/public ... Notice: Downloading from https://forgeapi.puppetlabs.com ... Notice: Installing -- do not interrupt ... /home/thomas/puppet/public └─┬ puppetlabs-concat (v1.1.1) └── puppetlabs-stdlib (v4.3.2)
Now add the new modules to our Git repository:
t@mylaptop ~/puppet/public $ git add . t@mylaptop ~/puppet/public $ git commit -m "adding concat" [production 50c6fca] adding concat 407 files changed, 20089 insertions(+)
Then push to our Git server:
t@mylaptop ~/puppet/public $ git push origin production
How to do it...
Now that we have the concat
module available on our server, we can create a concat
container resource in our base
module:
concat {'hosts.allow': path => '/etc/hosts.allow', mode => 0644 }
Create a concat::fragment
module for the header of the new file:
concat::fragment {'hosts.allow header': target => 'hosts.allow', content => "# File managed by puppet\n", order => '01' }
Create a concat::fragment
that includes a local file:
concat::fragment {'hosts.allow local': target => 'hosts.allow', source => '/etc/hosts.allow.local', order => '10', }
Create a concat::fragment
module that will go at the end of the file:
concat::fragment {'hosts.allow tftp': target => 'hosts.allow', content => "in.ftpd: .example.com\n", order => '50', }
On the node, create /etc/hosts.allow.local
with the following contents:
in.tftpd: .example.com
Run Puppet to have the file created:
[root@cookbook ~]# puppet agent -t Info: Caching catalog for cookbook.example.com Info: Applying configuration version '1412138600' Notice: /Stage[main]/Base/Concat[hosts.allow]/File[hosts.allow]/ensure: defined content as '{md5}b151c8bbc32c505f1c4a98b487f7d249' Notice: Finished catalog run in 0.29 seconds
Verify the contents of the new file as:
[root@cookbook ~]# cat /etc/hosts.allow # File managed by puppet in.tftpd: .example.com in.ftpd: .example.com
How it works...
The concat
resource defines a container that will hold all the subsequent concat::fragment
resources. Each concat::fragment
resource references the concat
resource as the target. Each concat::fragment
also includes an order
attribute. The order
attribute is used to specify the order in which the fragments are added to the final file. Our /etc/hosts.allow
file is built with the header line, the contents of the local file, and finally the in.tftpd
line we defined.