I have two lists:
list1: [ {'a': 'name1', 'id': 'ABC'}, {'ax': 'name2', 'id': 'DEF'} ]
list2: [ {'a': 'nameX', 'id': 'XYZ'}, {'ab': 'nameY', 'id': 'DEF'} ]
I need to derive another list, list3 which should contain the elements of list1 that is not present in list2 based on the key - id's value.
I've tried with jinja2 but not getting the desired output.
Playbook:
- name: test
hosts: localhost
connection: local
vars:
- list1: [ {'a': 'name1', 'id': 'ABC'}, {'ax': 'name2', 'id': 'DEF'} ]
- list2: [ {'a': 'nameX', 'id': 'XYZ'}, {'ab': 'nameY', 'id': 'DEF'} ]
gather_facts: False
tasks:
- set_fact:
list3: |-
{%- set dL = [] -%}
{%- for i in list1 -%}
{%- for j in list2 -%}
{%- if (i['id'] != j['id']) and (i not in dL) -%}
{{- dL.append(i) -}}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{{- dL -}}
- debug: var=list3
Output:
"list3": [
{
"a": "name1",
"id": "ABC"
},
{
"ax": "name2",
"id": "DEF"
}
]
Expected Output:
"list3": [
{
"a": "name1",
"id": "ABC"
}
]
How can I achieve this result?