Ansible:从字典中删除项目

Ansible: Remove item from dict

我遇到了一种情况,我想从列表中删除该项目。但我没有得到预期的结果。请帮助我在这里做错了什么?

这是列表:

    "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"
}

}

我正在尝试使用以下过滤器从列表中删除所有 "aws:cloudformation" 标签:

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

我得到以下结果:

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

但我预计结果如下:

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

帮我解决问题。

使用这个:

---
- 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

输出:

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

更通用的解决方案,其中输入是字典,黑名单是列表:

---
- 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 }}"
鉴于数据
  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

使用rejectattr。例如

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

给予

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

然后,将字典转换为字典列表

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

给予

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