Global handlers in Ansible

ansible

Is it possible to have truly global handlers in Ansible so that I can "notify" the handler from any task of any role of any playbook? And without explicitly importing.

I just want to define a handler once (say "restart httpd") and have it available to any "notify" directive anywhere.

Thanks!

Best Answer

You could define your handlers within your play instead of within roles. Handlers defined in a play would be available to all tasks/roles under that play.

You are able to use import if you wanted in the handlers section too.

---
- hosts: all
  handlers:
  - import_tasks: global_handlers.yml
  tasks:
  - shell: echo "Hello World"
    notify: some thing from global_handlers

Note that you need a 'static include' (per https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html#handlers-running-operations-on-change), so import_tasks must be used instead of include_tasks:

You cannot notify a handler that is defined inside of an include. As of Ansible 2.1, this does work, however the include must be static.