Quando la ragione di questo comportamento è perfettamente indicato da @flowerysong la risposta, c'è una soluzione alternativa che è possibile utilizzare per ottenere il vostro requisito.
Il trucco è usare il fatto che si possono definire due svolge all'interno di un playbook e, quindi, utilizzare la variabile impostata su host(s) del primo gioco.
Così, che cosa stiamo andando a fare è:
- Eseguire un gioco
localhost
e impostare un dato di fatto provenienti dal gruppo di host che vogliamo target nel secondo gioco
- Facoltativamente: anche io ho definito un
target_hosts
variabile nel primo gioco, per essere sicuri che, se la seconda partita di destinazione) viene modificato l'host variabile di gruppo è sempre quello che la seconda partita andando a bersaglio.
- Utilizzare tale variabile nel secondo gioco, da
localhost
's hostvars
Nota Extra: non c'è nessun gusti sono gusti, ma io preferisco l' inline se il ternary
filtro, comunque, sono assolutamente equivalenti.
Così, dato il playbook:
- 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:
- Quando viene eseguito con
host_set: true
nel nodes
gruppo:
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
- Quando viene eseguito con
host_set: false
nel nodes
gruppo:
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