I am trying to replace the key values in a dict inside a list(here only for the values inside Objectids) with the matching values from another list. Somehow i am only able to replace the 1st value but not iterating the whole list. Here the same key in the input.json will have multiple matching values from the finallist.json and all those values needs to be matched and added to the get the final expected output.
Input.json
[
{
"name": "DNS_One",
"objectIds": [
"DNS_One",
"DNS_One-HO",
"NTP"
]
},
{
"name": "DNS_Two",
"objectIds": [
"NTP"
]
}
]
finallist.json
[
{
"id": "123456",
"net_name": "DNS_One"
},
{
"id": "789101112",
"net_name": "DNS_One"
},
{
"id": "131415161718",
"net_name": "DNS_One-HO"
},
{
"id": "23897845",
"net_name": "NTP"
},
{
"id": "9879879546",
"net_name": "NTP"
}
]
Playbook
- name: Id mapping
vars:
objectid: >-
{{
finallist
| selectattr('net_name', 'in', item.1)
| map(attribute = 'id')
| first
| default([])
}}
set_fact:
obj: >-
{{
obj | default([]) +
[item.0 | combine({'objectIds': [objectid]})]
}}
with_subelements:
- "{{ input }}"
- objectIds
ignore_errors: yes
Expected Output
[
{
"name": "DNS_One",
"objectIds": [
"123456","789101112"
"131415161718",
"23897845","9879879546"
]
},
{
"name": "DNS_Two",
"objectIds": [
"23897845","9879879546"
]
}
]