2

设置

我有几个角色声明角色依赖关系,有时使用他们所依赖的角色的处理程序。我的设置的简化版本如下所示(这是输出,head inventory **/*.yml它显示了所有路径名和文件的完整内容):

==> inventory <==
[app]
server1 ansible_host=192.168.2.113
[db]
server2 ansible_host=192.168.2.153

==> playbook.yml <==
- hosts: all
  roles:
    - { role: app, when: "inventory_hostname in groups['app']" }
    - { role: db,  when: "inventory_hostname in groups['db']"  }

==> roles/app/handlers/main.yml <==
- name: app handler
  command: echo app handler

==> roles/app/meta/main.yml <==
dependencies: [base]

==> roles/app/tasks/main.yml <==
- command: /bin/true
  notify: [app handler, base handler]

==> roles/base/handlers/main.yml <==
- name: base handler
  command: echo base handler

==> roles/base/tasks/main.yml <==
- command: /bin/true

==> roles/db/handlers/main.yml <==
- name: db handler
  command: echo db handler

==> roles/db/meta/main.yml <==
dependencies: [base]

==> roles/db/tasks/main.yml <==
- command: /bin/true
  notify: [db handler, base handler]

现在我运行ansible-playbook -i inventory playbook.yml结果

PLAY [all] **********************************************************************

TASK [Gathering Facts] **********************************************************
ok: [server1]
ok: [server2]

TASK [base : command] ***********************************************************
skipping: [server2]
changed: [server1]

TASK [app : command] ************************************************************
skipping: [server2]
changed: [server1]

TASK [base : command] ***********************************************************
changed: [server2]

TASK [db : command] *************************************************************
skipping: [server1]
changed: [server2]

RUNNING HANDLER [base : base handler] *******************************************
skipping: [server2]
changed: [server1]

RUNNING HANDLER [app : app handler] *********************************************
changed: [server1]

RUNNING HANDLER [db : db handler] ***********************************************
changed: [server2]

PLAY RECAP **********************************************************************
server1                    : ok=5    changed=4    unreachable=0    failed=0   
server2                    : ok=4    changed=3    unreachable=0    failed=0   

问题

我的问题是我希望两台服务器都执行基本处理程序。但显然它在 server2 上被跳过了。如果我添加-v到 ansible 命令,我会得到无用的评论,即skipping: [server2] => {"changed": false, "skip_reason": "Conditional result was False"}.

让我感到困惑的是,基本角色似乎被包含了两次,并且每个服务器分别跳过了一个或另一个包含该角色的内容。

问题

  • 有什么方法可以确保处理程序被正确触发?

编辑

  • 处理程序没有被触发是一个错误,还是该行为记录在某处?

/编辑

小问题

边注

我想避免使用文档group_by中描述的技术,或者更一般地说,在他们自己的剧本中只为正确的服务器子集执行每个角色,因为我有很多服务器,我想通过以下方式加快剧本的运行使用.strategy: free

4

0 回答 0