| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2023, 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: route53_wait |
| 10 |
version_added: 6.3.0 |
| 11 |
short_description: wait for changes in Amazons Route 53 DNS service to propagate |
| 12 |
description: |
| 13 |
- When using M(amazon.aws.route53) with I(wait=false), this module allows to wait for the |
| 14 |
module's propagation to finish at a later point of time. |
| 15 |
options: |
| 16 |
result: |
| 17 |
aliases: |
| 18 |
- results |
| 19 |
description: |
| 20 |
- The registered result of one or multiple M(amazon.aws.route53) invocations. |
| 21 |
required: true |
| 22 |
type: dict |
| 23 |
wait_timeout: |
| 24 |
description: |
| 25 |
- How long to wait for the changes to be replicated, in seconds. |
| 26 |
- This timeout will be used for every changed result in I(result). |
| 27 |
default: 300 |
| 28 |
type: int |
| 29 |
region: |
| 30 |
description: |
| 31 |
- This setting is ignored by the module. It is only present to make it possible to |
| 32 |
have I(region) present in the module default group. |
| 33 |
type: str |
| 34 |
author: |
| 35 |
- Felix Fontein (@felixfontein) |
| 36 |
extends_documentation_fragment: |
| 37 |
- amazon.aws.common.modules |
| 38 |
- amazon.aws.boto3 |
| 39 |
""" |
| 40 |
|
| 41 |
RETURN = r""" |
| 42 |
# |
| 43 |
""" |
| 44 |
|
| 45 |
EXAMPLES = r""" |
| 46 |
# Example when using a single route53 invocation: |
| 47 |
|
| 48 |
- name: Add new.foo.com as an A record with 3 IPs |
| 49 |
amazon.aws.route53: |
| 50 |
state: present |
| 51 |
zone: foo.com |
| 52 |
record: new.foo.com |
| 53 |
type: A |
| 54 |
ttl: 7200 |
| 55 |
value: |
| 56 |
- 1.1.1.1 |
| 57 |
- 2.2.2.2 |
| 58 |
- 3.3.3.3 |
| 59 |
register: module_result |
| 60 |
|
| 61 |
# do something else |
| 62 |
|
| 63 |
- name: Wait for the changes of the above route53 invocation to propagate |
| 64 |
community.aws.route53_wait: |
| 65 |
result: "{{ module_result }}" |
| 66 |
|
| 67 |
######################################################################### |
| 68 |
# Example when using a loop over amazon.aws.route53: |
| 69 |
|
| 70 |
- name: Add various A records |
| 71 |
amazon.aws.route53: |
| 72 |
state: present |
| 73 |
zone: foo.com |
| 74 |
record: "{{ item.record }}" |
| 75 |
type: A |
| 76 |
ttl: 300 |
| 77 |
value: "{{ item.value }}" |
| 78 |
loop: |
| 79 |
- record: new.foo.com |
| 80 |
value: 1.1.1.1 |
| 81 |
- record: foo.foo.com |
| 82 |
value: 2.2.2.2 |
| 83 |
- record: bar.foo.com |
| 84 |
value: |
| 85 |
- 3.3.3.3 |
| 86 |
- 4.4.4.4 |
| 87 |
register: module_results |
| 88 |
|
| 89 |
# do something else |
| 90 |
|
| 91 |
- name: Wait for the changes of the above three route53 invocations to propagate |
| 92 |
community.aws.route53_wait: |
| 93 |
results: "{{ module_results }}" |
| 94 |
""" |
| 95 |
|
| 96 |
try: |
| 97 |
import botocore |
| 98 |
except ImportError: |
| 99 |
pass # Handled by AnsibleAWSModule |
| 100 |
|
| 101 |
from ansible.module_utils._text import to_native |
| 102 |
|
| 103 |
from ansible_collections.amazon.aws.plugins.module_utils.waiters import get_waiter |
| 104 |
|
| 105 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 106 |
|
| 107 |
WAIT_RETRY = 5 # how many seconds to wait between propagation status polls |
| 108 |
|
| 109 |
|
| 110 |
def detect_task_results(results): |
| 111 |
if "results" in results: |
| 112 |
# This must be the registered result of a loop of route53 tasks |
| 113 |
for key in ("changed", "msg", "skipped"): |
| 114 |
if key not in results: |
| 115 |
raise ValueError(f"missing {key} key") |
| 116 |
if not isinstance(results["results"], list): |
| 117 |
raise ValueError("results is present, but not a list") |
| 118 |
for index, result in enumerate(results["results"]): |
| 119 |
if not isinstance(result, dict): |
| 120 |
raise ValueError(f"result {index + 1} is not a dictionary") |
| 121 |
for key in ("changed", "failed", "ansible_loop_var", "invocation"): |
| 122 |
if key not in result: |
| 123 |
raise ValueError(f"missing {key} key for result {index + 1}") |
| 124 |
yield f" for result #{index + 1}", result |
| 125 |
return |
| 126 |
# This must be a single route53 task |
| 127 |
for key in ("changed", "failed"): |
| 128 |
if key not in results: |
| 129 |
raise ValueError(f"missing {key} key") |
| 130 |
yield "", results |
| 131 |
|
| 132 |
|
| 133 |
def main(): |
| 134 |
argument_spec = dict( |
| 135 |
result=dict(type="dict", required=True, aliases=["results"]), |
| 136 |
wait_timeout=dict(type="int", default=300), |
| 137 |
region=dict(type="str"), # ignored |
| 138 |
) |
| 139 |
|
| 140 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 141 |
|
| 142 |
result_in = module.params["result"] |
| 143 |
wait_timeout_in = module.params.get("wait_timeout") |
| 144 |
|
| 145 |
changed_results = [] |
| 146 |
try: |
| 147 |
for description, result in detect_task_results(result_in): |
| 148 |
if result.get("wait_id"): |
| 149 |
changed_results.append((description, result["wait_id"])) |
| 150 |
except ValueError as exc: |
| 151 |
module.fail_json( |
| 152 |
msg=f"The value passed as result does not seem to be a registered route53 result: {to_native(exc)}" |
| 153 |
) |
| 154 |
|
| 155 |
# connect to the route53 endpoint |
| 156 |
try: |
| 157 |
route53 = module.client("route53") |
| 158 |
except botocore.exceptions.HTTPClientError as e: |
| 159 |
module.fail_json_aws(e, msg="Failed to connect to AWS") |
| 160 |
|
| 161 |
for what, wait_id in changed_results: |
| 162 |
try: |
| 163 |
waiter = get_waiter(route53, "resource_record_sets_changed") |
| 164 |
waiter.wait( |
| 165 |
Id=wait_id, |
| 166 |
WaiterConfig=dict( |
| 167 |
Delay=WAIT_RETRY, |
| 168 |
MaxAttempts=wait_timeout_in // WAIT_RETRY, |
| 169 |
), |
| 170 |
) |
| 171 |
except botocore.exceptions.WaiterError as e: |
| 172 |
module.fail_json_aws(e, msg=f"Timeout waiting for resource records changes{what} to be applied") |
| 173 |
except ( |
| 174 |
botocore.exceptions.BotoCoreError, |
| 175 |
botocore.exceptions.ClientError, |
| 176 |
) as e: # pylint: disable=duplicate-except |
| 177 |
module.fail_json_aws(e, msg="Failed to update records") |
| 178 |
except Exception as e: |
| 179 |
module.fail_json(msg=f"Unhandled exception. ({to_native(e)})") |
| 180 |
|
| 181 |
module.exit_json(changed=False) |
| 182 |
|
| 183 |
|
| 184 |
if __name__ == "__main__": |
| 185 |
main() |