I have the following piece of code inside the inventory hosts.yaml:
---
all:
hosts:
myhost:
default_ok:
a:
x: default
y: default
b: default
c: default
patch_ok:
a:
y: patch
z: patch
b: patch
default_nok:
- a:
- x
- y
c: default_c_nok
- a:
- x
- y
c: default2_c_nok
patch_nok:
b: patch_b_nok
and the following piece of code for the Playbook:
- name: test OK
set_fact:
default_ok: '{{ default_ok | combine(patch_ok, recursive=true) }}'
tags: [init]
- name: debug test OK
ansible.builtin.debug:
loop: '{{ [default_ok] }}'
tags: [init]
- name: test NOK
set_fact:
default_nok: '{{ default_nok | combine(patch_nok, recursive=true) }}'
loop: '{{ default_nok }}'
tags: [init]
- name: debug test NOK
ansible.builtin.debug:
loop: '{{ [default_nok] }}'
tags: [init]
For the default_ok
all looks good:
default_ok:
a:
x: default
y: patch
z: patch
b: patch
c: default
Debug Logs for the good case:
TASK [sandbox : test OK] *********************************************************
ok: [myhost] => (item={'c': 'default', 'a': {'x': 'default', 'y': 'default'}, 'b': 'default'})
TASK [sandbox : debug test OK] ***************************************************
ok: [myhost] => (item={'c': 'default', 'a': {'y': 'patch', 'x': 'default', 'z': 'patch'}, 'b': 'patch'}) => {
"msg": "Hello world!"
}
For the default_nok
example we should see in debug at the end the following:
default_nok:
- a:
- x
- y
b: patch_b_nok
c: default_c_nok
- a:
- x
- y
b: patch_b_nok
c: default2_c_nok
BUT the debug just show the adjustment of one:
TASK [sandbox : test NOK] *********************************************************
ok: [myhost] => (item={'c': 'default_c_nok', 'a': ['x', 'y']})
ok: [myhost] => (item={'c': 'default2_c_nok', 'a': ['x', 'y']})
TASK [sandbox : debug test NOK] ***************************************************
ok: [myhost] => (item={'c': 'default2_c_nok', 'b': 'patch_b_nok', 'a': ['x', 'y']}) => {
"msg": "Hello world!"
}
Any thoughts?
Thanks.
question from:
https://stackoverflow.com/questions/65870096/ansible-add-an-additional-field-to-a-set-of-values-already-defined-in-hosts-yam 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…