当因这种行为是完全说通过 @flowerysong的答案,还有一个解决方法你可以用它来达到你的要求。
诀窍就是使用事实上,你可以界定两剧里的剧本和,那么,使用的变量集在主(s)的第一次播放。
因此,我们要做的是:
- 运行上玩
localhost
并设置一个事实,来自东道组,我们的目标是到目标在第二玩
- 任选:我也定义了
target_hosts
变量,在第一起,以确保,如果第二玩的目标是修改,主组变量始终是一个第二起是要目标。
- 使用这个变量在第二播放,从
localhost
's hostvars
额外注意:没有核算的味道,但是我喜欢的 内联如果 到 ternary
滤波器,仍然,他们都是严格等同的。
因此,鉴于指南:
- hosts: localhost
gather_facts: no
tasks:
- set_fact:
target_hosts: nodes
- set_fact:
host_set: "{{ hostvars[groups[target_hosts].0].host_set }}"
- hosts: "{{ hostvars.localhost.target_hosts }}"
gather_facts: no
serial: "{{ '100%' if hostvars.localhost.host_set else '1' }}"
tasks:
- debug:
- 时运行
host_set: true
在 nodes
小组:
PLAY [localhost] *******************************************************************************************************************
TASK [set_fact] ********************************************************************************************************************
ok: [localhost]
TASK [set_fact] ********************************************************************************************************************
ok: [localhost]
PLAY [nodes] ***********************************************************************************************************************
TASK [debug] ***********************************************************************************************************************
ok: [node1] =>
msg: Hello world!
ok: [node2] =>
msg: Hello world!
ok: [node3] =>
msg: Hello world!
PLAY RECAP *************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node3 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- 时运行
host_set: false
在 nodes
小组:
PLAY [localhost] *******************************************************************************************************************
TASK [set_fact] ********************************************************************************************************************
ok: [localhost]
TASK [set_fact] ********************************************************************************************************************
ok: [localhost]
PLAY [nodes] ***********************************************************************************************************************
TASK [debug] ***********************************************************************************************************************
ok: [node1] =>
msg: Hello world!
PLAY [nodes] ***********************************************************************************************************************
TASK [debug] ***********************************************************************************************************************
ok: [node2] =>
msg: Hello world!
PLAY [nodes] ***********************************************************************************************************************
TASK [debug] ***********************************************************************************************************************
ok: [node3] =>
msg: Hello world!
PLAY RECAP *************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node3 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0