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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With