8

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.

7

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.

  • 2
    How do you do the inverse? Let's say I want the service to subscribe to Foo['bar']? Thanks – José F. Romaniello Jan 31 '15 at 17:56

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.