问题原文
原帖链接 · Peter K skakee · CC BY-SA 4.0
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!
社区高分解答
原帖链接 · Zoredache · CC BY-SA 4.0
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.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
