【问题标题】:Ansible: delete one route from route table in AWSAnsible:从 AWS 的路由表中删除一条路由
【发布时间】:2021-12-27 18:57:57
【问题描述】:

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

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

这对于自动创建新主机来说很好。但是如果我想删除一个主机,我想删除匹配的路由表条目。

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

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

但是,我无法将该表提供给 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"
  } 
]

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

感谢您的任何意见。

【问题讨论】:

    标签: ansible amazon-vpc


    【解决方案1】:

    解决方案示例:

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

    【讨论】:

    • 哇,谢谢。我的头脑仍然无法想出那些类型的解决方案。 ;)
    • 现在唯一存在的问题是,ec2_vpc_route_table 模块尚不支持 v6,因此无法实际用于此类任务。
    • 我不明白你的问题是什么,但你可以提出一个新的问题。如果在纯 ansible 中做起来很复杂,我们可以使用用 python 编写的自定义插件过滤器。
    • 抱歉,这只是在发现我无法使用您的工作解决方案后的评论,因为 ansible 模块无法重新插入找到的 IPv6 路由。解决方案没问题。
    猜你喜欢
    • 1970-01-01
    • 2014-04-09
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 2021-08-21
    • 2016-03-25
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多