How to set up a Handler for a Notification or Subscription in a Defined Type?
How do I add handler to a defined type in puppet? For example, if I have:
define foo::bar ($baz) {
...
}
How can I have handler in foo::bar
to handle something that contains
...
notify => Foo::Bar['zippidy']
...
?
The handler would then run various Execs inside conditional logic when it receives a notification.
You can notify a defined resource that you've declared elsewhere in the catalog. How about an example?
CentOS system, httpd installed and stopped. Tested with Puppet 2.7.18
$ service httpd status
httpd is stopped
Here's an example manifest that contains an exec resource inside a defined resource type, a declaration of that defined resource type and a service resource that notifies that defined resource type.
./notify_defined_types.pp
define foo(){
exec { "${name}_exec":
command => "echo hello ${name}",
path => '/bin:/usr/bin',
refreshonly => true,
logoutput => true,
}
}
foo { 'bar': }
service { 'httpd':
ensure => running,
notify => Foo['bar'],
}
When I apply this, the change of state in my httpd service resource triggers a notification to the Foo['bar'] resource. This notification will apply to any service or exec resources used inside the foo defined resource type.
$ puppet apply notify_defined_types.pp
notice: /Stage[main]//Service[httpd]/ensure: ensure changed 'stopped' to 'running'
notice: /Stage[main]//Foo[bar]/Exec[bar_exec]/returns: hello bar
notice: /Stage[main]//Foo[bar]/Exec[bar_exec]: Triggered 'refresh' from 1 events
notice: Finished catalog run in 0.51 seconds
$ puppet apply notify_defined_types.pp
notice: Finished catalog run in 0.38 seconds
Make sense? You just simply notify the resource that you declared. It'll trigger any exec or service resources exposed inside the defined resource type.