Ansible:从字典中删除项目

问题描述 投票:0回答:4

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

这是列表:

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

帮我解决问题。

ansible ansible-2.x ansible-facts
4个回答
4
投票

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

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

4
投票
鉴于数据
  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

更新(社区.通用9.1.0)

问:“删除所有标签

aws:cloudformation

A:使用 community.general.remove_keys。声明

  instances_tags_target: '^aws:cloudformation.*$'
  instances_tags: "{{ get_ec2_id.instances| json_query('[].tags') |
                      community.general.remove_keys(target=instances_tags_target,
                                                    matching_parameter='regex') }}"

给予

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

用于测试的完整剧本示例

- hosts: localhost

  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

    instances_tags_target: '^aws:cloudformation.*$'
    instances_tags: "{{ get_ec2_id.instances| json_query('[].tags') |
                        community.general.remove_keys(target=instances_tags_target,
                                                      matching_parameter='regex') }}"

  tasks:

    - debug:
        var: instances_tags

起源

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

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

0
投票

从ansible 2.5开始,您可以使用

ansible.utils.remove_keys
,例如

  - name: Remove element from dictionary
    set_fact:
      dict: "{{ dict | ansible.utils.remove_keys('key_name')  }}"

来源:

© www.soinside.com 2019 - 2024. All rights reserved.