On Feb 20, 5:05 pm, James <[email protected]> wrote:
> Dear Puppet,
>
> I wish to run an Exec when a puppet define is removed.
It is always a bad idea for Puppet manifests to try to employ
introspective logic, basing decisions or actions on a dynamic
determination of whether a certain class or resource is declared for
the target node. The approach you attempt to take is more benign than
many, being mediated as it is by the target filesystem, but it still
has a bad smell.
> The define puts
> a file in /tmp/foobar/${name}. When the define is no longer there, the
> purge does an ensure => removed, and the file gets deleted. I've also
> been able to make this work with Tidy[] instead of purge.
>
> eg:
> file { '/tmp/foobar/':
> ensure => directory, # make sure this is a directory
> recurse => true, # recursively manage directory
> purge => true, # purge all unmanaged
> files
> force => true, # also purge subdirs and
> links
> owner => root, group => nobody, mode => 600, backup => false,
> }
>
> The problem:
> including a notify => Exec['whatever'] does not work. Either this is a
> bug, and/or perhaps someone could suggest a workaround.
According to me, if that File resource were set to notify your Exec
(or if the Exec subscribed to the File), then the Exec should be
signaled if the File purges any files. So, given
exec { 'example':
command => '/bin/echo purged files >> /tmp/purges',
refreshonly => true,
}
file { '/tmp/foobar/':
ensure => directory,
recurse => true,
purge => true,
force => true,
notify => Exec['example']
}
you should see a new line added to file /tmp/purges on every run in
which Puppet purges any files from /tmp/foobar/ (or when /tmp/foobar/
is modified in any other way, but that's irrelevant). If you do not
see that then I'd call it a bug. In fact, it could be a manifestation
of http://projects.puppetlabs.com/issues/5414, though the description
and discussion seem to indicate otherwise.
Now back to code smell: what if a file were dropped into /tmp/foobar/
by some means other than Puppet? Would you still want your Exec to
run when that file was purged? If so, then your code isn't smelly,
but your initial problem description was inaccurate.
On the other hand, if you really want the Exec to run only when a
previously managed file is removed, then your whole approach is
flawed. Instead of removing files from management, thereby allowing
them to be purged, you should actively manage them absent. That could
look something like this:
exec { 'example':
command => '/bin/echo removed files >> /tmp/removals',
refreshonly => true,
}
define foobar_file($ensure => 'present') {
case $ensure {
'present': {
file { "/tmp/foobar/${name}": ensure => 'present' }
}
'absent': {
file { "/tmp/foobar/${name}":
ensure => 'absent',
notify => Exec['example']
}
}
default: { fail("invalid ensure parameter: '${ensure}'") }
}
}
node default {
foobar_file { "wanted": }
foobar_file { "unwanted": ensure => 'absent' }
}
John
--
You received this message because you are subscribed to the Google Groups
"Puppet Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/puppet-users?hl=en.