Debian Codegraph @

ansible_collections/amazon/aws/plugins/modules/route53_info.py

raw
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: route53_info
9 short_description: Retrieves Route 53 details using AWS methods
10 version_added: 5.0.0
11 description:
12 - Gets various details related to Route 53 zone, record set or health check details.
13 - This module was originally added to C(community.aws) in release 1.0.0.
14 options:
15 query:
16 description:
17 - Specifies the query action to take.
18 required: true
19 choices: [
20 'change',
21 'checker_ip_range',
22 'health_check',
23 'hosted_zone',
24 'record_sets',
25 'reusable_delegation_set',
26 ]
27 type: str
28 change_id:
29 description:
30 - The ID of the change batch request.
31 - The value that you specify here is the value that
32 ChangeResourceRecordSets returned in the Id element
33 when you submitted the request.
34 - Required if O(query=change).
35 required: false
36 type: str
37 hosted_zone_id:
38 description:
39 - The Hosted Zone ID of the DNS zone.
40 - Required if O(query) is set to V(hosted_zone) and O(hosted_zone_method) is set to V(details).
41 - Required if O(query) is set to V(record_sets).
42 required: false
43 type: str
44 max_items:
45 description:
46 - Maximum number of items to return for various get/list requests.
47 required: false
48 type: int
49 next_marker:
50 description:
51 - "Some requests such as list_command: hosted_zones will return a maximum
52 number of entries - EG 100 or the number specified by O(max_items).
53 If the number of entries exceeds this maximum another request can be sent
54 using the NextMarker entry from the first response to get the next page
55 of results."
56 required: false
57 type: str
58 delegation_set_id:
59 description:
60 - The DNS Zone delegation set ID.
61 required: false
62 type: str
63 start_record_name:
64 description:
65 - "The first name in the lexicographic ordering of domain names that you want
66 the list_command: record_sets to start listing from."
67 required: false
68 type: str
69 type:
70 description:
71 - The type of DNS record.
72 required: false
73 choices: [ 'A', 'CNAME', 'MX', 'AAAA', 'TXT', 'PTR', 'SRV', 'SPF', 'CAA', 'NS', 'NAPTR', 'SOA', 'DS' ]
74 type: str
75 dns_name:
76 description:
77 - The first name in the lexicographic ordering of domain names that you want
78 the list_command to start listing from.
79 required: false
80 type: str
81 resource_id:
82 description:
83 - The ID/s of the specified resource/s.
84 - Required if O(query=health_check) and O(health_check_method=tags).
85 - Required if O(query=hosted_zone) and O(hosted_zone_method=tags).
86 required: false
87 aliases: ['resource_ids']
88 type: list
89 elements: str
90 health_check_id:
91 description:
92 - The ID of the health check.
93 - Required if V(query) is set to V(health_check) and
94 O(health_check_method) is set to V(details) or V(status) or V(failure_reason).
95 required: false
96 type: str
97 hosted_zone_method:
98 description:
99 - "This is used in conjunction with query: hosted_zone.
100 It allows for listing details, counts or tags of various
101 hosted zone details."
102 required: false
103 choices: [
104 'details',
105 'list',
106 'list_by_name',
107 'count',
108 'tags',
109 ]
110 default: 'list'
111 type: str
112 health_check_method:
113 description:
114 - "This is used in conjunction with query: health_check.
115 It allows for listing details, counts or tags of various
116 health check details."
117 required: false
118 choices: [
119 'list',
120 'details',
121 'status',
122 'failure_reason',
123 'count',
124 'tags',
125 ]
126 default: 'list'
127 type: str
128 author:
129 - Karen Cheng (@Etherdaemon)
130 extends_documentation_fragment:
131 - amazon.aws.common.modules
132 - amazon.aws.region.modules
133 - amazon.aws.boto3
134 """
135
136 EXAMPLES = r"""
137 # Simple example of listing all hosted zones
138 - name: List all hosted zones
139 amazon.aws.route53_info:
140 query: hosted_zone
141 register: hosted_zones
142
143 # Getting a count of hosted zones
144 - name: Return a count of all hosted zones
145 amazon.aws.route53_info:
146 query: hosted_zone
147 hosted_zone_method: count
148 register: hosted_zone_count
149
150 - name: List the first 20 resource record sets in a given hosted zone
151 amazon.aws.route53_info:
152 profile: account_name
153 query: record_sets
154 hosted_zone_id: ZZZ1111112222
155 max_items: 20
156 register: record_sets
157
158 - name: List first 20 health checks
159 amazon.aws.route53_info:
160 query: health_check
161 health_check_method: list
162 max_items: 20
163 register: health_checks
164
165 - name: Get health check last failure_reason
166 amazon.aws.route53_info:
167 query: health_check
168 health_check_method: failure_reason
169 health_check_id: 00000000-1111-2222-3333-12345678abcd
170 register: health_check_failure_reason
171
172 - name: Retrieve reusable delegation set details
173 amazon.aws.route53_info:
174 query: reusable_delegation_set
175 delegation_set_id: delegation id
176 register: delegation_sets
177
178 - name: setup of example for using next_marker
179 amazon.aws.route53_info:
180 query: hosted_zone
181 max_items: 1
182 register: first_info
183
184 - name: example for using next_marker
185 amazon.aws.route53_info:
186 query: hosted_zone
187 next_marker: "{{ first_info.NextMarker }}"
188 max_items: 1
189 when: "'NextMarker' in first_info"
190
191 - name: retrieve host entries starting with host1.workshop.test.io
192 block:
193 - name: grab zone id
194 amazon.aws.route53_zone:
195 zone: "test.io"
196 register: AWSINFO
197
198 - name: grab Route53 record information
199 amazon.aws.route53_info:
200 type: A
201 query: record_sets
202 hosted_zone_id: "{{ AWSINFO.zone_id }}"
203 start_record_name: "host1.workshop.test.io"
204 register: RECORDS
205 """
206
207 RETURN = r"""
208 resource_record_sets:
209 description: A list of resource record sets returned by list_resource_record_sets in boto3.
210 returned: when O(query=record_sets)
211 type: list
212 elements: dict
213 contains:
214 name:
215 description: The name of a record in the specified hosted zone.
216 type: str
217 sample: 'www.example.com'
218 type:
219 description: The DNS record type.
220 type: str
221 sample: 'A'
222 ttl:
223 description: The resource record cache time to live (TTL), in seconds.
224 type: int
225 sample: 60
226 set_identifier:
227 description: An identifier that differentiates among multiple resource record sets that have the same combination of name and type.
228 type: str
229 sample: 'abcd'
230 resource_records:
231 description: Information about the resource records.
232 type: list
233 elements: dict
234 contains:
235 value:
236 description: The current or new DNS record value.
237 type: str
238 sample: 'ns-12.awsdns-34.com.'
239 geo_location:
240 description: The specified geographic location for which the Route53 responds to based on location.
241 type: dict
242 elements: str
243 contains:
244 continent_code:
245 description: The two-letter code for the continent.
246 type: str
247 sample: 'NA'
248 country_code:
249 description: The two-letter code for a country.
250 type: str
251 sample: 'US'
252 subdivision_code:
253 description: The two-letter code for a state of the United States
254 type: str
255 sample: 'NY'
256 version_added: 4.0.0
257 version_added_collection: community.aws
258 hosted_zones:
259 description: A list of hosted zones returned by list_hosted_zones in boto3.
260 returned: when O(query=hosted_zone)
261 type: list
262 elements: dict
263 contains:
264 id:
265 description: The ID of the hosted zone assigned by Amazon Route53 to the hosted zone at the creation time.
266 type: str
267 sample: '/hostedzone/Z01234567AB1234567890'
268 name:
269 description: The name of the domain.
270 type: str
271 sample: 'example.io'
272 resource_record_set_count:
273 description: The number of resource record sets in the hosted zone.
274 type: int
275 sample: 3
276 caller_reference:
277 description: The value specified for CallerReference at the time of hosted zone creation.
278 type: str
279 sample: '01d0db12-x0x9-12a3-1234-0z000z00zz0z'
280 config:
281 description: A dict that contains Comment and PrivateZone elements.
282 type: dict
283 contains:
284 comment:
285 description: Any comments that included about in the hosted zone.
286 type: str
287 sample: 'HostedZone created by Route53 Registrar'
288 private_zone:
289 description: A value that indicates whether this is a private hosted zone or not.
290 type: bool
291 sample: false
292 version_added: 4.0.0
293 version_added_collection: community.aws
294 health_checks:
295 description: A list of Route 53 health checks returned by list_health_checks in boto3.
296 type: list
297 elements: dict
298 returned: when O(query=health_check)
299 contains:
300 id:
301 description: The identifier that Amazon Route53 assigned to the health check at the time of creation.
302 type: str
303 sample: '12345cdc-2cc4-1234-bed2-123456abc1a2'
304 health_check_version:
305 description: The version of the health check.
306 type: str
307 sample: 1
308 caller_reference:
309 description: A unique string that you specified when you created the health check.
310 type: str
311 sample: '01d0db12-x0x9-12a3-1234-0z000z00zz0z'
312 health_check_config:
313 description: A dict that contains detailed information about one health check.
314 type: dict
315 contains:
316 disabled:
317 description: Whether Route53 should stop performing health checks on a endpoint.
318 type: bool
319 sample: false
320 enable_sni:
321 description: Whether Route53 should send value of FullyQualifiedDomainName to endpoint in client_hello message during TLS negotiation.
322 type: bool
323 sample: true
324 failure_threshold:
325 description: The number of consecutive health checks that an endpoint must pass/fail for Route 53 to change current status of endpoint.
326 type: int
327 sample: 3
328 fully_qualified_domain_name:
329 description: The fully qualified DNS name of the endpoint on which Route53 performs health checks.
330 type: str
331 sample: 'hello'
332 inverted:
333 description: Whether Route53 should invert the status of a health check.
334 type: bool
335 sample: false
336 ip_address:
337 description: The IPv4/IPv6 IP address of the endpoint that Route53 should perform health checks on.
338 type: str
339 sample: 192.0.2.44
340 measure_latency:
341 description: Whether Route53 should measure latency between health checkers in multiple AWS regions and the endpoint.
342 type: bool
343 sample: false
344 port:
345 description: The port of the endpoint that Route53 should perform health checks on.
346 type: int
347 sample: 80
348 request_interval:
349 description: The number of seconds between the time that Route53 gets a response from endpoint and the next health check request.
350 type: int
351 sample: 30
352 resource_path:
353 description: The path that Route53 requests when performing health checks.
354 type: str
355 sample: '/welcome.html'
356 search_string:
357 description: The string that Route 53 uses to search for in the response body from specified resource.
358 type: str
359 sample: 'test-string-to-match'
360 type:
361 description: The type of the health check.
362 type: str
363 sample: HTTPS
364 version_added: 4.0.0
365 version_added_collection: community.aws
366 checker_ip_ranges:
367 description: A list of IP ranges in CIDR format for Amazon Route 53 health checkers.
368 returned: when O(query=checker_ip_range)
369 type: list
370 elements: str
371 version_added: 4.1.0
372 version_added_collection: community.aws
373 delegation_sets:
374 description: A list of dicts that contains information about the reusable delegation set.
375 returned: when O(query=reusable_delegation_set)
376 type: list
377 elements: dict
378 version_added: 4.1.0
379 version_added_collection: community.aws
380 health_check:
381 description: A dict of Route53 health check details returned by get_health_check in boto3.
382 type: dict
383 returned: when O(query=health_check) and O(health_check_method=details)
384 contains:
385 id:
386 description: The identifier that Amazon Route 53 assigned to the health check at the time of creation.
387 type: str
388 sample: '12345cdc-2cc4-1234-bed2-123456abc1a2'
389 health_check_version:
390 description: The version of the health check.
391 type: str
392 sample: 1
393 caller_reference:
394 description: A unique string that you specified when you created the health check.
395 type: str
396 sample: '01d0db12-x0x9-12a3-1234-0z000z00zz0z'
397 health_check_config:
398 description: A dict that contains detailed information about one health check.
399 type: dict
400 contains:
401 disabled:
402 description: Whether Route53 should stop performing health checks on a endpoint.
403 type: bool
404 sample: false
405 enable_sni:
406 description: Whether Route53 should send value of FullyQualifiedDomainName to endpoint in client_hello message during TLS negotiation.
407 type: bool
408 sample: true
409 failure_threshold:
410 description: The number of consecutive health checks that an endpoint must pass/fail for Route53 to change current status of endpoint.
411 type: int
412 sample: 3
413 fully_qualified_domain_name:
414 description: The fully qualified DNS name of the endpoint on which Route53 performs health checks.
415 type: str
416 sample: 'hello'
417 inverted:
418 description: Whether Route53 should invert the status of a health check.
419 type: bool
420 sample: false
421 ip_address:
422 description: The IPv4/IPv6 IP address of the endpoint that Route53 should perform health checks on.
423 type: str
424 sample: 192.0.2.44
425 measure_latency:
426 description: Whether Route53 should measure latency between health checkers in multiple AWS regions and the endpoint.
427 type: bool
428 sample: false
429 port:
430 description: The port of the endpoint that Route53 should perform health checks on.
431 type: int
432 sample: 80
433 request_interval:
434 description: The number of seconds between the time that Route53 gets a response from endpoint and the next health check request.
435 type: int
436 sample: 30
437 resource_path:
438 description: The path that Route53 requests when performing health checks.
439 type: str
440 sample: '/welcome.html'
441 search_string:
442 description: The string that Route53 uses to search for in the response body from specified resource.
443 type: str
444 sample: 'test-string-to-match'
445 type:
446 description: The type of the health check.
447 type: str
448 sample: HTTPS
449 version_added: 4.1.0
450 version_added_collection: community.aws
451 health_check_observations:
452 description: A dict of Route53 health check details returned by get_health_check_status and get_health_check_last_failure_reason in boto3.
453 type: list
454 elements: dict
455 returned: when O(query=health_check) and O(health_check_method=status) or O(health_check_method=failure_reason)
456 contains:
457 ip_address:
458 description: The IP address of the Amazon Route 53 health checker that provided the failure reason in StatusReport.
459 type: str
460 sample: '12.345.67.89'
461 region:
462 description: The region of the Amazon Route 53 health checker that provided the status in StatusReport.
463 type: str
464 sample: 'us-west-1'
465 status_report:
466 description: A complex type that contains the last failure reason and the time of the failed health check.
467 type: dict
468 contains:
469 checked_time:
470 description: The date and time that the health checker performed the health check in ISO 8601 format and Coordinated Universal Time (UTC).
471 type: str
472 sample: '2023-03-08T23:10:08.452000+00:00'
473 status:
474 description: A description of the status of the health check endpoint as reported by one of the Amazon Route 53 health checkers.
475 type: str
476 sample: 'Failure: Resolved IP: 12.345.67.89. The connection was closed by the endpoint.'
477 version_added: 5.4.0
478 """
479
480 try:
481 import botocore
482 except ImportError:
483 pass # Handled by AnsibleAWSModule
484
485 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
486
487 from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule
488 from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry
489
490
491 # Split out paginator to allow for the backoff decorator to function
492 @AWSRetry.jittered_backoff()
493 def _paginated_result(paginator_name, **params):
494 paginator = client.get_paginator(paginator_name)
495 return paginator.paginate(**params).build_full_result()
496
497
498 def get_hosted_zone():
499 params = dict()
500
501 if module.params.get("hosted_zone_id"):
502 params["Id"] = module.params.get("hosted_zone_id")
503 else:
504 module.fail_json(msg="Hosted Zone Id is required")
505
506 return client.get_hosted_zone(**params)
507
508
509 def reusable_delegation_set_details():
510 params = dict()
511
512 if not module.params.get("delegation_set_id"):
513 if module.params.get("max_items"):
514 params["MaxItems"] = str(module.params.get("max_items"))
515
516 if module.params.get("next_marker"):
517 params["Marker"] = module.params.get("next_marker")
518
519 results = client.list_reusable_delegation_sets(**params)
520 else:
521 params["DelegationSetId"] = module.params.get("delegation_set_id")
522 results = client.get_reusable_delegation_set(**params)
523
524 return {"delegation_sets": results["DelegationSets"]}
525
526
527 def list_hosted_zones():
528 params = dict()
529
530 # Set PaginationConfig with max_items
531 if module.params.get("max_items"):
532 params["PaginationConfig"] = dict(MaxItems=module.params.get("max_items"))
533
534 if module.params.get("next_marker"):
535 params["Marker"] = module.params.get("next_marker")
536
537 if module.params.get("delegation_set_id"):
538 params["DelegationSetId"] = module.params.get("delegation_set_id")
539
540 zones = _paginated_result("list_hosted_zones", **params)["HostedZones"]
541 snaked_zones = [camel_dict_to_snake_dict(zone) for zone in zones]
542
543 return {"hosted_zones": snaked_zones}
544
545
546 def list_hosted_zones_by_name():
547 params = dict()
548
549 if module.params.get("hosted_zone_id"):
550 params["HostedZoneId"] = module.params.get("hosted_zone_id")
551
552 if module.params.get("dns_name"):
553 params["DNSName"] = module.params.get("dns_name")
554
555 if module.params.get("max_items"):
556 params["MaxItems"] = str(module.params.get("max_items"))
557
558 zones = client.list_hosted_zones_by_name(**params)["HostedZones"]
559 snaked_zones = [camel_dict_to_snake_dict(zone) for zone in zones]
560
561 return {"hosted_zones": snaked_zones}
562
563
564 def change_details():
565 params = dict()
566
567 if module.params.get("change_id"):
568 params["Id"] = module.params.get("change_id")
569 else:
570 module.fail_json(msg="change_id is required")
571
572 results = client.get_change(**params)
573 return results
574
575
576 def checker_ip_range_details():
577 results = client.get_checker_ip_ranges()
578 return {"checker_ip_ranges": results["CheckerIpRanges"]}
579
580
581 def get_count():
582 if module.params.get("query") == "health_check":
583 results = client.get_health_check_count()
584 else:
585 results = client.get_hosted_zone_count()
586
587 return results
588
589
590 def get_health_check():
591 params = dict()
592 results = dict()
593
594 if not module.params.get("health_check_id"):
595 module.fail_json(msg="health_check_id is required")
596 else:
597 params["HealthCheckId"] = module.params.get("health_check_id")
598
599 if module.params.get("health_check_method") == "details":
600 response = client.get_health_check(**params)
601 results["health_check"] = camel_dict_to_snake_dict(response["HealthCheck"])
602
603 elif module.params.get("health_check_method") == "failure_reason":
604 response = client.get_health_check_last_failure_reason(**params)
605 results["health_check_observations"] = [
606 camel_dict_to_snake_dict(health_check) for health_check in response["HealthCheckObservations"]
607 ]
608
609 elif module.params.get("health_check_method") == "status":
610 response = client.get_health_check_status(**params)
611 results["health_check_observations"] = [
612 camel_dict_to_snake_dict(health_check) for health_check in response["HealthCheckObservations"]
613 ]
614
615 return results
616
617
618 def get_resource_tags():
619 params = dict()
620
621 if module.params.get("resource_id"):
622 params["ResourceIds"] = module.params.get("resource_id")
623 else:
624 module.fail_json(msg="resource_id or resource_ids is required")
625
626 if module.params.get("query") == "health_check":
627 params["ResourceType"] = "healthcheck"
628 else:
629 params["ResourceType"] = "hostedzone"
630
631 return client.list_tags_for_resources(**params)
632
633
634 def list_health_checks():
635 params = dict()
636
637 if module.params.get("next_marker"):
638 params["Marker"] = module.params.get("next_marker")
639
640 # Set PaginationConfig with max_items
641 if module.params.get("max_items"):
642 params["PaginationConfig"] = dict(MaxItems=module.params.get("max_items"))
643
644 health_checks = _paginated_result("list_health_checks", **params)["HealthChecks"]
645 snaked_health_checks = [camel_dict_to_snake_dict(health_check) for health_check in health_checks]
646
647 return {"health_checks": snaked_health_checks}
648
649
650 def record_sets_details():
651 params = dict()
652
653 if module.params.get("hosted_zone_id"):
654 params["HostedZoneId"] = module.params.get("hosted_zone_id")
655 else:
656 module.fail_json(msg="Hosted Zone Id is required")
657
658 if module.params.get("start_record_name"):
659 params["StartRecordName"] = module.params.get("start_record_name")
660
661 # Check that both params are set if type is applied
662 if module.params.get("type") and not module.params.get("start_record_name"):
663 module.fail_json(msg="start_record_name must be specified if type is set")
664
665 if module.params.get("type"):
666 params["StartRecordType"] = module.params.get("type")
667
668 # Set PaginationConfig with max_items
669 if module.params.get("max_items"):
670 params["PaginationConfig"] = dict(MaxItems=module.params.get("max_items"))
671
672 record_sets = _paginated_result("list_resource_record_sets", **params)["ResourceRecordSets"]
673 snaked_record_sets = [camel_dict_to_snake_dict(record_set) for record_set in record_sets]
674
675 return {"resource_record_sets": snaked_record_sets}
676
677
678 def health_check_details():
679 health_check_invocations = {
680 "list": list_health_checks,
681 "details": get_health_check,
682 "status": get_health_check,
683 "failure_reason": get_health_check,
684 "count": get_count,
685 "tags": get_resource_tags,
686 }
687
688 results = health_check_invocations[module.params.get("health_check_method")]()
689 return results
690
691
692 def hosted_zone_details():
693 hosted_zone_invocations = {
694 "details": get_hosted_zone,
695 "list": list_hosted_zones,
696 "list_by_name": list_hosted_zones_by_name,
697 "count": get_count,
698 "tags": get_resource_tags,
699 }
700
701 results = hosted_zone_invocations[module.params.get("hosted_zone_method")]()
702 return results
703
704
705 def main():
706 global module
707 global client
708
709 argument_spec = dict(
710 query=dict(
711 choices=[
712 "change",
713 "checker_ip_range",
714 "health_check",
715 "hosted_zone",
716 "record_sets",
717 "reusable_delegation_set",
718 ],
719 required=True,
720 ),
721 change_id=dict(),
722 hosted_zone_id=dict(),
723 max_items=dict(type="int"),
724 next_marker=dict(),
725 delegation_set_id=dict(),
726 start_record_name=dict(),
727 type=dict(
728 type="str",
729 choices=["A", "CNAME", "MX", "AAAA", "TXT", "PTR", "SRV", "SPF", "CAA", "NS", "NAPTR", "SOA", "DS"],
730 ),
731 dns_name=dict(),
732 resource_id=dict(type="list", aliases=["resource_ids"], elements="str"),
733 health_check_id=dict(),
734 hosted_zone_method=dict(choices=["details", "list", "list_by_name", "count", "tags"], default="list"),
735 health_check_method=dict(
736 choices=[
737 "list",
738 "details",
739 "status",
740 "failure_reason",
741 "count",
742 "tags",
743 ],
744 default="list",
745 ),
746 )
747
748 module = AnsibleAWSModule(
749 argument_spec=argument_spec,
750 supports_check_mode=True,
751 mutually_exclusive=[
752 ["hosted_zone_method", "health_check_method"],
753 ],
754 check_boto3=False,
755 )
756
757 try:
758 client = module.client("route53", retry_decorator=AWSRetry.jittered_backoff())
759 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
760 module.fail_json_aws(e, msg="Failed to connect to AWS")
761
762 invocations = {
763 "change": change_details,
764 "checker_ip_range": checker_ip_range_details,
765 "health_check": health_check_details,
766 "hosted_zone": hosted_zone_details,
767 "record_sets": record_sets_details,
768 "reusable_delegation_set": reusable_delegation_set_details,
769 }
770
771 results = dict(changed=False)
772 try:
773 results = invocations[module.params.get("query")]()
774 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
775 module.fail_json_aws(e, msg="Query failed")
776
777 module.exit_json(**results)
778
779
780 if __name__ == "__main__":
781 main()