Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.