Merge lists

24 views
Skip to first unread message

Punit Jain

unread,
Jun 20, 2019, 5:01:19 AM6/20/19
to ansible...@googlegroups.com
Hi,

I have a fact as below:

"dns_parsed": [
    "10.4.3.2"
]

and another fact as:

"actual_config": {
    "dns": {
        "domains": ['test.com'],
        "servers": []
      }
 }

I want to update actual_config to make it:

"actual_config": {
    "dns": {
        "domains": ['test.com'],
        "servers": ["10.4.3.2"]
      }
 }

my playbook part to accomplish this:

- name: merge dns data
  set_fact:
    actual_config: "{{ actual_config.dns.servers | union(dns_parsed) }}"

- debug:
  var=actual_config

however it removes all other keys from actual_config.

"actual_config": [
      "10.4.3.2"
]

Any clue how to make it work ? 

Thanks and Regards,
Punit
    

Dick Visser

unread,
Jun 20, 2019, 5:14:41 AM6/20/19
to ansible...@googlegroups.com
You’re setting a dict to one of its own keys - I think that doesn’t work or at least results in nothing. But the union of that and dns_parsed will be the dns_parsed.

You could look into the ‘combine’ filter to overwrite a specific key. 

Dick

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAFXWBQ%2B0qy-kpxWOPkBDYw-Htq99Gm4cc53JE2JO_gG1zVSB-A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Dick Visser

unread,
Jun 20, 2019, 6:37:43 AM6/20/19
to ansible...@googlegroups.com
On Thu, 20 Jun 2019 at 12:14, Dick Visser <dick....@geant.org> wrote:
>
> You’re setting a dict to one of its own keys - I think that doesn’t work or at least results in nothing. But the union of that and dns_parsed will be the dns_parsed.
>
> You could look into the ‘combine’ filter to overwrite a specific key.

My first assumption was wrong. But the combine filter is the way,
you'd have to use the recursive option and create the containing dict,
like this:


- set_fact:
actual_config: "{{ actual_config | combine({'dns': {
'servers': dns_parsed }}, recursive=True) }}"

Dick

Punit Jain

unread,
Jun 20, 2019, 7:04:37 AM6/20/19
to ansible...@googlegroups.com
Thanks Dick works !!!

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages