I want to run a puppet exec-resource based on a file update. Something like:

File { '/tmp/foo.bar':
    audit => content,
} ~>
exec { 'deployment':
    command => 'do_something_meaningful.sh',

But this gets executed on every puppet-apply-run. Even if the file has not changed. So I tried the exec-attribute refreshonly. But with this, the exec gets not executed on the first puppet-apply. I think this is because there is yet no md5-checksum at the state.yml. It works great from the second run.

My current workaround is something like this:

File { '/tmp/foo.bar':
    audit => content,

exec { 'deployment_on_change':
    command     => 'do_something_meaningful.sh',
    refreshonly => true,
    subscribe   => File['/tmp/foo.bar']

exec {'deployment_on_first_run':
    command   => 'do_something_meaningful.sh',
    onlyif    => 'test ! -f marker.file',
    subscribe   => File['/tmp/foo.bar']
} ->
file { 'marker.file':
    ensure => present

Is there a more elegant way to solve this problem?

  • I cannot reproduce this fully, with refreshonly => true it also gets applied on the first run for me (and second but then not anymore) – faker Oct 20 '16 at 13:50

You're forcing Puppet into something that it's not designed for. By executing you would only prolong each Puppet run. And you don't want that.

Simply use a cron job, that will check if something has changed.

  ensure  => present,
  owner   => 'myuser',
  mode    => '0755',
  source  => 'puppet:///modules/my_module/do_something_meaningful.sh',

cron { 'do_something_meaningful':
  command => '/usr/bin/local/do_something_meaningful',
  user    => 'myuser',
  hour    => ['8-17'],
  minute  => '*/30',
  require => File['/usr/bin/local/do_something_meaningful'],

If you're deploying code using some other tool, you should hook do_something_meaningful.sh into the deployment process. Puppet is good for ensuring that some resource (files, packages, services, etc.) exists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.