Merge lists

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

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

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

Thanks Dick works !!!