| 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_info |
| 10 |
short_description: Look up nameservers for a DNS name |
| 11 |
version_added: 2.6.0 |
| 12 |
description: |
| 13 |
- Retrieve all nameservers that are responsible for a DNS name. |
| 14 |
extends_documentation_fragment: |
| 15 |
- community.dns._attributes |
| 16 |
- community.dns._attributes.info_module |
| 17 |
- community.dns._attributes.idempotent_not_modify_state |
| 18 |
author: |
| 19 |
- Felix Fontein (@felixfontein) |
| 20 |
options: |
| 21 |
name: |
| 22 |
description: |
| 23 |
- A list of DNS names whose nameservers to retrieve. |
| 24 |
required: true |
| 25 |
type: list |
| 26 |
elements: str |
| 27 |
resolve_addresses: |
| 28 |
description: |
| 29 |
- Whether to resolve the nameserver names to IP addresses. |
| 30 |
type: bool |
| 31 |
default: false |
| 32 |
query_retry: |
| 33 |
description: |
| 34 |
- Number of retries for DNS query timeouts. |
| 35 |
type: int |
| 36 |
default: 3 |
| 37 |
query_timeout: |
| 38 |
description: |
| 39 |
- Timeout per DNS query in seconds. |
| 40 |
type: float |
| 41 |
default: 10 |
| 42 |
always_ask_default_resolver: |
| 43 |
description: |
| 44 |
- When set to V(true) (default), will use the default resolver to find the authoritative nameservers of a subzone. See |
| 45 |
O(server) for how to configure the default resolver. |
| 46 |
- When set to V(false), will use the authoritative nameservers of the parent zone to find the authoritative nameservers |
| 47 |
of a subzone. This only makes sense when the nameservers were recently changed and have not yet propagated. |
| 48 |
type: bool |
| 49 |
default: true |
| 50 |
servfail_retries: |
| 51 |
description: |
| 52 |
- How often to retry on SERVFAIL errors. |
| 53 |
type: int |
| 54 |
default: 0 |
| 55 |
server: |
| 56 |
description: |
| 57 |
- The DNS server(s) to use to look up the result. Must be a list of one or more IP addresses. |
| 58 |
- By default, the system's standard resolver is used. |
| 59 |
type: list |
| 60 |
elements: str |
| 61 |
version_added: 2.7.0 |
| 62 |
requirements: |
| 63 |
- dnspython >= 2.0.0 |
| 64 |
""" |
| 65 |
|
| 66 |
EXAMPLES = r""" |
| 67 |
- name: Retrieve name servers of two DNS names |
| 68 |
community.dns.nameserver_info: |
| 69 |
name: |
| 70 |
- www.example.com |
| 71 |
- example.org |
| 72 |
register: result |
| 73 |
|
| 74 |
- name: Show nameservers for www.example.com |
| 75 |
ansible.builtin.debug: |
| 76 |
msg: '{{ result.results[0].nameserver }}' |
| 77 |
""" |
| 78 |
|
| 79 |
RETURN = r""" |
| 80 |
results: |
| 81 |
description: |
| 82 |
- Information on the nameservers for every DNS name provided in O(name). |
| 83 |
returned: always |
| 84 |
type: list |
| 85 |
elements: dict |
| 86 |
contains: |
| 87 |
name: |
| 88 |
description: |
| 89 |
- The DNS name this entry is for. |
| 90 |
returned: always |
| 91 |
type: str |
| 92 |
sample: www.example.com |
| 93 |
nameservers: |
| 94 |
description: |
| 95 |
- A list of nameservers for this DNS name. |
| 96 |
returned: success |
| 97 |
type: list |
| 98 |
elements: str |
| 99 |
sample: |
| 100 |
- ns1.example.com |
| 101 |
- ns2.example.com |
| 102 |
sample: |
| 103 |
- name: www.example.com |
| 104 |
nameservers: |
| 105 |
- ns1.example.com |
| 106 |
- ns2.example.com |
| 107 |
- name: example.org |
| 108 |
nameservers: |
| 109 |
- ns1.example.org |
| 110 |
- ns2.example.org |
| 111 |
- ns3.example.org |
| 112 |
""" |
| 113 |
|
| 114 |
import typing as t |
| 115 |
|
| 116 |
from ansible.module_utils.basic import AnsibleModule |
| 117 |
|
| 118 |
from ansible_collections.community.dns.plugins.module_utils._resolver import ( |
| 119 |
ResolveDirectlyFromNameServers, |
| 120 |
assert_requirements_present, |
| 121 |
guarded_run, |
| 122 |
) |
| 123 |
|
| 124 |
|
| 125 |
def main() -> None: |
| 126 |
module = AnsibleModule( |
| 127 |
argument_spec={ |
| 128 |
"name": {"required": True, "type": "list", "elements": "str"}, |
| 129 |
"resolve_addresses": {"type": "bool", "default": False}, |
| 130 |
"query_retry": {"type": "int", "default": 3}, |
| 131 |
"query_timeout": {"type": "float", "default": 10}, |
| 132 |
"always_ask_default_resolver": {"type": "bool", "default": True}, |
| 133 |
"servfail_retries": {"type": "int", "default": 0}, |
| 134 |
"server": {"type": "list", "elements": "str"}, |
| 135 |
}, |
| 136 |
supports_check_mode=True, |
| 137 |
) |
| 138 |
assert_requirements_present(module) |
| 139 |
|
| 140 |
names = module.params["name"] |
| 141 |
resolve_addresses = module.params["resolve_addresses"] |
| 142 |
|
| 143 |
resolver = ResolveDirectlyFromNameServers( |
| 144 |
timeout=module.params["query_timeout"], |
| 145 |
timeout_retries=module.params["query_retry"], |
| 146 |
servfail_retries=module.params["servfail_retries"], |
| 147 |
always_ask_default_resolver=module.params["always_ask_default_resolver"], |
| 148 |
server_addresses=module.params["server"], |
| 149 |
) |
| 150 |
results: list[dict[str, t.Any]] = [{"name": name} for name in names] |
| 151 |
|
| 152 |
def f(): |
| 153 |
for index, name in enumerate(names): |
| 154 |
results[index]["nameservers"] = sorted( |
| 155 |
resolver.resolve_nameservers(name, resolve_addresses=resolve_addresses) |
| 156 |
) |
| 157 |
|
| 158 |
guarded_run(f, module, generate_additional_results=lambda: {"results": results}) |
| 159 |
module.exit_json(results=results) |
| 160 |
|
| 161 |
|
| 162 |
if __name__ == "__main__": |
| 163 |
main() |