Ansible:从 AWS 中的路由 table 中删除一条路由

Ansible: delete one route from route table in AWS

我在 AWS 中有一个路由 table,其中有一个子网被路由到每个主机的一个主机。我可以使用以下代码自动设置这些路由:

- name: Add route to host container network
  ec2_vpc_route_table:
    region: region
    vpc_id: "vpc-somestring"
    purge_subnets: false
    purge_routes: false
    lookup: id
    route_table_id: rtb-somestring
    routes:
      - dest: "1.2.3.0/24"
        instance_id: "i-somestring"

这适合自动创建新主机。但是如果我想删除一个主机,我想删除匹配的路由table条目。

我想,我可以只使用 ec2_vpc_route_table_info 获取路由 table,然后使用 rejectattr 过滤的路由并将其反馈给 ec2_vpc_route_table,替换整个table。但是,信息给了我这种路由格式 tables:

    "all_routes": [
        {
            "destination_cidr_block": "1.2.3.0/24",
            "gateway_id": null,
            "instance_id": "i-somestring",
            "instance_owner_id": "1234567890",
            "interface_id": "eni-somestring",
            "network_interface_id": "eni-somestring",
            "origin": "CreateRoute",
            "state": "active"
        },
        {
            "destination_cidr_block": "5.5.5.0/21",
            "gateway_id": "local",
            "instance_id": null,
            "interface_id": null,
            "network_interface_id": null,
            "origin": "CreateRouteTable",
            "state": "active"
        },
        {
            "destination_cidr_block": null,
            "destination_ipv6_cidr_block": "affe:affe:affe:affe::/56",
            "gateway_id": "local",
            "instance_id": null,
            "interface_id": null,
            "network_interface_id": null,
            "origin": "CreateRouteTable",
            "state": "active"
        }
    ]

但是,我无法将 table 提供给 ec2_vpc_route_table,因为该模块只需要一个如下所示的列表:

[
  {
    "dest": "1.2.3.0/24",
    "instance_id": "i-somestring"
  },
  {
    "dest": "5.5.5.0/21",
    "gateway_id": "local
  },
  {
    "dest": "affe:affe:affe:affe::/56",
    "gateway_id": "local"
  } 
]

为什么信息模块的输出不是我可以反馈给 route_table 模块的格式?如何将输出转换为可以反馈给 route_table 模块的格式?

感谢任何意见。

解决方案示例:

- hosts: localhost
  gather_facts: false
  vars:
    all_routes: "{{ lookup('file', 'zson.json') | from_json }}"

  tasks:
    - name: display json
      debug: 
        var: all_routes

    - name: create new json
      set_fact:
        result: "{{ result | d([]) + [{ 'dest': _block, _key: _gateway }] }}"
      vars:
        _block: "{{ item.destination_cidr_block if item.destination_cidr_block != None else item.destination_ipv6_cidr_block }}"
        _gateway: "{{ item.gateway_id if item.gateway_id != None else item.instance_id }}"
        _key: "{{ 'gateway_id' if item.gateway_id != None else 'instance_id' }}"          
      loop: "{{all_routes }}"
      
    - name: display result
      debug: 
        var: result

结果:

ok: [localhost] => {
    "result": [
        {
            "dest": "1.2.3.0/24",
            "instance_id": "i-somestring"
        },
        {
            "dest": "5.5.5.0/21",
            "gateway_id": "local"
        },
        {
            "dest": "affe:affe:affe:affe::/56",
            "gateway_id": "local"
        }
    ]
}