| 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 |
module: ec2_transit_gateway |
| 9 |
short_description: Create and delete AWS Transit Gateways |
| 10 |
version_added: 1.0.0 |
| 11 |
version_added_collection: community.aws |
| 12 |
description: |
| 13 |
- Creates AWS Transit Gateways. |
| 14 |
- Deletes AWS Transit Gateways. |
| 15 |
- Updates tags on existing transit gateways. |
| 16 |
options: |
| 17 |
asn: |
| 18 |
description: |
| 19 |
- A private Autonomous System Number (ASN) for the Amazon side of a BGP session. |
| 20 |
- The range is 64512 to 65534 for 16-bit ASNs and 4200000000 to 4294967294 for 32-bit ASNs. |
| 21 |
type: int |
| 22 |
auto_associate: |
| 23 |
description: |
| 24 |
- Enable or disable automatic association with the default association route table. |
| 25 |
default: true |
| 26 |
type: bool |
| 27 |
auto_attach: |
| 28 |
description: Enable or disable automatic acceptance of attachment requests. |
| 29 |
default: false |
| 30 |
type: bool |
| 31 |
auto_propagate: |
| 32 |
description: Enable or disable automatic propagation of routes to the default propagation route table. |
| 33 |
default: true |
| 34 |
type: bool |
| 35 |
description: |
| 36 |
description: The description of the transit gateway. |
| 37 |
type: str |
| 38 |
dns_support: |
| 39 |
description: Whether to enable AWS DNS support. |
| 40 |
default: true |
| 41 |
type: bool |
| 42 |
multicast_support: |
| 43 |
description: Whether to enable AWS Multicast support. Valid only at the time of creation of the Transit Gateway. |
| 44 |
type: bool |
| 45 |
version_added: 8.1.0 |
| 46 |
state: |
| 47 |
description: |
| 48 |
- V(present) to ensure resource is created. |
| 49 |
- V(absent) to remove resource. |
| 50 |
default: present |
| 51 |
choices: [ "present", "absent"] |
| 52 |
type: str |
| 53 |
transit_gateway_id: |
| 54 |
description: The ID of the transit gateway. |
| 55 |
type: str |
| 56 |
vpn_ecmp_support: |
| 57 |
description: Enable or disable Equal Cost Multipath Protocol support. |
| 58 |
default: true |
| 59 |
type: bool |
| 60 |
wait: |
| 61 |
description: Whether to wait for status. |
| 62 |
default: true |
| 63 |
type: bool |
| 64 |
wait_timeout: |
| 65 |
description: Number of seconds to wait for status. |
| 66 |
default: 300 |
| 67 |
type: int |
| 68 |
|
| 69 |
author: |
| 70 |
- "Bob Boldin (@BobBoldin)" |
| 71 |
extends_documentation_fragment: |
| 72 |
- amazon.aws.common.modules |
| 73 |
- amazon.aws.region.modules |
| 74 |
- amazon.aws.boto3 |
| 75 |
- amazon.aws.tags |
| 76 |
""" |
| 77 |
|
| 78 |
EXAMPLES = r""" |
| 79 |
- name: Create a new transit gateway using defaults |
| 80 |
amazon.aws.ec2_transit_gateway: |
| 81 |
state: present |
| 82 |
region: us-east-1 |
| 83 |
description: personal-testing |
| 84 |
register: created_tgw |
| 85 |
|
| 86 |
- name: Create a new transit gateway with options |
| 87 |
amazon.aws.ec2_transit_gateway: |
| 88 |
asn: 64514 |
| 89 |
auto_associate: false |
| 90 |
auto_propagate: false |
| 91 |
multicast_support: true |
| 92 |
dns_support: true |
| 93 |
description: "nonprod transit gateway" |
| 94 |
purge_tags: false |
| 95 |
state: present |
| 96 |
region: us-east-1 |
| 97 |
tags: |
| 98 |
Name: nonprod transit gateway |
| 99 |
status: testing |
| 100 |
|
| 101 |
- name: Remove a transit gateway by description |
| 102 |
amazon.aws.ec2_transit_gateway: |
| 103 |
state: absent |
| 104 |
region: us-east-1 |
| 105 |
description: personal-testing |
| 106 |
|
| 107 |
- name: Remove a transit gateway by id |
| 108 |
amazon.aws.ec2_transit_gateway: |
| 109 |
state: absent |
| 110 |
region: ap-southeast-2 |
| 111 |
transit_gateway_id: tgw-3a9aa123 |
| 112 |
register: deleted_tgw |
| 113 |
""" |
| 114 |
|
| 115 |
RETURN = r""" |
| 116 |
transit_gateway: |
| 117 |
description: The attributes of the transit gateway. |
| 118 |
type: dict |
| 119 |
returned: I(state=present) |
| 120 |
contains: |
| 121 |
creation_time: |
| 122 |
description: The creation time of the transit gateway. |
| 123 |
returned: always |
| 124 |
type: str |
| 125 |
sample: "2019-03-06T17:13:51+00:00" |
| 126 |
description: |
| 127 |
description: The description of the transit gateway. |
| 128 |
returned: always |
| 129 |
type: str |
| 130 |
sample: "my test tgw" |
| 131 |
options: |
| 132 |
description: The options attributes of the transit gateway. |
| 133 |
returned: always |
| 134 |
type: dict |
| 135 |
contains: |
| 136 |
amazon_side_asn: |
| 137 |
description: |
| 138 |
- A private Autonomous System Number (ASN) for the Amazon side of a BGP session. |
| 139 |
The range is 64512 to 65534 for 16-bit ASNs and 4200000000 to 4294967294 for 32-bit ASNs. |
| 140 |
returned: always |
| 141 |
type: str |
| 142 |
sample: "64512" |
| 143 |
auto_accept_shared_attachements: |
| 144 |
description: Indicates whether attachment requests are automatically accepted. |
| 145 |
returned: always |
| 146 |
type: str |
| 147 |
sample: "disable" |
| 148 |
default_route_table_association: |
| 149 |
description: |
| 150 |
- Indicates whether resource attachments are automatically |
| 151 |
associated with the default association route table. |
| 152 |
returned: always |
| 153 |
type: str |
| 154 |
sample: "enable" |
| 155 |
association_default_route_table_id: |
| 156 |
description: The ID of the default association route table. |
| 157 |
returned: Iwhen exists |
| 158 |
type: str |
| 159 |
sample: "tgw-rtb-abc123444" |
| 160 |
default_route_table_propagation: |
| 161 |
description: |
| 162 |
- Indicates whether resource attachments automatically |
| 163 |
propagate routes to the default propagation route table. |
| 164 |
returned: always |
| 165 |
type: str |
| 166 |
sample: "disable" |
| 167 |
propagation_default_route_table_id: |
| 168 |
description: The ID of the default propagation route table. |
| 169 |
returned: when exists |
| 170 |
type: str |
| 171 |
sample: "tgw-rtb-def456777" |
| 172 |
vpn_ecmp_support: |
| 173 |
description: Indicates whether Equal Cost Multipath Protocol support is enabled. |
| 174 |
returned: always |
| 175 |
type: str |
| 176 |
sample: "enable" |
| 177 |
dns_support: |
| 178 |
description: Indicates whether DNS support is enabled. |
| 179 |
returned: always |
| 180 |
type: str |
| 181 |
sample: "enable" |
| 182 |
multicast_support: |
| 183 |
description: Indicates whether Multicast support is enabled. |
| 184 |
returned: always |
| 185 |
type: str |
| 186 |
sample: "enable" |
| 187 |
version_added: 7.3.0 |
| 188 |
owner_id: |
| 189 |
description: The account that owns the transit gateway. |
| 190 |
returned: always |
| 191 |
type: str |
| 192 |
sample: "123456789012" |
| 193 |
state: |
| 194 |
description: The state of the transit gateway. |
| 195 |
returned: always |
| 196 |
type: str |
| 197 |
sample: "pending" |
| 198 |
tags: |
| 199 |
description: A dictionary of resource tags. |
| 200 |
returned: always |
| 201 |
type: dict |
| 202 |
sample: |
| 203 |
tags: |
| 204 |
Name: nonprod_tgw |
| 205 |
transit_gateway_arn: |
| 206 |
description: The ID of the transit_gateway. |
| 207 |
returned: always |
| 208 |
type: str |
| 209 |
sample: "tgw-3a9aa123" |
| 210 |
transit_gateway_id: |
| 211 |
description: The ID of the transit_gateway. |
| 212 |
returned: always |
| 213 |
type: str |
| 214 |
sample: "tgw-3a9aa123" |
| 215 |
""" |
| 216 |
|
| 217 |
from typing import Any |
| 218 |
from typing import Dict |
| 219 |
from typing import Optional |
| 220 |
|
| 221 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 222 |
|
| 223 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import create_ec2_transit_gateway |
| 224 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import delete_ec2_transit_gateway |
| 225 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_ec2_transit_gateways |
| 226 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ensure_ec2_tags |
| 227 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 228 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 229 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list |
| 230 |
from ansible_collections.amazon.aws.plugins.module_utils.waiters import wait_for_resource_state |
| 231 |
|
| 232 |
|
| 233 |
class AnsibleEc2Tgw: |
| 234 |
def __init__(self, module: AnsibleAWSModule, results: Dict[str, Any]) -> None: |
| 235 |
self._module = module |
| 236 |
self._results = results |
| 237 |
self._connection = module.client("ec2") |
| 238 |
self._check_mode = self._module.check_mode |
| 239 |
|
| 240 |
def process(self) -> None: |
| 241 |
"""Process the request based on state parameter.""" |
| 242 |
description = self._module.params.get("description") |
| 243 |
state = self._module.params.get("state", "present") |
| 244 |
tgw_id = self._module.params.get("transit_gateway_id") |
| 245 |
|
| 246 |
if state == "present": |
| 247 |
self.ensure_tgw_present(tgw_id, description) |
| 248 |
elif state == "absent": |
| 249 |
self.ensure_tgw_absent(tgw_id, description) |
| 250 |
|
| 251 |
def wait_for_status(self, wait_timeout: int, tgw_id: str, status: str, skip_deleted: bool = True) -> Dict[str, Any]: |
| 252 |
""" |
| 253 |
Wait for the Transit Gateway to reach the specified status. |
| 254 |
:param wait_timeout: Number of seconds to wait, until this timeout is reached. |
| 255 |
:param tgw_id: The Amazon NAT ID. |
| 256 |
:param status: The status to wait for. |
| 257 |
:param skip_deleted: Ignore deleted transit gateways. |
| 258 |
:return: Transit gateway object. |
| 259 |
""" |
| 260 |
polling_increment_secs = 5 |
| 261 |
max_attempts = wait_timeout // polling_increment_secs |
| 262 |
|
| 263 |
waiter_method = f"transit_gateway_{status}" |
| 264 |
|
| 265 |
wait_for_resource_state( |
| 266 |
self._connection, |
| 267 |
self._module, |
| 268 |
waiter_method, |
| 269 |
TransitGatewayIds=[tgw_id], |
| 270 |
delay=polling_increment_secs, |
| 271 |
max_attempts=max_attempts, |
| 272 |
) |
| 273 |
|
| 274 |
transit_gateway = self.get_matching_tgw(tgw_id=tgw_id, skip_deleted=skip_deleted) |
| 275 |
if transit_gateway is None: |
| 276 |
self._module.fail_json(msg="Transit Gateway not found after waiting.") |
| 277 |
|
| 278 |
return transit_gateway |
| 279 |
|
| 280 |
def get_matching_tgw( |
| 281 |
self, tgw_id: Optional[str], description: Optional[str] = None, skip_deleted: bool = True |
| 282 |
) -> Optional[Dict[str, Any]]: |
| 283 |
"""Search for an existing tgw by either tgw_id or description. |
| 284 |
:param tgw_id: The AWS id of the transit gateway. |
| 285 |
:param description: The description of the transit gateway. |
| 286 |
:param skip_deleted: Ignore deleted transit gateways. |
| 287 |
:return: Transit gateway object. |
| 288 |
""" |
| 289 |
filters = [] |
| 290 |
params = {} |
| 291 |
if tgw_id: |
| 292 |
filters = ansible_dict_to_boto3_filter_list({"transit-gateway-id": tgw_id}) |
| 293 |
|
| 294 |
params["Filters"] = filters |
| 295 |
response = describe_ec2_transit_gateways(self._connection, **params) |
| 296 |
|
| 297 |
tgw = None |
| 298 |
tgws = [] |
| 299 |
|
| 300 |
if len(response) == 1 and tgw_id: |
| 301 |
if (response[0]["State"] != "deleted") or not skip_deleted: |
| 302 |
tgws.extend(response) |
| 303 |
|
| 304 |
for gateway in response: |
| 305 |
if description == gateway.get("Description", "") and gateway["State"] != "deleted": |
| 306 |
tgws.append(gateway) |
| 307 |
|
| 308 |
if len(tgws) > 1: |
| 309 |
self._module.fail_json( |
| 310 |
msg=f"EC2 returned more than one transit Gateway for description {description}, aborting" |
| 311 |
) |
| 312 |
elif tgws: |
| 313 |
tgw = camel_dict_to_snake_dict(tgws[0], ignore_list=["Tags"]) |
| 314 |
tgw["tags"] = boto3_tag_list_to_ansible_dict(tgws[0]["Tags"]) |
| 315 |
|
| 316 |
return tgw |
| 317 |
|
| 318 |
@staticmethod |
| 319 |
def enable_option_flag(flag: Optional[bool]) -> str: |
| 320 |
disabled = "disable" |
| 321 |
enabled = "enable" |
| 322 |
return enabled if flag else disabled |
| 323 |
|
| 324 |
def create_tgw(self, description: str) -> Dict[str, Any]: |
| 325 |
""" |
| 326 |
Create a transit gateway and optionally wait for status to become available. |
| 327 |
:param description: The description of the transit gateway. |
| 328 |
:return: Transit gateway object. |
| 329 |
""" |
| 330 |
options: Dict[str, Any] = {} |
| 331 |
wait = self._module.params.get("wait") |
| 332 |
wait_timeout = self._module.params.get("wait_timeout") |
| 333 |
|
| 334 |
if self._module.params.get("asn"): |
| 335 |
options["AmazonSideAsn"] = self._module.params.get("asn") |
| 336 |
|
| 337 |
options["AutoAcceptSharedAttachments"] = self.enable_option_flag(self._module.params.get("auto_attach")) |
| 338 |
options["DefaultRouteTableAssociation"] = self.enable_option_flag(self._module.params.get("auto_associate")) |
| 339 |
options["DefaultRouteTablePropagation"] = self.enable_option_flag(self._module.params.get("auto_propagate")) |
| 340 |
options["VpnEcmpSupport"] = self.enable_option_flag(self._module.params.get("vpn_ecmp_support")) |
| 341 |
options["DnsSupport"] = self.enable_option_flag(self._module.params.get("dns_support")) |
| 342 |
options["MulticastSupport"] = self.enable_option_flag(self._module.params.get("multicast_support")) |
| 343 |
|
| 344 |
params = {"Description": description, "Options": options} |
| 345 |
|
| 346 |
response = create_ec2_transit_gateway(self._connection, **params) |
| 347 |
|
| 348 |
tgw_id = response["TransitGatewayId"] |
| 349 |
|
| 350 |
if wait: |
| 351 |
result = self.wait_for_status(wait_timeout=wait_timeout, tgw_id=tgw_id, status="available") |
| 352 |
else: |
| 353 |
result = self.get_matching_tgw(tgw_id=tgw_id) |
| 354 |
|
| 355 |
self._results["msg"] = f"Transit gateway {result['transit_gateway_id']} created" |
| 356 |
|
| 357 |
return result |
| 358 |
|
| 359 |
def delete_tgw(self, tgw_id: str) -> Dict[str, Any]: |
| 360 |
""" |
| 361 |
Delete the transit gateway and optionally wait for status to become deleted. |
| 362 |
:param tgw_id: The id of the transit gateway. |
| 363 |
:return: Transit gateway object. |
| 364 |
""" |
| 365 |
wait = self._module.params.get("wait") |
| 366 |
wait_timeout = self._module.params.get("wait_timeout") |
| 367 |
|
| 368 |
delete_ec2_transit_gateway(self._connection, tgw_id) |
| 369 |
|
| 370 |
if wait: |
| 371 |
result = self.wait_for_status( |
| 372 |
wait_timeout=wait_timeout, tgw_id=tgw_id, status="deleted", skip_deleted=False |
| 373 |
) |
| 374 |
else: |
| 375 |
result = self.get_matching_tgw(tgw_id=tgw_id, skip_deleted=False) |
| 376 |
|
| 377 |
self._results["msg"] = f"Transit gateway {tgw_id} deleted" |
| 378 |
|
| 379 |
return result |
| 380 |
|
| 381 |
def ensure_tgw_present(self, tgw_id: Optional[str] = None, description: Optional[str] = None) -> Dict[str, Any]: |
| 382 |
""" |
| 383 |
Will create a tgw if no match to the tgw_id or description are found. |
| 384 |
Will update the tgw tags if matching one found but tags are not synced. |
| 385 |
:param tgw_id: The AWS id of the transit gateway. |
| 386 |
:param description: The description of the transit gateway. |
| 387 |
:return: Transit gateway object. |
| 388 |
""" |
| 389 |
tgw = self.get_matching_tgw(tgw_id, description) |
| 390 |
|
| 391 |
if tgw is None: |
| 392 |
if self._check_mode: |
| 393 |
self._results["changed"] = True |
| 394 |
self._results["transit_gateway_id"] = None |
| 395 |
return self._results |
| 396 |
|
| 397 |
if not description: |
| 398 |
self._module.fail_json(msg="Failed to create Transit Gateway: description argument required") |
| 399 |
tgw = self.create_tgw(description) |
| 400 |
self._results["changed"] = True |
| 401 |
|
| 402 |
self._results["changed"] |= ensure_ec2_tags( |
| 403 |
self._connection, |
| 404 |
self._module, |
| 405 |
tgw["transit_gateway_id"], |
| 406 |
tags=self._module.params.get("tags"), |
| 407 |
purge_tags=self._module.params.get("purge_tags"), |
| 408 |
) |
| 409 |
|
| 410 |
self._results["transit_gateway"] = self.get_matching_tgw(tgw_id=tgw["transit_gateway_id"]) |
| 411 |
|
| 412 |
return self._results |
| 413 |
|
| 414 |
def ensure_tgw_absent(self, tgw_id: Optional[str] = None, description: Optional[str] = None) -> Dict[str, Any]: |
| 415 |
""" |
| 416 |
Will delete the tgw if a single tgw is found not yet in deleted status. |
| 417 |
:param tgw_id: The AWS id of the transit gateway. |
| 418 |
:param description: The description of the transit gateway. |
| 419 |
:return: Transit gateway object. |
| 420 |
""" |
| 421 |
self._results["transit_gateway_id"] = None |
| 422 |
tgw = self.get_matching_tgw(tgw_id, description) |
| 423 |
|
| 424 |
if tgw is not None: |
| 425 |
if self._check_mode: |
| 426 |
self._results["changed"] = True |
| 427 |
return self._results |
| 428 |
|
| 429 |
tgw = self.delete_tgw(tgw_id=tgw["transit_gateway_id"]) |
| 430 |
self._results["changed"] = True |
| 431 |
self._results["transit_gateway"] = self.get_matching_tgw( |
| 432 |
tgw_id=tgw["transit_gateway_id"], skip_deleted=False |
| 433 |
) |
| 434 |
|
| 435 |
return self._results |
| 436 |
|
| 437 |
|
| 438 |
def setup_module_object() -> AnsibleAWSModule: |
| 439 |
""" |
| 440 |
merge argument spec and create Ansible module object |
| 441 |
:return: Ansible module object |
| 442 |
""" |
| 443 |
|
| 444 |
argument_spec = dict( |
| 445 |
asn=dict(type="int"), |
| 446 |
auto_associate=dict(type="bool", default=True), |
| 447 |
auto_attach=dict(type="bool", default=False), |
| 448 |
auto_propagate=dict(type="bool", default=True), |
| 449 |
description=dict(type="str"), |
| 450 |
multicast_support=dict(type="bool"), |
| 451 |
dns_support=dict(type="bool", default=True), |
| 452 |
purge_tags=dict(type="bool", default=True), |
| 453 |
state=dict(default="present", choices=["present", "absent"]), |
| 454 |
tags=dict(type="dict", aliases=["resource_tags"]), |
| 455 |
transit_gateway_id=dict(type="str"), |
| 456 |
vpn_ecmp_support=dict(type="bool", default=True), |
| 457 |
wait=dict(type="bool", default=True), |
| 458 |
wait_timeout=dict(type="int", default=300), |
| 459 |
) |
| 460 |
|
| 461 |
module = AnsibleAWSModule( |
| 462 |
argument_spec=argument_spec, |
| 463 |
required_one_of=[("description", "transit_gateway_id")], |
| 464 |
supports_check_mode=True, |
| 465 |
) |
| 466 |
|
| 467 |
return module |
| 468 |
|
| 469 |
|
| 470 |
def main(): |
| 471 |
module = setup_module_object() |
| 472 |
|
| 473 |
results = dict(changed=False) |
| 474 |
|
| 475 |
tgw_manager = AnsibleEc2Tgw(module=module, results=results) |
| 476 |
tgw_manager.process() |
| 477 |
|
| 478 |
module.exit_json(**results) |
| 479 |
|
| 480 |
|
| 481 |
if __name__ == "__main__": |
| 482 |
main() |