Making quick edits to config files
When you need to have Puppet change a particular setting in a config file, it's common to simply deploy the whole file with Puppet. This isn't always possible, though; especially if it's a file which several different parts of your Puppet manifest may need to modify.
What would be useful is a simple recipe to add a line to a config file if it's not already present, for example, adding a module name to /etc/modules
to tell the kernel to load that module at boot. You can use an exec
resource to do jobs like this.
How to do it…
This example shows how to use exec
to append a line to a text file:
Add the following code to your
manifests/site.pp
file:define append_if_no_such_line($file,$line) { exec { "/bin/echo '${line}' >> '${file}'": unless => "/bin/grep -Fx '${line}' '${file}'", } }
Modify your
manifests/nodes.pp
file as follows:node 'cookbook' { append_if_no_such_line { 'enable-ip-conntrack': file => '/etc/modules', line => 'ip_conntrack...