大网数据广告活动
服务器运维问答

Global handlers in Ansible

· 52CE 我爱测

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") an…

问题原文

原帖链接 · 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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-08-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。