我有多个角色,它们声明了角色依赖关系,有时还使用它们所依赖角色的处理程序。我的安装程序的简化版本如下所示(这是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"}
的无用注释。
让我感到困惑的是,基本角色似乎被两次包含,而每个服务器分别跳过了一个或另一个角色。
编辑
/ EDIT
我想避免使用the docs中描述的group_by
技术,或者更一般地说,仅对服务器的正确子集执行其自己的剧本中的每个角色,因为我有很多服务器,并且我想通过使用strategy: free
来加快剧本的运行速度。