Ansible:如何查找连接到子网的路由 ID

Ansible:如何查找连接到子网的路由 ID

我有一个包含一些路由和子网的 VPC。我想让 ansible 使用它,但我找不到将子网与路由表 ID 关联起来的方法。我使用了 ec2_vpc_route_table_facts、ec2_vpc_subnet_facts 和 ec2_vpc_net_facts 模块,但除非我遗漏了什么,否则它们都无法告诉我哪个子网与哪个路由表相关联。

TASK [ec2_vpc_route_table_facts] ***********************************************
ok: [localhost]

TASK [display routes] **********************************************************
ok: [localhost] => {
    "msg": {
        "changed": false, 
        "route_tables": [
            {
                "id": "rtb-83fd25e7", 
                "routes": [
                    {
                        "destination_cidr_block": "172.31.0.0/16", 
                        "gateway_id": "local", 
                        "instance_id": null, 
                        "interface_id": null, 
                        "origin": "CreateRouteTable", 
                        "state": "active", 
                        "vpc_peering_connection_id": null
                    }, 
                    {
                        "destination_cidr_block": "0.0.0.0/0", 
                        "gateway_id": "igw-6f792c0a", 
                        "instance_id": null, 
                        "interface_id": null, 
                        "origin": "CreateRoute", 
                        "state": "active", 
                        "vpc_peering_connection_id": null
                    }
                ], 
                "tags": {}, 
                "vpc_id": "vpc-e749a683"
            }, 
            {
                "id": "rtb-abf02bcf", 
                "routes": [
                    {
                        "destination_cidr_block": "172.31.0.0/16", 
                        "gateway_id": "local", 
                        "instance_id": null, 
                        "interface_id": null, 
                        "origin": "CreateRouteTable", 
                        "state": "active", 
                        "vpc_peering_connection_id": null
                    }
                ], 
                "tags": {}, 
                "vpc_id": "vpc-e749a683"
            }
        ]
    }
}

TASK [ec2_vpc_subnet_facts] ****************************************************
ok: [localhost]

TASK [display subnets] *********************************************************
ok: [localhost] => {
    "msg": {
        "changed": false, 
        "subnets": [
            {
                "availability_zone": "eu-west-1b", 
                "available_ip_address_count": 4091, 
                "cidr_block": "172.31.16.0/20", 
                "default_for_az": "true", 
                "id": "subnet-3ac33c4c", 
                "map_public_ip_on_launch": "true", 
                "state": "available", 
                "tags": {}, 
                "vpc_id": "vpc-e749a683"
            }, 
            {
                "availability_zone": "eu-west-1c", 
                "available_ip_address_count": 4091, 
                "cidr_block": "172.31.32.0/20", 
                "default_for_az": "true", 
                "id": "subnet-4efbef17", 
                "map_public_ip_on_launch": "true", 
                "state": "available", 
                "tags": {}, 
                "vpc_id": "vpc-e749a683"
            }, 
            {
                "availability_zone": "eu-west-1a", 
                "available_ip_address_count": 4091, 
                "cidr_block": "172.31.0.0/20", 
                "default_for_az": "true", 
                "id": "subnet-9a3deafe", 
                "map_public_ip_on_launch": "true", 
                "state": "available", 
                "tags": {}, 
                "vpc_id": "vpc-e749a683"
            }
        ]
    }
}

我认为实现此目的的唯一方法是在路由中包含具有合理值的标签(例如子网 ID)。但这仍然需要有人手动保持更新,没有办法通过 Ansible 自动执行此操作,对吗?

答案1

我认为一个放置良好的过滤器插件可以帮到你。例如,如果你知道路由表 ID,你可以编写一个简单的 Python 函数来检索与路由表 ID 关联的 subnet_id 列表。你将把这个过滤器放在 ansible 目录的根目录中。

下面的例子.... filter_plugins/example.py

def get_all_subnet_ids_in_route_table(route_table_id, region=None):
    """
    Args:
        route_table_id (str): The route table id you are retrieving subnets for.

    Kwargs:
        region (str): The aws region in which the route table exists.

    Basic Usage:
        >>> get_all_subnet_ids_in_route_table("rtb-1234567", "us-west-2")
        ['subnet-1234567', 'subnet-7654321']

    Returns:
        List of subnet ids
    """
    subnet_ids = list()
    client = boto3.client('ec2', region_name=region)
    params = {
        'RouteTableIds': [route_table_id]
    }
    routes = client.describe_route_tables(**params)
    if routes:
        for route in routes['RouteTables']:
            for association in route['Associations']:
                if association.get('SubnetId', None):
                    subnet_ids.append(association['SubnetId'])
        return subnet_ids
    else:
        raise errors.AnsibleFilterError(
            "No subnets were found for {0}".format(route_table_id)
        )


class FilterModule(object):
    ''' Ansible core jinja2 filters '''

    def filters(self):
        return {
            'get_all_subnet_ids_in_route_table': get_all_subnet_ids_in_route_table
        }

如果您想使用此过滤器,只需执行以下操作即可。

route_table_id: rtb-1234567
aws_region: eu-west-1
subnet_ids_for_example_rt: "{{ route_table_id | get_all_subnet_ids_in_route_table(aws_region) }}"

答案2

看起来这个问题在 Ansible 2.3 中已经修复了(https://github.com/ansible/ansible/commit/90002e06ae0ab255d71e6976d7e5d23e93850cd3)。

现在,当您调用时ec2_vpc_route_table_facts,除了routes列表之外,每个返回的路由表还有一个associations列表,其中包含任何关联的 subnet_id

{
    "associations": [
        {
            "id": "rtbassoc-02cf4c00",
            "main": false,
            "route_table_id": "rtb-7051d400",
            "subnet_id": "subnet-3099da00"
        }
    ],
    "id": "rtb-7051d400",
    "routes": [
        {
            "destination_cidr_block": "52.123.123.123/32",
            "gateway_id": "igw-96298400",
            "instance_id": null,
            "interface_id": null,
            "state": "active",
            "vpc_peering_connection_id": null
        },
        {
            "destination_cidr_block": "52.123.123.124/32",
            "gateway_id": "igw-96298400",
            "instance_id": null,
            "interface_id": null,
            "state": "active",
            "vpc_peering_connection_id": null
        },
        {
            "destination_cidr_block": "10.69.123.0/24",
            "gateway_id": "local",
            "instance_id": null,
            "interface_id": null,
            "state": "active",
            "vpc_peering_connection_id": null
        }
    ],
    "tags": {
        "Name": "test-gmd-b-routes"
    },
    "vpc_id": "vpc-c5439a00"
}

相关内容