Ansible filter a list of dictionaries to only contain unique values in one field

absurd

I have a list of dictionaries in an ansible variable. Some dictionaries have the same value in the field 'id and the field 'name' while they differ in other key value pairs (that are not important for me). I want to filter out all those dictionaries that are "duplicates" regarding the 'name' and 'id' fields.

Example:

[{
        "name": "abc",
          "id": "123456",
   "other_key": "unimportant value"
  },
  {
        "name": "abc",
          "id": "123456",
   "other_key": "another unimportant value"
  },
  {
        "name": "bcd",
          "id": "789012",
   "other_key": "unimportant value"
  }]

Desired result:

[{
        "name": "abc",
          "id": "123456"
  },
  {
        "name": "bcd",
          "id": "789012"
  }]

How can I achieve this in Ansible? (the 'other_key' variable does not necessarily have to be discarded it could also be e.g. just the first occurrence, it just does not matter).

I already produced a list of unique ids with:

{{ mydictionaries | map(attribute='id') | unique | list }}

But how do I filter the list of dictionaries with this?

Zeitounator

You can filter only the desired keys from your map list with json_query and then apply the unique filter.

{{ mydictionnaries | json_query('[].{"name": name, "id": id}') | unique }}

Below a proof of concept playbook. Please note in the above doc that json_query requires pip install jmespath on the ansible controller.

---
- name: Unique filtered dictionaries list example
  hosts: localhost
  gather_facts: false

  vars:
    mydictionaries: [{"name": "abc","id": "123456","other_key": "unimportant value"},{"name": "abc","id": "123456","other_key": "another unimportant value"},{"name": "bcd","id": "789012","other_key": "unimportant value"}]

  tasks:
    - name: Filter out list as wanted
      debug:
        msg: >-
          {{
            mydictionaries
            | json_query('[].{"name": name, "id": id}')
            | unique
          }}

which gives

PLAY [Unique filtered dictionaries list example] *************************************************************************************************************************************************************************************************************************************

TASK [Filter out list as wanted] ****************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        {
            "id": "123456",
            "name": "abc"
        },
        {
            "id": "789012",
            "name": "bcd"
        }
    ]
}

PLAY RECAP **************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Merging dictionaries into one list of dictionaries on a unique id

How can I filter a list within a dataframe to contain only unique values?

Filter elasticsearch results to contain only unique documents based on one field value

Filter list of dictionaries with duplicate values at a certain key

Clean way to filter an array of dictionaries by unique dictionary values

Filter array for unique field values

Generating a list of dictionaries in python using lists that contain duplicate values

How to loop through a list of dictionaries that contain more than one key, and change different key values?

Filter dataframe by values in a list of dictionaries in pyspark

Swap values in dictionary which contain list of dictionaries?

Filter elements from list of dictionaries in ansible that match a condition

Pandas filter dataframe by values in a list of dictionaries

Compare values in a list of dictionaries and return only dictionaries with highest values

Filtering only one of a list of unique values using dplyr

how to validate field to contain only specific values?

Ansible: Updating values in a list of dictionaries

How to merge values of dictionaries with same key into one from a list of dictionaries?

Filter list of dictionaries by upper/lower limit on values

LINQ Filter values on list values using contain

List of dictionaries to list of one property's values

Ansible get unique list of dictionaries based on a key

Get unique keys and their unique values in a list of nested dictionaries

Filter key-values from a list of dictionaries

How to get unique values from a list of dictionaries?

Filter one dictionary out of list of dictionaries and extract values of certain keys

Find unique values in python list of dictionaries

I need to get the unique values from the tags field in all the nested dictionaries from the list

How to sort a list of dictionaries by a list that might contain duplicate values?

How to sort a list of dictionaries by a list that can contain duplicate values?