Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

Ansible: Add an additional field to a set of values already defined in hosts.yaml

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...