Using post-change commands in puppet manifests

21 June 2006

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"]
}

Creative Commons License Watzmann.Blog by David Lutterkort is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.

Generated with Jekyll