How to create a signal file when the file changes

18 views
Skip to first unread message

程伟

unread,
May 16, 2018, 11:12:54 PM5/16/18
to Puppet Users
Hi,everyone! 
I want to create a signal file when some particular file changes. I find a syntax called notify in puppet docs could realize my need. So I tried like this:
file {'target_file':
ensure => 'present',
source => 'puppet:///files/target_file',
path => '/opt/target_file',
notify => File['signal_file'],
}

file {'signal_file':
ensure => 'present',
path => '/opt/signal/signal_file',
subscribe => File['target_file'],
}
However, I tried it many times, and found that even if target_file has existed and not changed, signal_file still created. It's not my purpose. 
Can anyone tell me if I miss something? 

jcbollinger

unread,
May 17, 2018, 9:55:09 AM5/17/18
to Puppet Users
Yes, you're missing something.

When Puppet creates or modifies a resource A, every resource that A notifies and every resource that subscribes to A receives an "event" (either notify or subscribe is sufficient; you don't need both).  A resource that receives an event from any source will attempt to "refresh" some time after it is synced, but a refresh is separate from the preceding sync, and for most resource types it is a no-op.  The most common uses of this feature are in conjunction with Service and Exec resources, which have meaningful refresh behavior.

Other than that, 'notify' has the same semantics as 'before', and 'subscribe' has the same semantics as 'require', so if A fails, those resources it notifies and those that subscribe to it will be skipped.  As long as A succeeds, however, whether that involves any changes or not, those other resources will be applied.  In your particular case, then, you could change your 'notify' to 'before' and your 'subscribe' to 'require' without discernible effect on Puppet's behavior: it will attempt to apply File[signal_file] regardless of whether it receives an event, so long as File[target_file] is successfully synced first.

It looks like you can achieve your objective with an Exec instead:

file { 'target_file':
 
ensure => 'present',
  source
=> 'puppet:///files/target_file',
  path  
=> '/opt/target_file',
}

~> exec { 'create signal file':
  command    
=> '/bin/touch /opt/signal/signal_file',
  refreshonly
=> true,
}

Note that the '~>' operator there has the same effect as either your notify or your subscribe would have, and that the Exec is marked as refreshonly (a feature specific to the Exec resource type) so that its designated command is run only when that resource is refreshed, not when it is synced.


John

程伟

unread,
May 28, 2018, 7:36:30 AM5/28/18
to Puppet Users
Thank you john! 

在 2018年5月17日星期四 UTC+8下午9:55:09,jcbollinger写道:
Reply all
Reply to author
Forward
0 new messages