Sometimes it is necessary in a puppet manifest to run a command with exec every time puppet changes a file. The subscribe metaparameter is ideal for this purpose, since it causes the dependent element to be executed whenever the referenced element changes — with the small caveat that the dependent element will also be executed when it sees a need to run by itself. In the case of exec that means always, unless execution is suppressed with onlyif. Therefore, to run a command every time a file changes, but not when that file hasn't been changed, the manifest must contain something like:
file { "/tmp/foo.txt":
source => "/tmp/bar.txt"
}
exec { subscribe-echo:
command => "/bin/echo subscribe triggered",
logoutput => true,
onlyif => "/bin/false",
subscribe => file["/tmp/foo.txt"]
}
exec type has a refreshonly attribute for excactly this purpose, so that the exec above can also be written as
exec { subscribe-echo:
command => "/bin/echo subscribe triggered",
logoutput => true,
refreshonly => true,
subscribe => file["/tmp/foo.txt"]
}
Comments are closed for this post.