Debian Codegraph @

ansible_collections/community/dns/plugins/modules/nameserver_record_info.py

raw
1 #!/usr/bin/python
2 # Copyright (c) 2022, Felix Fontein <[email protected]>
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 from __future__ import annotations
7
8 DOCUMENTATION = r"""
9 module: nameserver_record_info
10 short_description: Look up all records of a type from all nameservers for a DNS name
11 version_added: 2.6.0
12 description:
13 - Given a DNS name and a record type, will retrieve all nameservers that are responsible for this DNS name, and from them
14 all records for this name of the given type.
15 extends_documentation_fragment:
16 - community.dns._attributes
17 - community.dns._attributes.info_module
18 - community.dns._attributes.idempotent_not_modify_state
19 author:
20 - Felix Fontein (@felixfontein)
21 options:
22 name:
23 description:
24 - A list of DNS names whose nameservers to retrieve.
25 required: true
26 type: list
27 elements: str
28 type:
29 description:
30 - The record type to retrieve.
31 - Support for V(HTTPS) and V(SVCB) has been added in community.dns 3.4.0.
32 required: true
33 type: str
34 choices:
35 - A
36 - AAAA
37 - CAA
38 - CNAME
39 - DNAME
40 - DNSKEY
41 - DS
42 - HINFO
43 - HTTPS
44 - LOC
45 - MX
46 - NAPTR
47 - NS
48 - NSEC
49 - NSEC3
50 - NSEC3PARAM
51 - PTR
52 - RP
53 - RRSIG
54 - SOA
55 - SPF
56 - SRV
57 - SSHFP
58 - SVCB
59 - TLSA
60 - TXT
61 query_retry:
62 description:
63 - Number of retries for DNS query timeouts.
64 type: int
65 default: 3
66 query_timeout:
67 description:
68 - Timeout per DNS query in seconds.
69 type: float
70 default: 10
71 always_ask_default_resolver:
72 description:
73 - When set to V(true) (default), will use the default resolver to find the authoritative nameservers of a subzone. See
74 O(server) for how to configure the default resolver.
75 - When set to V(false), will use the authoritative nameservers of the parent zone to find the authoritative nameservers
76 of a subzone. This only makes sense when the nameservers were recently changed and have not yet propagated.
77 type: bool
78 default: true
79 servfail_retries:
80 description:
81 - How often to retry on SERVFAIL errors.
82 type: int
83 default: 0
84 server:
85 description:
86 - The DNS server(s) to use to look up the result. Must be a list of one or more IP addresses.
87 - By default, the system's standard resolver is used.
88 type: list
89 elements: str
90 version_added: 2.7.0
91 requirements:
92 - dnspython >= 2.0.0
93 """
94
95 EXAMPLES = r"""
96 - name: Retrieve TXT values from all nameservers for two DNS names
97 community.dns.nameserver_record_info:
98 name:
99 - www.example.com
100 - example.org
101 type: TXT
102 register: result
103
104 - name: Show TXT values for www.example.com for all nameservers
105 ansible.builtin.debug:
106 msg: '{{ result.results[0].result }}'
107 """
108
109 RETURN = r"""
110 results:
111 description:
112 - Information on the records for every DNS name provided in O(name).
113 returned: always
114 type: list
115 elements: dict
116 contains:
117 name:
118 description:
119 - The DNS name this entry is for.
120 returned: always
121 type: str
122 sample: www.example.com
123 result:
124 description:
125 - A list of values per nameserver.
126 returned: success
127 type: list
128 elements: dict
129 sample:
130 - nameserver: ns1.example.com
131 entries:
132 - X
133 - nameserver: ns2.example.com
134 entries:
135 - X
136 contains:
137 nameserver:
138 description:
139 - The nameserver.
140 returned: success
141 type: str
142 sample: ns1.example.com
143 values:
144 description:
145 - The records of type O(type).
146 - Depending on O(type), different fields are returned.
147 - For O(type=TXT) and O(type=SPF), also the concatenated value is returned as RV(results[].result[].entries[].value).
148 - B(The field has been renamed) to RV(results[].result[].entries) in community.dns 3.4.0.
149 While the old name will be around for a longer time, prefer using the new one.
150 returned: success
151 type: list
152 elements: dict
153 entries:
154 description:
155 - The records of type O(type).
156 - Depending on O(type), different fields are returned.
157 - For O(type=TXT) and O(type=SPF), also the concatenated value is returned as RV(results[].result[].entries[].value).
158 - This field has been called RV(results[].result[].values) before.
159 version_added: 3.4.0
160 returned: success
161 type: list
162 elements: dict
163 sample:
164 - address: 127.0.0.1
165 contains:
166 address:
167 description:
168 - A IPv4 respectively IPv6 address.
169 type: str
170 returned: if O(type=A) or O(type=AAAA)
171 algorithm:
172 description:
173 - The algorithm ID.
174 type: int
175 returned: if O(type=DNSKEY) or O(type=DS) or O(type=NSEC3) or O(type=NSEC3PARAM) or O(type=RRSIG) or O(type=SSHFP)
176 altitude:
177 description:
178 - The altitude.
179 type: float
180 returned: if O(type=LOC)
181 cert:
182 description:
183 - The certificate.
184 type: str
185 returned: if O(type=TLSA)
186 cpu:
187 description:
188 - The CPU.
189 type: str
190 returned: if O(type=HINFO)
191 digest:
192 description:
193 - The digest.
194 type: str
195 returned: if O(type=DS)
196 digest_type:
197 description:
198 - The digest's type.
199 type: int
200 returned: if O(type=DS)
201 exchange:
202 description:
203 - The exchange server.
204 type: str
205 returned: if O(type=MX)
206 expiration:
207 description:
208 - The expiration Unix timestamp.
209 type: int
210 returned: if O(type=RRSIG)
211 expire:
212 description:
213 - Number of seconds after which secondary name servers should stop answering request for this zone if the
214 main name server does not respond.
215 type: int
216 returned: if O(type=SOA)
217 fingerprint:
218 description:
219 - The fingerprint.
220 type: str
221 returned: if O(type=SSHFP)
222 flags:
223 description:
224 - Flags.
225 - This is actually of type C(string) for O(type=NAPTR).
226 type: int
227 returned: if O(type=CAA) or O(type=DNSKEY) or O(type=NAPTR) or O(type=NSEC3) or O(type=NSEC3PARAM)
228 fp_type:
229 description:
230 - The fingerprint's type.
231 type: int
232 returned: if O(type=SSHFP)
233 horizontal_precision:
234 description:
235 - The horizontal precision of the location.
236 type: float
237 returned: if O(type=LOC)
238 inception:
239 description:
240 - The inception Unix timestamp.
241 type: int
242 returned: if O(type=RRSIG)
243 iterations:
244 description:
245 - The number of iterations.
246 type: int
247 returned: if O(type=NSEC3) or O(type=NSEC3PARAM)
248 key:
249 description:
250 - The key.
251 type: str
252 returned: if O(type=DNSKEY)
253 key_tag:
254 description:
255 - The key's tag.
256 type: int
257 returned: if O(type=DS) or O(type=RRSIG)
258 labels:
259 description:
260 - The labels.
261 type: int
262 returned: if O(type=RRSIG)
263 latitude:
264 description:
265 - The location's latitude.
266 type: list
267 elements: int
268 returned: if O(type=LOC)
269 longitude:
270 description:
271 - The location's longitude.
272 type: list
273 elements: int
274 returned: if O(type=LOC)
275 mbox:
276 description:
277 - The mbox.
278 type: str
279 returned: if O(type=RP)
280 minimum:
281 description:
282 - Used to calculate the TTL for purposes of negative caching.
283 type: int
284 returned: if O(type=SOA)
285 mname:
286 description:
287 - Primary main name server for this zone.
288 type: str
289 returned: if O(type=SOA)
290 mtype:
291 description:
292 - The mtype.
293 type: int
294 returned: if O(type=TLSA)
295 next:
296 description:
297 - The next value.
298 type: str
299 returned: if O(type=NSEC) or O(type=NSEC3)
300 order:
301 description:
302 - The order value.
303 type: int
304 returned: if O(type=NAPTR)
305 original_ttl:
306 description:
307 - The original TTL.
308 type: int
309 returned: if O(type=RRSIG)
310 os:
311 description:
312 - The operating system.
313 type: str
314 returned: if O(type=HINFO)
315 params:
316 description:
317 - The parameters. See L(RFC 9460, https://datatracker.ietf.org/doc/rfc9460/?include_text=1) for details.
318 type: dict
319 returned: if O(type=HTTPS) or O(type=SVCB)
320 contains:
321 mandatory:
322 description:
323 - Keys of parameters that are considered mandatory.
324 type: list
325 elements: str
326 alpn:
327 description:
328 - List of Base64-encoded ALPN IDs.
329 type: list
330 elements: str
331 no-default-alpn:
332 description:
333 - The value will always be V(null) if this key is present.
334 type: raw
335 port:
336 description:
337 - A port.
338 type: int
339 ipv4hint:
340 description:
341 - A list of IPv4 addresses.
342 type: list
343 elements: str
344 ech:
345 description:
346 - A Base64-encoded ECH (Encrypted Client Hello) key.
347 type: raw
348 ipv6hint:
349 description:
350 - A list of IPv6 addresses.
351 type: list
352 elements: str
353 ohttp:
354 description:
355 - The value will always be V(null) if this key is present.
356 type: raw
357 port:
358 description:
359 - The port.
360 type: int
361 returned: if O(type=SRV)
362 preference:
363 description:
364 - The preference value for this record.
365 type: int
366 returned: if O(type=MX) or O(type=NAPTR)
367 priority:
368 description:
369 - The priority value for this record.
370 type: int
371 returned: if O(type=HTTPS), O(type=SRV), or O(type=SVCB)
372 protocol:
373 description:
374 - The protocol.
375 type: int
376 returned: if O(type=DNSKEY)
377 refresh:
378 description:
379 - Number of seconds after which secondary name servers should query the main name server for the SOA record
380 to detect zone changes.
381 type: int
382 returned: if O(type=SOA)
383 regexp:
384 description:
385 - A regular expression.
386 type: str
387 returned: if O(type=NAPTR)
388 replacement:
389 description:
390 - The replacement.
391 type: str
392 returned: if O(type=NAPTR)
393 retry:
394 description:
395 - Number of seconds after which secondary name servers should retry to request the serial number from the
396 main name server if the main name server does not respond.
397 type: int
398 returned: if O(type=SOA)
399 rname:
400 description:
401 - E-mail address of the administrator responsible for this zone.
402 type: str
403 returned: if O(type=SOA)
404 salt:
405 description:
406 - The salt.
407 type: str
408 returned: if O(type=NSEC3) or O(type=NSEC3PARAM)
409 selector:
410 description:
411 - The selector.
412 type: int
413 returned: if O(type=TLSA)
414 serial:
415 description:
416 - Serial number for this zone.
417 type: int
418 returned: if O(type=SOA)
419 service:
420 description:
421 - The service.
422 type: str
423 returned: if O(type=NAPTR)
424 signature:
425 description:
426 - The signature.
427 type: str
428 returned: if O(type=RRSIG)
429 signer:
430 description:
431 - The signer.
432 type: str
433 returned: if O(type=RRSIG)
434 size:
435 description:
436 - The size of the location.
437 type: float
438 returned: if O(type=LOC)
439 strings:
440 description:
441 - List of strings for this record.
442 - See RV(results[].result[].entries[].value) for the concatenated result.
443 type: list
444 elements: str
445 returned: if O(type=SPF) or O(type=TXT)
446 tag:
447 description:
448 - The tag.
449 type: str
450 returned: if O(type=CAA)
451 target:
452 description:
453 - The target.
454 type: str
455 returned: if O(type=CNAME), O(type=DNAME), O(type=HTTPS), O(type=NS), O(type=PTR), O(type=SRV), or O(type=SVCB)
456 txt:
457 description:
458 - The TXT value.
459 type: str
460 returned: if O(type=RP)
461 type_covered:
462 description:
463 - The type covered.
464 type: str
465 returned: if O(type=RRSIG)
466 usage:
467 description:
468 - The usage flag.
469 type: int
470 returned: if O(type=TLSA)
471 value:
472 description:
473 - The value.
474 - For O(type=SPF) or O(type=TXT), this is the concatenation of RV(results[].result[].entries[].strings).
475 type: str
476 returned: if O(type=CAA) or O(type=SPF) or O(type=TXT)
477 vertical_precision:
478 description:
479 - The vertical precision of the location.
480 type: float
481 returned: if O(type=LOC)
482 weight:
483 description:
484 - The service's weight.
485 type: int
486 returned: if O(type=SRV)
487 windows:
488 description:
489 - The windows.
490 type: str
491 returned: if O(type=NSEC) or O(type=NSEC3)
492 sample:
493 - name: www.example.com
494 result:
495 - nameserver: ns1.example.com
496 values:
497 - address: 127.0.0.1
498 - nameserver: ns2.example.com
499 values:
500 - address: 127.0.0.1
501 - name: example.org
502 result:
503 - nameserver: ns1.example.org
504 values:
505 - address: 127.0.0.1
506 - address: 127.0.0.2
507 - nameserver: ns2.example.org
508 values:
509 - address: 127.0.0.2
510 - nameserver: ns3.example.org
511 values:
512 - address: 127.0.0.1
513 """
514
515 import typing as t
516
517 from ansible.module_utils.basic import AnsibleModule
518
519 from ansible_collections.community.dns.plugins.module_utils._dnspython_records import (
520 NAME_TO_RDTYPE,
521 NAME_TO_REQUIRED_VERSION,
522 convert_rdata_to_dict,
523 )
524 from ansible_collections.community.dns.plugins.module_utils._resolver import (
525 ResolveDirectlyFromNameServers,
526 assert_requirements_present,
527 guarded_run,
528 )
529
530
531 def main() -> None:
532 module = AnsibleModule(
533 argument_spec={
534 "name": {"required": True, "type": "list", "elements": "str"},
535 "type": {
536 "required": True,
537 "type": "str",
538 "choices": [
539 "A",
540 "AAAA",
541 "CAA",
542 "CNAME",
543 "DNAME",
544 "DNSKEY",
545 "DS",
546 "HINFO",
547 "HTTPS",
548 "LOC",
549 "MX",
550 "NAPTR",
551 "NS",
552 "NSEC",
553 "NSEC3",
554 "NSEC3PARAM",
555 "PTR",
556 "RP",
557 "RRSIG",
558 "SOA",
559 "SPF",
560 "SRV",
561 "SSHFP",
562 "SVCB",
563 "TLSA",
564 "TXT",
565 ],
566 },
567 "query_retry": {"type": "int", "default": 3},
568 "query_timeout": {"type": "float", "default": 10},
569 "always_ask_default_resolver": {"type": "bool", "default": True},
570 "servfail_retries": {"type": "int", "default": 0},
571 "server": {"type": "list", "elements": "str"},
572 },
573 supports_check_mode=True,
574 )
575 assert_requirements_present(module)
576
577 names = module.params["name"]
578 record_type = module.params["type"]
579
580 resolver = ResolveDirectlyFromNameServers(
581 timeout=module.params["query_timeout"],
582 timeout_retries=module.params["query_retry"],
583 servfail_retries=module.params["servfail_retries"],
584 always_ask_default_resolver=module.params["always_ask_default_resolver"],
585 server_addresses=module.params["server"],
586 )
587 results: list[dict[str, t.Any]] = [{"name": name} for name in names]
588
589 if record_type not in NAME_TO_RDTYPE:
590 min_version = NAME_TO_REQUIRED_VERSION[record_type]
591 module.fail_json(
592 msg=f"Your dnspython version does not support {record_type} records. You need version {min_version} or newer."
593 )
594 rdtype = NAME_TO_RDTYPE[record_type]
595
596 def f():
597 for index, name in enumerate(names):
598 result = []
599 results[index]["result"] = result
600 records_for_nameservers = resolver.resolve(name, rdtype=rdtype)
601 for nameserver, records in records_for_nameservers.items():
602 ns_result = {
603 "nameserver": nameserver,
604 }
605 result.append(ns_result)
606 values = []
607 if records is not None:
608 for data in records:
609 values.append(convert_rdata_to_dict(data))
610 ns_result["values"] = values
611 ns_result["entries"] = values
612 result.sort(key=lambda v: v["nameserver"])
613
614 guarded_run(f, module, generate_additional_results=lambda: {"results": results})
615 module.exit_json(results=results)
616
617
618 if __name__ == "__main__":
619 main()