Atunci când motivul acestui comportament este foarte bine precizat de @flowerysong răspunsul lui, nu este o soluție pe care o puteți folosi pentru a obține dvs. cerință.
Trucul este folosind faptul că puteți defini două piese în interiorul-o carte și, apoi, utilizați set de variabile pe gazdă(s) de la primul play.
Deci, ce am de gând să faceți este:
- Rula un joc de
localhost
și a stabilit un fapt vine de la gazdă grupul pe care dorim să-țintă în cel de-al doilea joc
- Opțional: am, de asemenea, definit un
target_hosts
variabilă în primul joc, pentru a fi sigur că, dacă cel de-al doilea play-țintă este modificat, gazda grup variabilă este întotdeauna cel de-al doilea joc este de a merge la țintă.
- Utilizarea ca variabilă în cel de-al doilea joc, de
localhost
modelului hostvars
Notă suplimentară: nu există nici o contabilitate pentru gusturile, dar eu prefer inline dacă la ternary
filtru, încă, acestea sunt strict echivalente.
Deci, având în vedere schemele:
- 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:
- Atunci când a alerga cu
host_set: true
în nodes
grup:
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
- Atunci când a alerga cu
host_set: false
în nodes
grup:
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