I have these two lists:
"list": [
{
"DEVICE_NAME": "uscx001.net",
"IMPLEMENTATION": "system\nInterface range Gi1/10/0/42 to Gi1/10/0/45 Gi2/10/0/32 to Gi2/10/0/34\ndefault\nport link-mode bridge\ndescription *::HP-HSP::Available::::\nport access vlan 999\nshutdown\nreturn\nsave force",
"POST_CHECK": "dis inter brief | inc 1/10/0/4[2345]|2/10/0/3[234]",
"PRE_CHECK": "dis inter brief | inc 1/10/0/4[2345]|2/10/0/3[234]",
"NO": "1"
},
{
"DEVICE_NAME": "uscx002.net",
"IMPLEMENTATION": "system\nInterface range Gi1/9/0/9 to Gi1/9/0/12 Gi2/9/0/9 to Gi2/9/0/11\ndefault\nport link-mode bridge\ndescription *::HP-HSP::Available::::\nport access vlan 999\nshutdown\nreturn\nsave force",
"POST_CHECK": "dis inter brief | inc 1/9/0/9|1/9/0/1[012]|2/9/0/9|2/9/0/1[01]",
"PRE_CHECK": "dis inter brief | inc 1/9/0/9|1/9/0/1[012]|2/9/0/9|2/9/0/1[01]",
"NO": "2"
}
]
and
"json": {
"offset": 0,
"limit": 1000,
"search": {},
"filter_op": "and",
"total": 701,
"fields": [
"ID",
"Name",
"PluginKey"
],
"data": [
{
"ID": 54,
"Name": "inet-fw.lab.net",
"PluginKey": "checkpoint_gaia"
},
{
"ID": 2558,
"Name": "uscx001.net",
"PluginKey": "hp_aseries_alt_2"
},
{
"ID": 2559,
"Name": "uscx002.net",
"PluginKey": "hp_aseries_alt_2"
},
{
"ID": 2560,
"Name": "test1.net",
"PluginKey": "hp_aseries_alt_2"
}
]
}
Intention is create a new list which will contain first list with additional information ID and PluginKey from second list for found devices from second list, so it will look like as following:
"list2": [
{
"DEVICE_NAME": "uscx001.net",
"IMPLEMENTATION": "system\nInterface range Gi1/10/0/42 to Gi1/10/0/45 Gi2/10/0/32 to Gi2/10/0/34\ndefault\nport link-mode bridge\ndescription *::HP-HSP::Available::::\nport access vlan 999\nshutdown\nreturn\nsave force",
"POST_CHECK": "dis inter brief | inc 1/10/0/4[2345]|2/10/0/3[234]",
"PRE_CHECK": "dis inter brief | inc 1/10/0/4[2345]|2/10/0/3[234]",
"NO": "1",
"ID": 2558,
"PluginKey": "hp_aseries_alt_2"
},
{
"DEVICE_NAME": "uscx002.net",
"IMPLEMENTATION": "system\nInterface range Gi1/9/0/9 to Gi1/9/0/12 Gi2/9/0/9 to Gi2/9/0/11\ndefault\nport link-mode bridge\ndescription *::HP-HSP::Available::::\nport access vlan 999\nshutdown\nreturn\nsave force",
"POST_CHECK": "dis inter brief | inc 1/9/0/9|1/9/0/1[012]|2/9/0/9|2/9/0/1[01]",
"PRE_CHECK": "dis inter brief | inc 1/9/0/9|1/9/0/1[012]|2/9/0/9|2/9/0/1[01]",
"NO": "2",
"ID": 2559,
"PluginKey": "hp_aseries_alt_2"
}
]
I have been playing with combine, zip, map but it always gave me only one item instead of full list of items.
Could someone help me with this task please?
Ideally it would be perfect if we could use filters instead of loop, since I expect a very big list of thousands items - this example is just very small extract of data.
Does this give you what you want?
- name: create new list
set_fact:
list2: "{{ list2 | default([]) + [item | combine({'ID': device.ID, 'PluginKey': device.PluginKey})] }}"
loop: "{{ list1 }}"
vars:
device: "{{ json2.data | selectattr('Name', 'equalto', item.DEVICE_NAME) | list | first }}"
- name: debug it
debug:
msg: "{{ list2 }}"