Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible notify handlers in another role

Can I notify the handler in another role? What should I do to make ansible find it?

The use case is, e.g. I want to configure some service and then restart it if changed. Different OS have probably different files to edit and even the file format can be different. So I would like to put them into different roles (because the file format can be different, it can't be done by setting group_vars). But the way to restart the service is the same, using service module; so I'd like to put the handler to common role.

Is anyway to achieve this? Thanks.

like image 222
Kan Li Avatar asked Mar 26 '14 00:03

Kan Li


People also ask

Can an Ansible handler notify another handler?

Notifying handlers Handlers are executed in the order they are defined in the handlers section, not in the order listed in the notify statement. Notifying the same handler multiple times will result in executing the handler only once regardless of how many tasks notify it.

How do you use handlers in Ansible roles?

In a nutshell, handlers are special tasks that only get executed when triggered via the notify directive. Handlers are executed at the end of the play, once all tasks are finished. In Ansible, handlers are typically used to start, reload, restart, and stop services.

How do you notify in Ansible?

In the example playbook above, we start by installing the apache2 server using the package module. We then use a notify module to set a notify action. The final step is to configure a handler to run after the server is installed. The name of the notification should be the same as the name used in the handler module.


1 Answers

You can also call handlers of a dependency role. May be cleaner than including files or explicitly listing roles in a playbook just for the purpose of role to role relationship. E.g.:

  • roles/my-handlers/handlers/main.yml

    --- - name: nginx restart   service: >     name=nginx     state=restarted 
  • roles/my-other/meta/main.yml

    --- dependencies: - role: my-handlers 
  • roles/my-other/tasks/main.yml

    --- - copy: >     src=nginx.conf     dest=/etc/nginx/   notify: nginx restart 
like image 86
famousgarkin Avatar answered Oct 09 '22 21:10

famousgarkin