On Wednesday, August 14, 2013 8:23:47 AM UTC-5, [email protected]
wrote:
>
> Hi,
>
> I wanted to know if there is an easy way to replace a file in puppet
> (using the file directive) but only when a certain string exists.
>
> My use case:
> I have a package that installs a config file, I want to replace that file
> with puppet, but I in normal operation the application may also write to
> that file.
> So a simple replace => true is not sufficient.
>
> I tried with something like this:
> define configfile ($source) {
>
> $grepcommand = '/bin/grep --quiet "string to check"'
> exec { "check_${name}":
> command => "/bin/true",
> onlyif => "${grepcommand} ${name}",
> }
>
> file { "$name":
> ensure => "present",
> replace => "true",
> mode => "0644",
> owner => 'user',
> group => 'user',
> source => $source,
> require => Exec["check_${name}"]
> }
>
> }
>
> configfile {"/etc/software/config.xml":
> source => "puppet:///modules/software/config.xml"
> }
>
> I can't get that to work properly, the file is always getting replaced.
>
>
I think it would work if you rewrote your Exec like so:
exec { "check_${name}":
command => 'grep -q <disqualifying-pattern> ${name}'
}
The point is that for the Exec to condition application of the File, its
application must *fail* when you want to avoid applying the File. When an
Exec's 'onlyif' or 'unless' parameter causes the 'command' to be skipped,
that counts as the Exec succeeding. It should be viewed as finding the
Exec resource already in sync with the target node, so that the command
does not need to be run.
Nevertheless, this is all the wrong approach. Better alternatives include
- create per-file-of-interest custom facts to communicate the grep
results to Puppet, and use them in Puppet conditional statements to control
whether the target File resources are declared at all.
- if really all that matters is that the file contain a particular line,
then manage just that line. PuppetLabs' add-in "stdlib" module provides a
File_line resource serving exactly that purpose.
- if the objective is to install a default file, but only if the target
does not already exist, then set replace => false on your File resource
- flip around the File and Exec: sync the config to a temp file, and use
an Exec to conditionally update the true target with the temp version. The
main advantage here is that Puppet will not report failed resources in the
course of normal operations.
John
--
You received this message because you are subscribed to the Google Groups
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/puppet-users.
For more options, visit https://groups.google.com/groups/opt_out.