gpt4 book ai didi

ansible - 如何共享处理程序?

转载 作者:行者123 更新时间:2023-12-04 06:56:15 24 4
gpt4 key购买 nike

The docs说:

Since handlers are tasks too, you can also include handler files from the ‘handlers:’ section.


我做什么, playbook.yml :
- hosts: all
handlers:
- include: handlers.yml
# - name: h1
# debug: msg=h1
tasks:
- debug: msg=test
notify: h1
changed_when: true
handlers.yml :
- name: h1
debug: msg=h1
然后,
$ ansible-playbook playbook.yml -i localhost, -k -e ansible_python_interpreter=python2 -v
...
TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": "test"
}

PLAY RECAP *********************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0
...
但是当我取消注释这些行时,我看到
$ ansible-playbook playbook.yml -i localhost, -k -e ansible_python_interpreter=python2 -v
...
TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": "test"
}

RUNNING HANDLER [h1] ***********************************************************
ok: [localhost] => {
"msg": "h1"
}

PLAY RECAP *********************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0
...
我在运行 ansible-2.1.0.0 .
我究竟做错了什么?那是 第一件事我想知道。解决方法是其次。
UPD

Includes can also be used in the ‘handlers’ section, for instance, if you want to define how to restart apache, you only have to do that once for all of your playbooks. You might make a handlers.yml that looks like:

---
# this might be in a file like handlers/handlers.yml
- name: restart apache
service: name=apache state=restarted

And in your main playbook file, just include it like so, at the bottom of a play:

handlers:
- include: handlers/handlers.yml

最佳答案

根据您的游戏规模,更好的解决方案可能是使用角色。 Ansible 有 some discussion why roles are a good idea .

任务进入 roles/mystuff/tasks/main.ymlroles/somethingelse/tasks/main.yml .您可以通过创建仅包含处理程序的角色来在角色之间共享处理程序 roles/myhandlers/handlers/main.yml使两个角色都依赖于 myhandlers 角色 :
roles/mystuff/meta/main.ymlroles/somethingelse/meta/main.yml :

---

dependencies:
- myhandlers

更多关于依赖的信息在 https://docs.ansible.com/ansible/2.5/user_guide/playbooks_reuse_roles.html#role-dependencies

关于ansible - 如何共享处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38188717/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com