| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Contributors to the 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: route53_zone |
| 9 |
short_description: add or delete Route 53 zones |
| 10 |
version_added: 5.0.0 |
| 11 |
description: |
| 12 |
- Creates and deletes Route 53 private and public zones. |
| 13 |
- This module was originally added to C(community.aws) in release 1.0.0. |
| 14 |
options: |
| 15 |
zone: |
| 16 |
description: |
| 17 |
- "The DNS zone record (eg: foo.com.)" |
| 18 |
required: true |
| 19 |
type: str |
| 20 |
state: |
| 21 |
description: |
| 22 |
- Whether or not the zone should exist or not. |
| 23 |
default: present |
| 24 |
choices: [ "present", "absent" ] |
| 25 |
type: str |
| 26 |
vpc_id: |
| 27 |
description: |
| 28 |
- The VPC ID the zone should be a part of (if this is going to be a private zone). |
| 29 |
type: str |
| 30 |
vpc_region: |
| 31 |
description: |
| 32 |
- The VPC Region the zone should be a part of (if this is going to be a private zone). |
| 33 |
type: str |
| 34 |
vpcs: |
| 35 |
version_added: 5.3.0 |
| 36 |
description: |
| 37 |
- The VPCs the zone should be a part of (if this is going to be a private zone). |
| 38 |
type: list |
| 39 |
elements: dict |
| 40 |
suboptions: |
| 41 |
id: |
| 42 |
description: |
| 43 |
- The ID of the VPC. |
| 44 |
type: str |
| 45 |
required: true |
| 46 |
region: |
| 47 |
description: |
| 48 |
- The region of the VPC. |
| 49 |
type: str |
| 50 |
required: true |
| 51 |
comment: |
| 52 |
description: |
| 53 |
- Comment associated with the zone. |
| 54 |
default: '' |
| 55 |
type: str |
| 56 |
hosted_zone_id: |
| 57 |
description: |
| 58 |
- The unique zone identifier you want to delete or "all" if there are many zones with the same domain name. |
| 59 |
- Required if there are multiple zones identified with the above options. |
| 60 |
type: str |
| 61 |
delegation_set_id: |
| 62 |
description: |
| 63 |
- The reusable delegation set ID to be associated with the zone. |
| 64 |
- Note that you can't associate a reusable delegation set with a private hosted zone. |
| 65 |
type: str |
| 66 |
dnssec: |
| 67 |
description: |
| 68 |
- Enables DNSSEC signing in a specific hosted zone. |
| 69 |
type: bool |
| 70 |
default: false |
| 71 |
version_added: 9.2.0 |
| 72 |
extends_documentation_fragment: |
| 73 |
- amazon.aws.common.modules |
| 74 |
- amazon.aws.region.modules |
| 75 |
- amazon.aws.tags |
| 76 |
- amazon.aws.boto3 |
| 77 |
notes: |
| 78 |
- Support for O(tags) and O(purge_tags) was added in release 2.1.0. |
| 79 |
author: |
| 80 |
- "Christopher Troup (@minichate)" |
| 81 |
""" |
| 82 |
|
| 83 |
EXAMPLES = r""" |
| 84 |
- name: create a public zone |
| 85 |
amazon.aws.route53_zone: |
| 86 |
zone: example.com |
| 87 |
comment: this is an example |
| 88 |
|
| 89 |
- name: delete a public zone |
| 90 |
amazon.aws.route53_zone: |
| 91 |
zone: example.com |
| 92 |
state: absent |
| 93 |
|
| 94 |
- name: create a private zone |
| 95 |
amazon.aws.route53_zone: |
| 96 |
zone: devel.example.com |
| 97 |
vpc_id: '{{ myvpc_id }}' |
| 98 |
vpc_region: us-west-2 |
| 99 |
comment: developer domain |
| 100 |
|
| 101 |
- name: create a private zone with multiple associated VPCs |
| 102 |
amazon.aws.route53_zone: |
| 103 |
zone: crossdevel.example.com |
| 104 |
vpcs: |
| 105 |
- id: vpc-123456 |
| 106 |
region: us-west-2 |
| 107 |
- id: vpc-000001 |
| 108 |
region: us-west-2 |
| 109 |
comment: developer cross-vpc domain |
| 110 |
|
| 111 |
- name: create a public zone associated with a specific reusable delegation set |
| 112 |
amazon.aws.route53_zone: |
| 113 |
zone: example.com |
| 114 |
comment: reusable delegation set example |
| 115 |
delegation_set_id: A1BCDEF2GHIJKL |
| 116 |
|
| 117 |
- name: create a public zone with tags |
| 118 |
amazon.aws.route53_zone: |
| 119 |
zone: example.com |
| 120 |
comment: this is an example |
| 121 |
tags: |
| 122 |
Owner: Ansible Team |
| 123 |
|
| 124 |
- name: modify a public zone, removing all previous tags and adding a new one |
| 125 |
amazon.aws.route53_zone: |
| 126 |
zone: example.com |
| 127 |
comment: this is an example |
| 128 |
tags: |
| 129 |
Support: Ansible Community |
| 130 |
purge_tags: true |
| 131 |
""" |
| 132 |
|
| 133 |
RETURN = r""" |
| 134 |
comment: |
| 135 |
description: Optional hosted zone comment. |
| 136 |
returned: when hosted zone exists |
| 137 |
type: str |
| 138 |
sample: "Private zone" |
| 139 |
name: |
| 140 |
description: Hosted zone name. |
| 141 |
returned: when hosted zone exists |
| 142 |
type: str |
| 143 |
sample: "private.local." |
| 144 |
private_zone: |
| 145 |
description: Whether hosted zone is private or public. |
| 146 |
returned: when hosted zone exists |
| 147 |
type: bool |
| 148 |
sample: true |
| 149 |
vpc_id: |
| 150 |
description: Id of the first vpc attached to private hosted zone (use vpcs for associating multiple). |
| 151 |
returned: for private hosted zone |
| 152 |
type: str |
| 153 |
sample: "vpc-1d36c84f" |
| 154 |
vpc_region: |
| 155 |
description: Region of the first vpc attached to private hosted zone (use vpcs for assocaiting multiple). |
| 156 |
returned: for private hosted zone |
| 157 |
type: str |
| 158 |
sample: "eu-west-1" |
| 159 |
vpcs: |
| 160 |
version_added: 5.3.0 |
| 161 |
description: The list of VPCs attached to the private hosted zone. |
| 162 |
returned: for private hosted zone |
| 163 |
type: list |
| 164 |
elements: dict |
| 165 |
sample: "[{'id': 'vpc-123456', 'region': 'us-west-2'}]" |
| 166 |
contains: |
| 167 |
id: |
| 168 |
description: ID of the VPC. |
| 169 |
returned: for private hosted zone |
| 170 |
type: str |
| 171 |
sample: "vpc-123456" |
| 172 |
region: |
| 173 |
description: Region of the VPC. |
| 174 |
returned: for private hosted zone |
| 175 |
type: str |
| 176 |
sample: "eu-west-2" |
| 177 |
zone_id: |
| 178 |
description: Hosted zone id. |
| 179 |
returned: when hosted zone exists |
| 180 |
type: str |
| 181 |
sample: "Z6JQG9820BEFMW" |
| 182 |
delegation_set_id: |
| 183 |
description: Id of the associated reusable delegation set. |
| 184 |
returned: for public hosted zones, if they have been associated with a reusable delegation set |
| 185 |
type: str |
| 186 |
sample: "A1BCDEF2GHIJKL" |
| 187 |
dnssec: |
| 188 |
description: Information about DNSSEC for a specific hosted zone. |
| 189 |
returned: when O(state=present) and the hosted zone is public |
| 190 |
version_added: 9.2.0 |
| 191 |
type: dict |
| 192 |
contains: |
| 193 |
key_signing_key: |
| 194 |
description: The key-signing key (KSK) that the request creates. |
| 195 |
returned: when O(state=present) |
| 196 |
type: list |
| 197 |
elements: dict |
| 198 |
contains: |
| 199 |
name: |
| 200 |
description: A string used to identify a key-signing key (KSK). |
| 201 |
type: str |
| 202 |
kms_arn: |
| 203 |
description: The Amazon resource name (ARN) used to identify the customer managed key in Key Management Service (KMS). |
| 204 |
type: str |
| 205 |
flag: |
| 206 |
description: An integer that specifies how the key is used. |
| 207 |
type: int |
| 208 |
signing_algorithm_mnemonic: |
| 209 |
description: A string used to represent the signing algorithm. |
| 210 |
type: str |
| 211 |
signing_algorithm_type: |
| 212 |
description: An integer used to represent the signing algorithm. |
| 213 |
type: int |
| 214 |
digest_algorithm_mnemonic: |
| 215 |
description: A string used to represent the delegation signer digest algorithm. |
| 216 |
type: str |
| 217 |
digest_algorithm_type: |
| 218 |
description: An integer used to represent the delegation signer digest algorithm. |
| 219 |
type: int |
| 220 |
key_tag: |
| 221 |
description: An integer used to identify the DNSSEC record for the domain name. |
| 222 |
type: int |
| 223 |
digest_value: |
| 224 |
description: A cryptographic digest of a DNSKEY resource record (RR). |
| 225 |
type: str |
| 226 |
public_key: |
| 227 |
description: The public key, represented as a Base64 encoding. |
| 228 |
type: str |
| 229 |
ds_record: |
| 230 |
description: A string that represents a delegation signer (DS) record. |
| 231 |
type: str |
| 232 |
dnskey_record: |
| 233 |
description: A string that represents a DNSKEY record. |
| 234 |
type: str |
| 235 |
status: |
| 236 |
description: A string that represents the current key-signing key (KSK) status. |
| 237 |
type: str |
| 238 |
status_message: |
| 239 |
description: The status message provided for ACTION_NEEDED or INTERNAL_FAILURE statuses. |
| 240 |
type: str |
| 241 |
created_date: |
| 242 |
description: The date when the key-signing key (KSK) was created. |
| 243 |
type: str |
| 244 |
last_modified_date: |
| 245 |
description: The last time that the key-signing key (KSK) was changed. |
| 246 |
type: str |
| 247 |
sample: [{ |
| 248 |
"created_date": "2024-12-04T15:15:36.715000+00:00", |
| 249 |
"digest_algorithm_mnemonic": "SHA-256", |
| 250 |
"digest_algorithm_type": 2, |
| 251 |
"digest_value": "xxx", |
| 252 |
"dnskey_record": "xxx", |
| 253 |
"ds_record": "xxx", |
| 254 |
"flag": 257, |
| 255 |
"key_tag": 18948, |
| 256 |
"kms_arn": "arn:aws:kms:us-east-1:xxx:key/xxx", |
| 257 |
"last_modified_date": "2024-12-04T15:15:36.715000+00:00", |
| 258 |
"name": "ansible-test-44230979--ksk", |
| 259 |
"public_key": "xxxx", |
| 260 |
"signing_algorithm_mnemonic": "ECDSAP256SHA256", |
| 261 |
"signing_algorithm_type": 13, |
| 262 |
"status": "INACTIVE" |
| 263 |
}] |
| 264 |
status: |
| 265 |
description: A dictionary representing the status of DNSSEC. |
| 266 |
type: dict |
| 267 |
contains: |
| 268 |
serve_signature: |
| 269 |
description: A string that represents the current hosted zone signing status. |
| 270 |
type: str |
| 271 |
sample: { |
| 272 |
"serve_signature": "SIGNING" |
| 273 |
} |
| 274 |
tags: |
| 275 |
description: Tags associated with the zone. |
| 276 |
returned: when tags are defined |
| 277 |
type: dict |
| 278 |
""" |
| 279 |
|
| 280 |
import time |
| 281 |
from typing import Any |
| 282 |
from typing import Dict |
| 283 |
|
| 284 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 285 |
|
| 286 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 287 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 288 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 289 |
from ansible_collections.amazon.aws.plugins.module_utils.route53 import AnsibleRoute53Error |
| 290 |
from ansible_collections.amazon.aws.plugins.module_utils.route53 import get_tags |
| 291 |
from ansible_collections.amazon.aws.plugins.module_utils.route53 import manage_tags |
| 292 |
|
| 293 |
try: |
| 294 |
from botocore.exceptions import BotoCoreError |
| 295 |
from botocore.exceptions import ClientError |
| 296 |
except ImportError: |
| 297 |
pass # caught by AnsibleAWSModule |
| 298 |
|
| 299 |
|
| 300 |
@AWSRetry.jittered_backoff() |
| 301 |
def _list_zones(): |
| 302 |
paginator = client.get_paginator("list_hosted_zones") |
| 303 |
return paginator.paginate().build_full_result() |
| 304 |
|
| 305 |
|
| 306 |
def find_zones(zone_in, private_zone): |
| 307 |
try: |
| 308 |
results = _list_zones() |
| 309 |
except (BotoCoreError, ClientError) as e: |
| 310 |
module.fail_json_aws(e, msg="Could not list current hosted zones") |
| 311 |
zones = [] |
| 312 |
for r53zone in results["HostedZones"]: |
| 313 |
if r53zone["Name"] != zone_in: |
| 314 |
continue |
| 315 |
# only save zone names that match the public/private setting |
| 316 |
if (r53zone["Config"]["PrivateZone"] and private_zone) or ( |
| 317 |
not r53zone["Config"]["PrivateZone"] and not private_zone |
| 318 |
): |
| 319 |
zones.append(r53zone) |
| 320 |
|
| 321 |
return zones |
| 322 |
|
| 323 |
|
| 324 |
def get_dnssec(hosted_zone_id: str) -> Dict[str, Any]: |
| 325 |
try: |
| 326 |
return client.get_dnssec(HostedZoneId=hosted_zone_id) |
| 327 |
except (BotoCoreError, ClientError) as e: |
| 328 |
module.fail_json_aws(e, msg=f"Could not get dnssec details about hosted zone {hosted_zone_id}") |
| 329 |
|
| 330 |
|
| 331 |
def enable_hosted_zone_dnssec(zone_id: str) -> None: |
| 332 |
try: |
| 333 |
client.enable_hosted_zone_dnssec(HostedZoneId=zone_id) |
| 334 |
except (BotoCoreError, ClientError) as e: |
| 335 |
module.fail_json_aws(e, msg=f"Could not enable DNSSEC for {zone_id}") |
| 336 |
|
| 337 |
|
| 338 |
def disable_hosted_zone_dnssec(zone_id: str) -> None: |
| 339 |
try: |
| 340 |
client.disable_hosted_zone_dnssec(HostedZoneId=zone_id) |
| 341 |
except (BotoCoreError, ClientError) as e: |
| 342 |
module.fail_json_aws(e, msg=f"Could not enable DNSSEC for {zone_id}") |
| 343 |
|
| 344 |
|
| 345 |
def get_hosted_zone(hosted_zone_id: str) -> Dict[str, Any]: |
| 346 |
try: |
| 347 |
return client.get_hosted_zone(Id=hosted_zone_id) # could be in different regions or have different VPCids |
| 348 |
except (BotoCoreError, ClientError) as e: |
| 349 |
module.fail_json_aws(e, msg=f"Could not get details about hosted zone {hosted_zone_id}") |
| 350 |
|
| 351 |
|
| 352 |
def ensure_dnssec(zone_id: str) -> bool: |
| 353 |
changed = False |
| 354 |
dnssec = module.params.get("dnssec") |
| 355 |
|
| 356 |
response = get_dnssec(zone_id) |
| 357 |
dnssec_status = response["Status"]["ServeSignature"] |
| 358 |
|
| 359 |
# If get_dnssec command output returns "NOT_SIGNING", |
| 360 |
# the Domain Name System Security Extensions (DNSSEC) signing is not enabled for the |
| 361 |
# Amazon Route 53 hosted zone. |
| 362 |
if dnssec: |
| 363 |
if dnssec_status == "NOT_SIGNING": |
| 364 |
# Enable DNSSEC |
| 365 |
if not module.check_mode: |
| 366 |
enable_hosted_zone_dnssec(zone_id) |
| 367 |
changed = True |
| 368 |
elif dnssec_status == "DELETING": |
| 369 |
# DNSSEC signing is in the process of being removed for the hosted zone. |
| 370 |
module.warn( |
| 371 |
f"DNSSEC signing is in the process of being removed for the hosted zone: {zone_id}." |
| 372 |
"Could not enable it." |
| 373 |
) |
| 374 |
else: |
| 375 |
if dnssec_status == "SIGNING": |
| 376 |
# Disable DNSSEC |
| 377 |
if not module.check_mode: |
| 378 |
disable_hosted_zone_dnssec(zone_id) |
| 379 |
changed = True |
| 380 |
# if dnssec_status == "DELETING": |
| 381 |
# DNSSEC signing is in the process of being removed for the hosted zone. |
| 382 |
|
| 383 |
return changed |
| 384 |
|
| 385 |
|
| 386 |
def create(matching_zones): |
| 387 |
zone_in = module.params.get("zone").lower() |
| 388 |
vpc_id = module.params.get("vpc_id") |
| 389 |
vpc_region = module.params.get("vpc_region") |
| 390 |
vpcs = module.params.get("vpcs") or ([{"id": vpc_id, "region": vpc_region}] if vpc_id and vpc_region else None) |
| 391 |
comment = module.params.get("comment") |
| 392 |
delegation_set_id = module.params.get("delegation_set_id") |
| 393 |
tags = module.params.get("tags") |
| 394 |
purge_tags = module.params.get("purge_tags") |
| 395 |
|
| 396 |
if not zone_in.endswith("."): |
| 397 |
zone_in += "." |
| 398 |
|
| 399 |
private_zone = bool(vpcs) |
| 400 |
|
| 401 |
record = { |
| 402 |
"private_zone": private_zone, |
| 403 |
"vpc_id": vpcs and vpcs[0]["id"], # The first one for backwards compatibility |
| 404 |
"vpc_region": vpcs and vpcs[0]["region"], # The first one for backwards compatibility |
| 405 |
"vpcs": vpcs, |
| 406 |
"comment": comment, |
| 407 |
"name": zone_in, |
| 408 |
"delegation_set_id": delegation_set_id, |
| 409 |
"zone_id": None, |
| 410 |
} |
| 411 |
|
| 412 |
if private_zone: |
| 413 |
changed, result = create_or_update_private(matching_zones, record) |
| 414 |
else: |
| 415 |
changed, result = create_or_update_public(matching_zones, record) |
| 416 |
|
| 417 |
zone_id = result.get("zone_id") |
| 418 |
if zone_id: |
| 419 |
if not private_zone: |
| 420 |
# Enable/Disable DNSSEC |
| 421 |
changed |= ensure_dnssec(zone_id) |
| 422 |
|
| 423 |
# Update result with information about DNSSEC |
| 424 |
result["dnssec"] = camel_dict_to_snake_dict(get_dnssec(zone_id)) |
| 425 |
del result["dnssec"]["response_metadata"] |
| 426 |
|
| 427 |
# Handle Tags |
| 428 |
if tags is not None: |
| 429 |
changed |= manage_tags(client, "hostedzone", zone_id, tags, purge_tags, module.check_mode) |
| 430 |
result["tags"] = get_tags(client, "hostedzone", zone_id) |
| 431 |
else: |
| 432 |
result["tags"] = tags |
| 433 |
|
| 434 |
return changed, result |
| 435 |
|
| 436 |
|
| 437 |
def create_or_update_private(matching_zones, record): |
| 438 |
for z in matching_zones: |
| 439 |
result = get_hosted_zone(z["Id"]) # could be in different regions or have different VPCids |
| 440 |
zone_details = result["HostedZone"] |
| 441 |
vpc_details = result["VPCs"] |
| 442 |
matching = False |
| 443 |
if isinstance(vpc_details, dict) and len(record["vpcs"]) == 1: |
| 444 |
if vpc_details["VPC"]["VPCId"] == record["vpcs"][0]["id"]: |
| 445 |
matching = True |
| 446 |
else: |
| 447 |
# Sort the lists and compare them to make sure they contain the same items |
| 448 |
if sorted([vpc["id"] for vpc in record["vpcs"]]) == sorted([v["VPCId"] for v in vpc_details]) and sorted( |
| 449 |
[vpc["region"] for vpc in record["vpcs"]] |
| 450 |
) == sorted([v["VPCRegion"] for v in vpc_details]): |
| 451 |
matching = True |
| 452 |
|
| 453 |
if matching: |
| 454 |
record["zone_id"] = zone_details["Id"].replace("/hostedzone/", "") |
| 455 |
if "Comment" in zone_details["Config"] and zone_details["Config"]["Comment"] != record["comment"]: |
| 456 |
if not module.check_mode: |
| 457 |
try: |
| 458 |
client.update_hosted_zone_comment(Id=zone_details["Id"], Comment=record["comment"]) |
| 459 |
except (BotoCoreError, ClientError) as e: |
| 460 |
module.fail_json_aws(e, msg=f"Could not update comment for hosted zone {zone_details['Id']}") |
| 461 |
return True, record |
| 462 |
else: |
| 463 |
record["msg"] = ( |
| 464 |
"There is already a private hosted zone in the same region with the same VPC(s)" |
| 465 |
" you chose. Unable to create a new private hosted zone in the same name space." |
| 466 |
) |
| 467 |
return False, record |
| 468 |
|
| 469 |
if not module.check_mode: |
| 470 |
try: |
| 471 |
result = client.create_hosted_zone( |
| 472 |
Name=record["name"], |
| 473 |
HostedZoneConfig={ |
| 474 |
"Comment": record["comment"] if record["comment"] is not None else "", |
| 475 |
"PrivateZone": True, |
| 476 |
}, |
| 477 |
VPC={ |
| 478 |
"VPCRegion": record["vpcs"][0]["region"], |
| 479 |
"VPCId": record["vpcs"][0]["id"], |
| 480 |
}, |
| 481 |
CallerReference=f"{record['name']}-{time.time()}", |
| 482 |
) |
| 483 |
except (BotoCoreError, ClientError) as e: |
| 484 |
module.fail_json_aws(e, msg="Could not create hosted zone") |
| 485 |
|
| 486 |
hosted_zone = result["HostedZone"] |
| 487 |
zone_id = hosted_zone["Id"].replace("/hostedzone/", "") |
| 488 |
record["zone_id"] = zone_id |
| 489 |
|
| 490 |
if len(record["vpcs"]) > 1: |
| 491 |
for vpc in record["vpcs"][1:]: |
| 492 |
try: |
| 493 |
result = client.associate_vpc_with_hosted_zone( |
| 494 |
HostedZoneId=zone_id, |
| 495 |
VPC={ |
| 496 |
"VPCRegion": vpc["region"], |
| 497 |
"VPCId": vpc["id"], |
| 498 |
}, |
| 499 |
) |
| 500 |
except (BotoCoreError, ClientError) as e: |
| 501 |
module.fail_json_aws(e, msg="Could not associate additional VPCs with hosted zone") |
| 502 |
|
| 503 |
changed = True |
| 504 |
return changed, record |
| 505 |
|
| 506 |
|
| 507 |
def create_or_update_public(matching_zones, record): |
| 508 |
zone_details, zone_delegation_set_details = None, {} |
| 509 |
for matching_zone in matching_zones: |
| 510 |
zone = get_hosted_zone(matching_zone["Id"]) |
| 511 |
zone_details = zone["HostedZone"] |
| 512 |
zone_delegation_set_details = zone.get("DelegationSet", {}) |
| 513 |
if "Comment" in zone_details["Config"] and zone_details["Config"]["Comment"] != record["comment"]: |
| 514 |
if not module.check_mode: |
| 515 |
try: |
| 516 |
client.update_hosted_zone_comment(Id=zone_details["Id"], Comment=record["comment"]) |
| 517 |
except (BotoCoreError, ClientError) as e: |
| 518 |
module.fail_json_aws(e, msg=f"Could not update comment for hosted zone {zone_details['Id']}") |
| 519 |
changed = True |
| 520 |
else: |
| 521 |
changed = False |
| 522 |
break |
| 523 |
|
| 524 |
if zone_details is None: |
| 525 |
if not module.check_mode: |
| 526 |
try: |
| 527 |
params = dict( |
| 528 |
Name=record["name"], |
| 529 |
HostedZoneConfig={ |
| 530 |
"Comment": record["comment"] if record["comment"] is not None else "", |
| 531 |
"PrivateZone": False, |
| 532 |
}, |
| 533 |
CallerReference=f"{record['name']}-{time.time()}", |
| 534 |
) |
| 535 |
|
| 536 |
if record.get("delegation_set_id") is not None: |
| 537 |
params["DelegationSetId"] = record["delegation_set_id"] |
| 538 |
|
| 539 |
result = client.create_hosted_zone(**params) |
| 540 |
zone_details = result["HostedZone"] |
| 541 |
zone_delegation_set_details = result.get("DelegationSet", {}) |
| 542 |
|
| 543 |
except (BotoCoreError, ClientError) as e: |
| 544 |
module.fail_json_aws(e, msg="Could not create hosted zone") |
| 545 |
changed = True |
| 546 |
|
| 547 |
if module.check_mode: |
| 548 |
if zone_details: |
| 549 |
record["zone_id"] = zone_details["Id"].replace("/hostedzone/", "") |
| 550 |
else: |
| 551 |
record["zone_id"] = zone_details["Id"].replace("/hostedzone/", "") |
| 552 |
record["name"] = zone_details["Name"] |
| 553 |
record["delegation_set_id"] = zone_delegation_set_details.get("Id", "").replace("/delegationset/", "") |
| 554 |
|
| 555 |
return changed, record |
| 556 |
|
| 557 |
|
| 558 |
def delete_private(matching_zones, vpcs): |
| 559 |
for z in matching_zones: |
| 560 |
result = get_hosted_zone(z["Id"]) |
| 561 |
zone_details = result["HostedZone"] |
| 562 |
vpc_details = result["VPCs"] |
| 563 |
if isinstance(vpc_details, dict): |
| 564 |
if vpc_details["VPC"]["VPCId"] == vpcs[0]["id"] and vpcs[0]["region"] == vpc_details["VPC"]["VPCRegion"]: |
| 565 |
if not module.check_mode: |
| 566 |
delete_hosted_zone(z["Id"]) |
| 567 |
return True, f"Successfully deleted {zone_details['Name']}" |
| 568 |
else: |
| 569 |
# Sort the lists and compare them to make sure they contain the same items |
| 570 |
if sorted([vpc["id"] for vpc in vpcs]) == sorted([v["VPCId"] for v in vpc_details]) and sorted( |
| 571 |
[vpc["region"] for vpc in vpcs] |
| 572 |
) == sorted([v["VPCRegion"] for v in vpc_details]): |
| 573 |
if not module.check_mode: |
| 574 |
delete_hosted_zone(z["Id"]) |
| 575 |
return True, f"Successfully deleted {zone_details['Name']}" |
| 576 |
|
| 577 |
return False, "The VPCs do not match a private hosted zone." |
| 578 |
|
| 579 |
|
| 580 |
def delete_public(matching_zones): |
| 581 |
if len(matching_zones) > 1: |
| 582 |
changed = False |
| 583 |
msg = "There are multiple zones that match. Use hosted_zone_id to specify the correct zone." |
| 584 |
else: |
| 585 |
if not module.check_mode: |
| 586 |
delete_hosted_zone(matching_zones[0]["Id"]) |
| 587 |
changed = True |
| 588 |
msg = f"Successfully deleted {matching_zones[0]['Id']}" |
| 589 |
return changed, msg |
| 590 |
|
| 591 |
|
| 592 |
def delete_hosted_id(hosted_zone_id, matching_zones): |
| 593 |
if hosted_zone_id == "all": |
| 594 |
deleted = [] |
| 595 |
for z in matching_zones: |
| 596 |
deleted.append(z["Id"]) |
| 597 |
if not module.check_mode: |
| 598 |
delete_hosted_zone(z["Id"]) |
| 599 |
changed = True |
| 600 |
msg = f"Successfully deleted zones: {deleted}" |
| 601 |
elif hosted_zone_id in [zo["Id"].replace("/hostedzone/", "") for zo in matching_zones]: |
| 602 |
if not module.check_mode: |
| 603 |
delete_hosted_zone(hosted_zone_id) |
| 604 |
changed = True |
| 605 |
msg = f"Successfully deleted zone: {hosted_zone_id}" |
| 606 |
else: |
| 607 |
changed = False |
| 608 |
msg = f"There is no zone to delete that matches hosted_zone_id {hosted_zone_id}." |
| 609 |
return changed, msg |
| 610 |
|
| 611 |
|
| 612 |
def delete_hosted_zone(hosted_zone_id): |
| 613 |
try: |
| 614 |
client.delete_hosted_zone(Id=hosted_zone_id) |
| 615 |
except is_boto3_error_code("HostedZoneNotEmpty") as e: |
| 616 |
module.fail_json_aws(e, msg=f"Could not get delete hosted zone {hosted_zone_id}") |
| 617 |
except (BotoCoreError, ClientError) as e: |
| 618 |
module.fail_json_aws(e, msg=f"Could not delete hosted zone {hosted_zone_id}") |
| 619 |
|
| 620 |
|
| 621 |
def delete(matching_zones): |
| 622 |
zone_in = module.params.get("zone").lower() |
| 623 |
vpc_id = module.params.get("vpc_id") |
| 624 |
vpc_region = module.params.get("vpc_region") |
| 625 |
vpcs = module.params.get("vpcs") or ([{"id": vpc_id, "region": vpc_region}] if vpc_id and vpc_region else None) |
| 626 |
hosted_zone_id = module.params.get("hosted_zone_id") |
| 627 |
|
| 628 |
if not zone_in.endswith("."): |
| 629 |
zone_in += "." |
| 630 |
|
| 631 |
private_zone = bool(vpcs) |
| 632 |
|
| 633 |
if zone_in in [z["Name"] for z in matching_zones]: |
| 634 |
if hosted_zone_id: |
| 635 |
changed, result = delete_hosted_id(hosted_zone_id, matching_zones) |
| 636 |
else: |
| 637 |
if private_zone: |
| 638 |
changed, result = delete_private(matching_zones, vpcs) |
| 639 |
else: |
| 640 |
changed, result = delete_public(matching_zones) |
| 641 |
else: |
| 642 |
changed = False |
| 643 |
result = "No zone to delete." |
| 644 |
|
| 645 |
return changed, result |
| 646 |
|
| 647 |
|
| 648 |
def main(): |
| 649 |
global module |
| 650 |
global client |
| 651 |
|
| 652 |
argument_spec = dict( |
| 653 |
zone=dict(required=True), |
| 654 |
state=dict(default="present", choices=["present", "absent"]), |
| 655 |
vpc_id=dict(default=None), |
| 656 |
vpc_region=dict(default=None), |
| 657 |
vpcs=dict( |
| 658 |
type="list", default=None, elements="dict", options=dict(id=dict(required=True), region=dict(required=True)) |
| 659 |
), |
| 660 |
comment=dict(default=""), |
| 661 |
hosted_zone_id=dict(), |
| 662 |
delegation_set_id=dict(), |
| 663 |
tags=dict(type="dict", aliases=["resource_tags"]), |
| 664 |
purge_tags=dict(type="bool", default=True), |
| 665 |
dnssec=dict(type="bool", default=False), |
| 666 |
) |
| 667 |
|
| 668 |
mutually_exclusive = [ |
| 669 |
["delegation_set_id", "vpc_id"], |
| 670 |
["delegation_set_id", "vpc_region"], |
| 671 |
["delegation_set_id", "vpcs"], |
| 672 |
["vpcs", "vpc_id"], |
| 673 |
["vpcs", "vpc_region"], |
| 674 |
] |
| 675 |
|
| 676 |
module = AnsibleAWSModule( |
| 677 |
argument_spec=argument_spec, |
| 678 |
mutually_exclusive=mutually_exclusive, |
| 679 |
supports_check_mode=True, |
| 680 |
) |
| 681 |
|
| 682 |
try: |
| 683 |
zone_in = module.params.get("zone").lower() |
| 684 |
state = module.params.get("state").lower() |
| 685 |
vpc_id = module.params.get("vpc_id") |
| 686 |
vpc_region = module.params.get("vpc_region") |
| 687 |
vpcs = module.params.get("vpcs") |
| 688 |
|
| 689 |
if not zone_in.endswith("."): |
| 690 |
zone_in += "." |
| 691 |
|
| 692 |
private_zone = bool(vpcs or (vpc_id and vpc_region)) |
| 693 |
|
| 694 |
client = module.client("route53", retry_decorator=AWSRetry.jittered_backoff()) |
| 695 |
|
| 696 |
zones = find_zones(zone_in, private_zone) |
| 697 |
if state == "present": |
| 698 |
changed, result = create(matching_zones=zones) |
| 699 |
elif state == "absent": |
| 700 |
changed, result = delete(matching_zones=zones) |
| 701 |
|
| 702 |
if isinstance(result, dict): |
| 703 |
module.exit_json(changed=changed, result=result, **result) |
| 704 |
else: |
| 705 |
module.exit_json(changed=changed, result=result) |
| 706 |
except AnsibleRoute53Error as e: |
| 707 |
module.fail_json_aws_error(e) |
| 708 |
|
| 709 |
|
| 710 |
if __name__ == "__main__": |
| 711 |
main() |