| 1 |
# Copyright (c) 2023, Felix Fontein <[email protected]> |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
name: lookup |
| 9 |
author: Felix Fontein (@felixfontein) |
| 10 |
short_description: Look up DNS records |
| 11 |
version_added: 2.6.0 |
| 12 |
requirements: |
| 13 |
- dnspython >= 2.0.0 |
| 14 |
description: |
| 15 |
- Look up DNS records. |
| 16 |
options: |
| 17 |
_terms: |
| 18 |
description: |
| 19 |
- Domain name(s) to query. |
| 20 |
type: list |
| 21 |
elements: str |
| 22 |
required: true |
| 23 |
type: |
| 24 |
description: |
| 25 |
- The record type to retrieve. |
| 26 |
- Support for V(HTTPS) and V(SVCB) has been added in community.dns 3.4.0. |
| 27 |
type: str |
| 28 |
default: A |
| 29 |
choices: |
| 30 |
- A |
| 31 |
- AAAA |
| 32 |
- CAA |
| 33 |
- CNAME |
| 34 |
- DNAME |
| 35 |
- DNSKEY |
| 36 |
- DS |
| 37 |
- HINFO |
| 38 |
- HTTPS |
| 39 |
- LOC |
| 40 |
- MX |
| 41 |
- NAPTR |
| 42 |
- NS |
| 43 |
- NSEC |
| 44 |
- NSEC3 |
| 45 |
- NSEC3PARAM |
| 46 |
- PTR |
| 47 |
- RP |
| 48 |
- RRSIG |
| 49 |
- SOA |
| 50 |
- SPF |
| 51 |
- SRV |
| 52 |
- SSHFP |
| 53 |
- SVCB |
| 54 |
- TLSA |
| 55 |
- TXT |
| 56 |
query_retry: |
| 57 |
description: |
| 58 |
- Number of retries for DNS query timeouts. |
| 59 |
type: int |
| 60 |
default: 3 |
| 61 |
query_timeout: |
| 62 |
description: |
| 63 |
- Timeout per DNS query in seconds. |
| 64 |
type: float |
| 65 |
default: 10 |
| 66 |
server: |
| 67 |
description: |
| 68 |
- The DNS server(s) to use to look up the result. Must be a list of one or more IP addresses. |
| 69 |
- By default, the system's standard resolver is used. |
| 70 |
type: list |
| 71 |
elements: str |
| 72 |
servfail_retries: |
| 73 |
description: |
| 74 |
- How often to retry on SERVFAIL errors. |
| 75 |
type: int |
| 76 |
default: 0 |
| 77 |
nxdomain_handling: |
| 78 |
description: |
| 79 |
- How to handle NXDOMAIN errors. These appear if an unknown domain name is queried. |
| 80 |
- V(empty) (default) returns an empty result for that domain name. This means that for the corresponding domain name, |
| 81 |
nothing is added to RV(_result). |
| 82 |
- V(fail) makes the lookup fail. |
| 83 |
- V(message) adds the string V(NXDOMAIN) to RV(_result). |
| 84 |
type: str |
| 85 |
choices: |
| 86 |
- empty |
| 87 |
- fail |
| 88 |
- message |
| 89 |
default: empty |
| 90 |
search: |
| 91 |
description: |
| 92 |
- If V(false), the input is assumed to be an absolute domain name. |
| 93 |
- If V(true), the input is assumed to be a relative domain name if it does not end with C(.), the search list configured |
| 94 |
in the system's resolver configuration will be used for relative names, and the resolver's domain may be added to |
| 95 |
relative names. |
| 96 |
- Note that this behavior changed in community.dns 3.0.0. In community.dns 2.x.y, O(search=false) was the only available |
| 97 |
choice. |
| 98 |
type: bool |
| 99 |
default: true |
| 100 |
version_added: 3.0.0 |
| 101 |
notes: |
| 102 |
- Note that when using this lookup plugin with V(lookup(\)), and the result is a one-element list, Ansible simply returns |
| 103 |
the one element not as a list. Since this behavior is surprising and can cause problems, it is better to use V(query(\)) |
| 104 |
instead of V(lookup(\)). See the examples and also R(Forcing lookups to return lists, query) in the Ansible documentation. |
| 105 |
""" |
| 106 |
|
| 107 |
EXAMPLES = r""" |
| 108 |
- name: Look up A (IPv4) records for example.org |
| 109 |
ansible.builtin.debug: |
| 110 |
msg: "{{ query('community.dns.lookup', 'example.org.') }}" |
| 111 |
|
| 112 |
- name: Look up AAAA (IPv6) records for example.org |
| 113 |
ansible.builtin.debug: |
| 114 |
msg: "{{ query('community.dns.lookup', 'example.org.', type='AAAA' ) }}" |
| 115 |
""" |
| 116 |
|
| 117 |
RETURN = r""" |
| 118 |
_result: |
| 119 |
description: |
| 120 |
- The records of type O(type) for all queried DNS names. |
| 121 |
- If multiple DNS names are queried in O(_terms), the resulting lists have been concatenated. |
| 122 |
type: list |
| 123 |
elements: str |
| 124 |
sample: |
| 125 |
- 127.0.0.1 |
| 126 |
""" |
| 127 |
|
| 128 |
import typing as t |
| 129 |
from collections.abc import Callable |
| 130 |
|
| 131 |
from ansible.errors import AnsibleLookupError |
| 132 |
from ansible.module_utils.common.text.converters import to_text |
| 133 |
from ansible.plugins.lookup import LookupBase |
| 134 |
|
| 135 |
from ansible_collections.community.dns.plugins.module_utils._dnspython_records import ( |
| 136 |
NAME_TO_RDTYPE, |
| 137 |
NAME_TO_REQUIRED_VERSION, |
| 138 |
) |
| 139 |
from ansible_collections.community.dns.plugins.module_utils._ips import is_ip_address |
| 140 |
from ansible_collections.community.dns.plugins.module_utils._resolver import ( |
| 141 |
SimpleResolver, |
| 142 |
) |
| 143 |
from ansible_collections.community.dns.plugins.plugin_utils._resolver import ( |
| 144 |
assert_requirements_present as assert_requirements_present_dnspython, |
| 145 |
) |
| 146 |
from ansible_collections.community.dns.plugins.plugin_utils._resolver import ( |
| 147 |
guarded_run, |
| 148 |
) |
| 149 |
|
| 150 |
try: |
| 151 |
import dns.resolver |
| 152 |
from dns.rdatatype import RdataType |
| 153 |
from dns.resolver import NXDOMAIN |
| 154 |
except ImportError: |
| 155 |
# handled by assert_requirements_present_dnspython |
| 156 |
pass |
| 157 |
else: |
| 158 |
RdataType = int # type: ignore # noqa: F811 |
| 159 |
|
| 160 |
|
| 161 |
class LookupModule(LookupBase): |
| 162 |
@staticmethod |
| 163 |
def _resolve( |
| 164 |
resolver: SimpleResolver, |
| 165 |
name: str, |
| 166 |
rdtype: RdataType, |
| 167 |
server_addresses: list[str] | None, |
| 168 |
nxdomain_handling: t.Literal["empty", "fail", "message"], |
| 169 |
target_can_be_relative: bool = True, |
| 170 |
search: bool = True, |
| 171 |
) -> list[str]: |
| 172 |
def callback() -> list[str]: |
| 173 |
try: |
| 174 |
rrset = resolver.resolve( |
| 175 |
name, |
| 176 |
rdtype=rdtype, |
| 177 |
server_addresses=server_addresses, |
| 178 |
nxdomain_is_empty=nxdomain_handling == "empty", |
| 179 |
target_can_be_relative=target_can_be_relative, |
| 180 |
search=search, |
| 181 |
) |
| 182 |
if not rrset: |
| 183 |
return [] |
| 184 |
return [to_text(data) for data in rrset] |
| 185 |
except dns.resolver.NXDOMAIN: |
| 186 |
if nxdomain_handling == "message": |
| 187 |
return ["NXDOMAIN"] |
| 188 |
raise AnsibleLookupError(f"Got NXDOMAIN when querying {name}") from None |
| 189 |
|
| 190 |
return guarded_run( |
| 191 |
callback, |
| 192 |
error_class=AnsibleLookupError, |
| 193 |
server=name, |
| 194 |
) |
| 195 |
|
| 196 |
@staticmethod |
| 197 |
def _get_resolver(resolver: SimpleResolver, server: str) -> Callable[[], list[str]]: |
| 198 |
def f(): |
| 199 |
try: |
| 200 |
return resolver.resolve_addresses(server) |
| 201 |
except NXDOMAIN as exc: |
| 202 |
raise AnsibleLookupError( |
| 203 |
f"Nameserver {server} does not exist ({exc})" |
| 204 |
) from None |
| 205 |
|
| 206 |
return f |
| 207 |
|
| 208 |
def run( |
| 209 |
self, terms: list[t.Any], variables: t.Any | None = None, **kwargs: t.Any |
| 210 |
) -> list[str]: |
| 211 |
assert_requirements_present_dnspython("community.dns.lookup", "lookup") |
| 212 |
|
| 213 |
self.set_options(var_options=variables, direct=kwargs) |
| 214 |
|
| 215 |
resolver = SimpleResolver( |
| 216 |
timeout=self.get_option("query_timeout"), |
| 217 |
timeout_retries=self.get_option("query_retry"), |
| 218 |
servfail_retries=self.get_option("servfail_retries"), |
| 219 |
) |
| 220 |
|
| 221 |
record_type = self.get_option("type") |
| 222 |
if record_type not in NAME_TO_RDTYPE: |
| 223 |
min_version = NAME_TO_REQUIRED_VERSION[record_type] |
| 224 |
raise AnsibleLookupError( |
| 225 |
f"Your dnspython version does not support {record_type} records. You need version {min_version} or newer." |
| 226 |
) |
| 227 |
rdtype = NAME_TO_RDTYPE[record_type] |
| 228 |
|
| 229 |
nxdomain_handling: t.Literal["empty", "fail", "message"] = self.get_option( |
| 230 |
"nxdomain_handling" |
| 231 |
) |
| 232 |
|
| 233 |
search: bool = self.get_option("search") |
| 234 |
|
| 235 |
server_addresses: list[str] | None = None |
| 236 |
if self.get_option("server"): |
| 237 |
server_addresses = [] |
| 238 |
servers: list[str] = self.get_option("server") |
| 239 |
for server in servers: |
| 240 |
if is_ip_address(server): |
| 241 |
server_addresses.append(server) |
| 242 |
continue |
| 243 |
server_addresses.extend( |
| 244 |
guarded_run( |
| 245 |
self._get_resolver(resolver, server), |
| 246 |
error_class=AnsibleLookupError, |
| 247 |
server=server, |
| 248 |
) |
| 249 |
) |
| 250 |
|
| 251 |
result = [] |
| 252 |
for name in terms: |
| 253 |
result.extend( |
| 254 |
self._resolve( |
| 255 |
resolver, |
| 256 |
to_text(name), |
| 257 |
rdtype, |
| 258 |
server_addresses, |
| 259 |
nxdomain_handling, |
| 260 |
target_can_be_relative=search, |
| 261 |
search=search, |
| 262 |
) |
| 263 |
) |
| 264 |
return result |