'Ansible: Remove item from dict

I have a situation where i am trying to remove the item from a list. but i am not getting expected result. Please help me what I am doing wrong here?

here is the list :

    "get_ec2_id.instances[0].tags": {
    "Name": "test-db-system-2",
    "aws:cloudformation:logical-id": "DBInstance",
    "aws:cloudformation:stack-id": "arn:aws:cloudformation:us-east-1:123456789012:stack/test-db-system-2/0115v0a0-5d44-17e8-a024-503ama4a5qd1",
    "aws:cloudformation:stack-name": "test-db-system-2",
    "dbsystem:stack": "test-db-system-2",
    "dbsystem:type": "db"
}

}

I am trying to remove the all "aws:cloudformation" tags from a list using below filter:

    "{{ get_ec2_id.instances[0].tags | reject('search','aws:') | list }}"

I am getting the below result:

    ok: [10.52.8.101] => {
"instances_tags": [
    "dbsystem:type",
    "dbsystem:stack",
    "Name"
]
    }

but I am expected below result :

      "instances_tags": [
    "dbsystem:stack": "test-db-system-2",
    "dbsystem:type": "db"
    "Name" : "test-db-system-2",
]
  }

Help me to solve the issue.



Solution 1:[1]

More generic solution where input is a dict and blacklist is a list:

---
- set_fact:
    blacklist:
      - bad1
      - bad2
- set_fact:
    output: {}
- name: remove blacklisted items from input
  set_fact:
    output: "{{ output | combine({item.key: item.value}) }}"
  when: item.key not in blacklist
  loop: "{{ input | dict2items }}"

Solution 2:[2]

Use this:

---
- name: dictionary
  hosts: localhost
  gather_facts: False
  connection: local



  vars:
    get_ec2_id:
      instances:
        tags:
          Name: "test-db-system-2"
          "aws:cloudformation:logical-id": "DBInstance"
          "aws:cloudformation:stack-id": "arn:aws:cloudformation:us-east-1:123456789012:stack/test-db-system-2/0115v0a0-5d44-17e8-a024-503ama4a5qd1"
          "aws:cloudformation:stack-name": "test-db-system-2"
          "dbsystem:stack": "test-db-system-2"
          "dbsystem:type": "db"
    dict2: {}

  tasks:

    - name: Fact1
      set_fact:
        dict: "{{ get_ec2_id.instances.tags }}"

    - name: Debug1
      debug:
        var: dict

    - name: Fact2
      set_fact:
        dict2: "{{ dict2 | combine({item.key: item.value}) }}"
      when: "{{ item.key.find('aws:') }}"
      with_dict: "{{ dict }}"

    - name: Debug2
      debug:
        var: dict2

Output:

TASK [Debug2] ******************************************************************
ok: [localhost] => {
    "dict2": {
        "Name": "test-db-system-2", 
        "dbsystem:stack": "test-db-system-2", 
        "dbsystem:type": "db"
    }
}

Solution 3:[3]

Given the data
  get_ec2_id:
    instances:
    - tags:
        Name: test-db-system-2
        aws:cloudformation:logical-id: DBInstance
        aws:cloudformation:stack-id: arn:aws:cloudformation:us-east-1:123456789012:stack/test-db-system-2/0115v0a0-5d44-17e8-a024-503ama4a5qd1
        aws:cloudformation:stack-name: test-db-system-2
        dbsystem:stack: test-db-system-2
        dbsystem:type: db

Use rejectattr. For example

  dict2: "{{ get_ec2_id.instances.0.tags|
             dict2items|
             rejectattr('key', 'search', 'aws:')|
             items2dict }}"

gives

  dict2:
    Name: test-db-system-2
    dbsystem:stack: test-db-system-2
    dbsystem:type: db

Then, convert the dictionary into a list of dictionaries

  instances_tags: "{{ dict2|
                      dict2items|
                      json_query('[].[[key, value]]')|
                      map('community.general.dict')|
                      list }}"

gives

  instances_tags:
  - Name: test-db-system-2
  - dbsystem:stack: test-db-system-2
  - dbsystem:type: db

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nyet
Solution 2 imjoseangel
Solution 3