Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ansible 如何共享处理程序?_Ansible - Fatal编程技术网

Ansible 如何共享处理程序?

Ansible 如何共享处理程序?,ansible,Ansible,说: 由于处理程序也是任务,因此还可以包含“handlers:”部分中的处理程序文件 我所做的,playbook.yml: - hosts: all handlers: - include: handlers.yml # - name: h1 # debug: msg=h1 tasks: - debug: msg=test notify: h1 changed_when: true - name: h1 debug: ms

说:

由于处理程序也是任务,因此还可以包含“handlers:”部分中的处理程序文件

我所做的,
playbook.yml

- hosts: all
  handlers:
    - include: handlers.yml
    # - name: h1
      # debug: msg=h1
  tasks:
    - debug: msg=test
      notify: h1
      changed_when: true
- name: h1
  debug: msg=h1
---

dependencies:
  - myhandlers
handlers.yml

- hosts: all
  handlers:
    - include: handlers.yml
    # - name: h1
      # debug: msg=h1
  tasks:
    - debug: msg=test
      notify: h1
      changed_when: true
- name: h1
  debug: msg=h1
---

dependencies:
  - myhandlers
那么

但当我取消注释这些行时,我看到

$ 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也可以在“handlers”部分中使用,例如,如果您想定义如何重新启动apache,那么您只需对所有剧本执行一次即可。您可以创建一个如下所示的handlers.yml:

---
# this might be in a file like handlers/handlers.yml
- name: restart apache
  service: name=apache state=restarted
在您的主要剧本文件中,只需像这样将其包含在剧本的底部:

handlers:
  - include: handlers/handlers.yml

根据角色的大小,更好的解决方案可能是使用角色。Ansible有

任务进入
roles/mystuff/Tasks/main.yml
roles/somethingelse/Tasks/main.yml
。您可以在角色之间共享处理程序,方法是创建一个仅包含处理程序的角色
roles/myhandlers/handlers/main.yml
,并使这两个角色都依赖于myhandlers角色

roles/mystuff/meta/main.yml
roles/somethingelse/meta/main.yml

- hosts: all
  handlers:
    - include: handlers.yml
    # - name: h1
      # debug: msg=h1
  tasks:
    - debug: msg=test
      notify: h1
      changed_when: true
- name: h1
  debug: msg=h1
---

dependencies:
  - myhandlers
有关版主的依赖关系的更多信息,请参见。请仔细阅读我的问题。这就是我问题的答案。我完全知道,SO不是一个论坛


这在
ansible-2.1
中。这要归功于发现问题的人。

它告诉您可以在任务部分包含一个处理程序文件(因为处理程序文件包含一组任务)。它并没有告诉您可以在handlers部分使用include。include本身是一个(未命名)处理程序,它不会包含任何内容,因为它从未被通知。您的目标是什么?处理程序是全局的。您不需要在角色之间显式共享它们。将角色包含在处理程序中,您可以从任何位置通知他们。请参阅我的更新问题。我们的目标是在我的剧本中不重复处理程序。它有多个剧本。我还没有角色,很有趣。Ansible充满了奇怪的东西。这看起来像是这个bug:我测试过它,它确实在Ansible 1.x中工作过,但在任何2.x版本中都没有。此外,有趣的是,它似乎在devel分支中实际起作用,尽管没有人对它的工作票发表评论。可能是他们在做其他事情时修复了它。我注意到的另一点是,即使在调试任务中更改了
,当:true
时,任务也没有标记为
已更改
,只有
正常
。同样,这只发生在任何2.x版本中。在1.x中,它被标记为已更改。虽然这不是问题所在。即使在输出中没有标记为已更改,处理程序(不是按包含实现的)也会在2.x中被触发。再一次,Ansible充满了奇怪的东西……:)