Bu davranışın nedeni @flowerysong'un cevabı tarafından mükemmel bir şekilde ifade edildiğinde, gereksiniminize ulaşmak için kullanabileceğiniz bir geçici çözüm vardır.
İşin püf noktası, bir oyun kitabının içinde iki oyun tanımlayabilmeniz ve ardından ilk oyunun ana bilgisayar(lar) ında ayarlanan değişkeni kullanabilmenizdir.
Peki, ne yapacağız ne:
- Bir oyun çalıştırın
localhost
ve ikinci oyunda hedeflemeyi hedeflediğimiz ev sahibi gruptan gelen bir gerçeği belirledik
- İsteğe bağlı olarak: Ayrıca bir tanımladım
target_hosts
değişken ilk oyunda, ikinci oyunun hedefi değiştirilirse, ana grup değişkeninin her zaman ikinci oyunun hedefleyeceği değişken olduğundan emin olmak için.
- Bu değişkeni ikinci oyunda kullanın:
localhost
'shostvars
Ekstra not: tat için bir muhasebe yoktur, ancak satır içi tercih ederim ternary
filtre, yine de, kesinlikle eşdeğerdir.
Bu yüzden, senaryo verilen :
- 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:
- İle çalıştırıldığında
host_set: true
in the 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
- İle çalıştırıldığında
host_set: false
in the 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