问题
I have a pp manifest like this:
vcsrepo { '/home/pi/pop_machine':
ensure => latest,
provider => git,
source => 'https://github.com/kirkins/pop-machine-demo.git',
revision => 'master',
}
exec { 'npm start':
command => "/usr/bin/killall electron & /usr/bin/npm start",
cwd => "/home/pi/pop_machine/",
}
I want the exec resource to restart the device application only if the vcsrepo resource found an update on github and made changes.
Would this be possible with puppet alone, or should I write a bash script to check the last time the .git folder was updated?
回答1:
You can use the metaparameter subscribe and parameter refreshonly with your exec resource to accomplish this.
First, use the subscribe metaparmeter to establish an ordering relationship of the exec on the vcsrepo and to also check for a resource change: https://docs.puppet.com/puppet/latest/metaparameter.html#subscribe
Next, use refreshonly to instruct the exec resource to only apply a change if the vcsrepo repo effected a change (vis a vis non-idempotent): https://docs.puppet.com/puppet/latest/types/exec.html#exec-attribute-refreshonly
It would look like:
vcsrepo { '/home/pi/pop_machine':
ensure => latest,
provider => git,
source => 'https://github.com/kirkins/pop-machine-demo.git',
revision => 'master',
}
exec { 'npm start':
command => "/usr/bin/killall electron & /usr/bin/npm start",
cwd => "/home/pi/pop_machine/",
subscribe => Vcsrepo['/home/pi/pop_machine'],
refreshonly => true,
}
来源:https://stackoverflow.com/questions/45918630/puppet-trigger-resource-only-if-other-resource-applied-a-change