| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: ec2_vpc_route_table_info |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Gather information about ec2 VPC route tables in AWS |
| 12 |
description: |
| 13 |
- Gather information about EC2 VPC route tables in AWS. |
| 14 |
author: |
| 15 |
- "Rob White (@wimnat)" |
| 16 |
- "Mark Chappell (@tremble)" |
| 17 |
options: |
| 18 |
filters: |
| 19 |
description: |
| 20 |
- A dict of filters to apply. Each dict item consists of a filter key and a filter value. |
| 21 |
See U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeRouteTables.html) for possible filters. |
| 22 |
type: dict |
| 23 |
default: {} |
| 24 |
extends_documentation_fragment: |
| 25 |
- amazon.aws.common.modules |
| 26 |
- amazon.aws.region.modules |
| 27 |
- amazon.aws.boto3 |
| 28 |
""" |
| 29 |
|
| 30 |
EXAMPLES = r""" |
| 31 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 32 |
|
| 33 |
- name: Gather information about all VPC route tables |
| 34 |
amazon.aws.ec2_vpc_route_table_info: |
| 35 |
|
| 36 |
- name: Gather information about a particular VPC route table using route table ID |
| 37 |
amazon.aws.ec2_vpc_route_table_info: |
| 38 |
filters: |
| 39 |
route-table-id: rtb-00112233 |
| 40 |
|
| 41 |
- name: Gather information about any VPC route table with a tag key Name and value Example |
| 42 |
amazon.aws.ec2_vpc_route_table_info: |
| 43 |
filters: |
| 44 |
"tag:Name": Example |
| 45 |
|
| 46 |
- name: Gather information about any VPC route table within VPC with ID vpc-abcdef00 |
| 47 |
amazon.aws.ec2_vpc_route_table_info: |
| 48 |
filters: |
| 49 |
vpc-id: vpc-abcdef00 |
| 50 |
""" |
| 51 |
|
| 52 |
RETURN = r""" |
| 53 |
route_tables: |
| 54 |
description: |
| 55 |
- A list of dictionarys describing route tables. |
| 56 |
- See also U(https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_route_tables). |
| 57 |
returned: always |
| 58 |
type: complex |
| 59 |
contains: |
| 60 |
associations: |
| 61 |
description: List of associations between the route table and one or more subnets or a gateway. |
| 62 |
returned: always |
| 63 |
type: complex |
| 64 |
contains: |
| 65 |
association_state: |
| 66 |
description: The state of the association. |
| 67 |
returned: always |
| 68 |
type: complex |
| 69 |
contains: |
| 70 |
state: |
| 71 |
description: The state of the association. |
| 72 |
returned: always |
| 73 |
type: str |
| 74 |
sample: associated |
| 75 |
state_message: |
| 76 |
description: Additional information about the state of the association. |
| 77 |
returned: when available |
| 78 |
type: str |
| 79 |
sample: 'Creating association' |
| 80 |
gateway_id: |
| 81 |
description: ID of the internet gateway or virtual private gateway. |
| 82 |
returned: when route table is a gateway route table |
| 83 |
type: str |
| 84 |
sample: igw-03312309 |
| 85 |
main: |
| 86 |
description: Whether this is the main route table. |
| 87 |
returned: always |
| 88 |
type: bool |
| 89 |
sample: false |
| 90 |
route_table_association_id: |
| 91 |
description: ID of association between route table and subnet. |
| 92 |
returned: always |
| 93 |
type: str |
| 94 |
sample: rtbassoc-ab47cfc3 |
| 95 |
route_table_id: |
| 96 |
description: ID of the route table. |
| 97 |
returned: always |
| 98 |
type: str |
| 99 |
sample: rtb-bf779ed7 |
| 100 |
subnet_id: |
| 101 |
description: ID of the subnet. |
| 102 |
returned: when route table is a subnet route table |
| 103 |
type: str |
| 104 |
sample: subnet-82055af9 |
| 105 |
id: |
| 106 |
description: ID of the route table (same as RV(route_tables.route_table_id) for backwards compatibility). |
| 107 |
returned: always |
| 108 |
type: str |
| 109 |
sample: rtb-bf779ed7 |
| 110 |
owner_id: |
| 111 |
description: ID of the account which owns the route table. |
| 112 |
returned: always |
| 113 |
type: str |
| 114 |
sample: '012345678912' |
| 115 |
propagating_vgws: |
| 116 |
description: List of Virtual Private Gateways propagating routes. |
| 117 |
returned: always |
| 118 |
type: list |
| 119 |
sample: [] |
| 120 |
route_table_id: |
| 121 |
description: ID of the route table. |
| 122 |
returned: always |
| 123 |
type: str |
| 124 |
sample: rtb-bf779ed7 |
| 125 |
routes: |
| 126 |
description: List of routes in the route table. |
| 127 |
returned: always |
| 128 |
type: complex |
| 129 |
contains: |
| 130 |
destination_cidr_block: |
| 131 |
description: CIDR block of destination. |
| 132 |
returned: always |
| 133 |
type: str |
| 134 |
sample: 10.228.228.0/22 |
| 135 |
gateway_id: |
| 136 |
description: ID of the gateway. |
| 137 |
returned: when gateway is local or internet gateway |
| 138 |
type: str |
| 139 |
sample: local |
| 140 |
instance_id: |
| 141 |
description: |
| 142 |
- ID of a NAT instance. |
| 143 |
- Empty unless the route is via an EC2 instance. |
| 144 |
returned: always |
| 145 |
type: str |
| 146 |
sample: i-abcd123456789 |
| 147 |
instance_owner_id: |
| 148 |
description: |
| 149 |
- AWS account owning the NAT instance. |
| 150 |
- Empty unless the route is via an EC2 instance. |
| 151 |
returned: always |
| 152 |
type: str |
| 153 |
sample: 123456789012 |
| 154 |
network_interface_id: |
| 155 |
description: |
| 156 |
- The ID of the network interface. |
| 157 |
- Empty unless the route is via an EC2 instance. |
| 158 |
returned: always |
| 159 |
type: str |
| 160 |
sample: 123456789012 |
| 161 |
nat_gateway_id: |
| 162 |
description: ID of the NAT gateway. |
| 163 |
returned: when the route is via a NAT gateway. |
| 164 |
type: str |
| 165 |
sample: local |
| 166 |
transit_gateway_id: |
| 167 |
description: ID of the Transit gateway. |
| 168 |
returned: when the route is via a Transit gateway. |
| 169 |
type: str |
| 170 |
sample: tgw-123456789012 |
| 171 |
version_added: 9.0.0 |
| 172 |
origin: |
| 173 |
description: mechanism through which the route is in the table. |
| 174 |
returned: always |
| 175 |
type: str |
| 176 |
sample: CreateRouteTable |
| 177 |
state: |
| 178 |
description: state of the route. |
| 179 |
returned: always |
| 180 |
type: str |
| 181 |
sample: active |
| 182 |
tags: |
| 183 |
description: Tags applied to the route table. |
| 184 |
returned: always |
| 185 |
type: dict |
| 186 |
sample: |
| 187 |
Name: Public route table |
| 188 |
Public: 'true' |
| 189 |
vpc_id: |
| 190 |
description: ID for the VPC in which the route lives. |
| 191 |
returned: always |
| 192 |
type: str |
| 193 |
sample: vpc-6e2d2407 |
| 194 |
""" |
| 195 |
|
| 196 |
from typing import Any |
| 197 |
from typing import Dict |
| 198 |
|
| 199 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 200 |
|
| 201 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error |
| 202 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_route_tables |
| 203 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 204 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 205 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list |
| 206 |
|
| 207 |
|
| 208 |
def normalize_route(route: Dict[str, Any]) -> Dict[str, Any]: |
| 209 |
# Historically these were all there, but set to null when empty' |
| 210 |
for legacy_key in ["DestinationCidrBlock", "GatewayId", "InstanceId", "Origin", "State", "NetworkInterfaceId"]: |
| 211 |
if legacy_key not in route: |
| 212 |
route[legacy_key] = None |
| 213 |
route["InterfaceId"] = route["NetworkInterfaceId"] |
| 214 |
return route |
| 215 |
|
| 216 |
|
| 217 |
def normalize_association(assoc: Dict[str, Any]) -> Dict[str, Any]: |
| 218 |
# Name change between boto v2 and boto v3, return both |
| 219 |
assoc["Id"] = assoc["RouteTableAssociationId"] |
| 220 |
return assoc |
| 221 |
|
| 222 |
|
| 223 |
def normalize_route_table(table: Dict[str, Any]) -> Dict[str, Any]: |
| 224 |
table["tags"] = boto3_tag_list_to_ansible_dict(table["Tags"]) |
| 225 |
table["Associations"] = [normalize_association(assoc) for assoc in table["Associations"]] |
| 226 |
table["Routes"] = [normalize_route(route) for route in table["Routes"]] |
| 227 |
table["Id"] = table["RouteTableId"] |
| 228 |
del table["Tags"] |
| 229 |
return camel_dict_to_snake_dict(table, ignore_list=["tags"]) |
| 230 |
|
| 231 |
|
| 232 |
def list_ec2_vpc_route_tables(connection, module: AnsibleAWSModule) -> None: |
| 233 |
filters = ansible_dict_to_boto3_filter_list(module.params.get("filters")) |
| 234 |
|
| 235 |
try: |
| 236 |
route_tables = describe_route_tables(connection, Filters=filters) |
| 237 |
except AnsibleEC2Error as e: |
| 238 |
module.fail_json_aws_error(e) |
| 239 |
|
| 240 |
route_tables = [normalize_route_table(table) for table in route_tables] |
| 241 |
module.exit_json(changed=False, route_tables=route_tables) |
| 242 |
|
| 243 |
|
| 244 |
def main() -> None: |
| 245 |
argument_spec = dict( |
| 246 |
filters=dict(default={}, type="dict"), |
| 247 |
) |
| 248 |
|
| 249 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 250 |
|
| 251 |
connection = module.client("ec2") |
| 252 |
|
| 253 |
list_ec2_vpc_route_tables(connection, module) |
| 254 |
|
| 255 |
|
| 256 |
if __name__ == "__main__": |
| 257 |
main() |