| 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: reverse_lookup |
| 9 |
author: Felix Fontein (@felixfontein) |
| 10 |
short_description: Reverse-look up IP addresses |
| 11 |
version_added: 3.1.0 |
| 12 |
requirements: |
| 13 |
- dnspython >= 2.0.0 |
| 14 |
description: |
| 15 |
- Look up hostnames for IP addresses using DNS reverse lookup. |
| 16 |
options: |
| 17 |
_terms: |
| 18 |
description: |
| 19 |
- IP address(es) to look up. |
| 20 |
type: list |
| 21 |
elements: str |
| 22 |
required: true |
| 23 |
query_retry: |
| 24 |
description: |
| 25 |
- Number of retries for DNS query timeouts. |
| 26 |
type: int |
| 27 |
default: 3 |
| 28 |
query_timeout: |
| 29 |
description: |
| 30 |
- Timeout per DNS query in seconds. |
| 31 |
type: float |
| 32 |
default: 10 |
| 33 |
server: |
| 34 |
description: |
| 35 |
- The DNS server(s) to use to look up the result. Must be a list of one or more IP addresses. |
| 36 |
- By default, the system's standard resolver is used. |
| 37 |
type: list |
| 38 |
elements: str |
| 39 |
servfail_retries: |
| 40 |
description: |
| 41 |
- How often to retry on SERVFAIL errors. |
| 42 |
type: int |
| 43 |
default: 0 |
| 44 |
notes: |
| 45 |
- Note that when using this lookup plugin with V(lookup(\)), and the result is a one-element list, Ansible simply returns |
| 46 |
the one element not as a list. Since this behavior is surprising and can cause problems, it is better to use V(query(\)) |
| 47 |
instead of V(lookup(\)). See the examples and also R(Forcing lookups to return lists, query) in the Ansible documentation. |
| 48 |
""" |
| 49 |
|
| 50 |
EXAMPLES = r""" |
| 51 |
- name: Look up hostname of IPv4 address |
| 52 |
ansible.builtin.debug: |
| 53 |
msg: "{{ query('community.dns.reverse_lookup', '192.168.1.1') }}" |
| 54 |
|
| 55 |
- name: Look up hostname of IPv6 address |
| 56 |
ansible.builtin.debug: |
| 57 |
msg: "{{ query('community.dns.reverse_lookup', '1:2:3::4') }}" |
| 58 |
""" |
| 59 |
|
| 60 |
RETURN = r""" |
| 61 |
_result: |
| 62 |
description: |
| 63 |
- The hostname(s) returned for the queried IP addresses. |
| 64 |
- If multiple IP addresses are queried in O(_terms), the resulting lists have been concatenated. |
| 65 |
type: list |
| 66 |
elements: str |
| 67 |
sample: |
| 68 |
- example.com |
| 69 |
- example.org |
| 70 |
""" |
| 71 |
|
| 72 |
import ipaddress |
| 73 |
import typing as t |
| 74 |
from collections.abc import Callable |
| 75 |
|
| 76 |
from ansible.errors import AnsibleLookupError |
| 77 |
from ansible.module_utils.common.text.converters import to_text |
| 78 |
from ansible.plugins.lookup import LookupBase |
| 79 |
|
| 80 |
from ansible_collections.community.dns.plugins.module_utils._ips import is_ip_address |
| 81 |
from ansible_collections.community.dns.plugins.module_utils._resolver import ( |
| 82 |
SimpleResolver, |
| 83 |
) |
| 84 |
from ansible_collections.community.dns.plugins.plugin_utils._resolver import ( |
| 85 |
assert_requirements_present as assert_requirements_present_dnspython, |
| 86 |
) |
| 87 |
from ansible_collections.community.dns.plugins.plugin_utils._resolver import ( |
| 88 |
guarded_run, |
| 89 |
) |
| 90 |
|
| 91 |
try: |
| 92 |
import dns.resolver |
| 93 |
from dns.rdatatype import RdataType |
| 94 |
from dns.resolver import NXDOMAIN |
| 95 |
except ImportError: |
| 96 |
# handled by assert_requirements_present_dnspython |
| 97 |
pass |
| 98 |
else: |
| 99 |
RdataType = int # type: ignore # noqa: F811 |
| 100 |
|
| 101 |
|
| 102 |
class LookupModule(LookupBase): |
| 103 |
@staticmethod |
| 104 |
def _resolve( |
| 105 |
resolver: SimpleResolver, |
| 106 |
name: str, |
| 107 |
rdtype: RdataType, |
| 108 |
server_addresses: list[str] | None, |
| 109 |
) -> list[str]: |
| 110 |
def callback() -> list[str]: |
| 111 |
rrset = resolver.resolve( |
| 112 |
name, |
| 113 |
rdtype=rdtype, |
| 114 |
server_addresses=server_addresses, |
| 115 |
nxdomain_is_empty=True, |
| 116 |
target_can_be_relative=False, |
| 117 |
search=False, |
| 118 |
) |
| 119 |
if not rrset: |
| 120 |
return [] |
| 121 |
return [to_text(data) for data in rrset] |
| 122 |
|
| 123 |
return guarded_run( |
| 124 |
callback, |
| 125 |
error_class=AnsibleLookupError, |
| 126 |
server=name, |
| 127 |
) |
| 128 |
|
| 129 |
@staticmethod |
| 130 |
def _get_resolver(resolver: SimpleResolver, server: str) -> Callable[[], list[str]]: |
| 131 |
def f(): |
| 132 |
try: |
| 133 |
return resolver.resolve_addresses(server) |
| 134 |
except NXDOMAIN as exc: |
| 135 |
raise AnsibleLookupError( |
| 136 |
f"Nameserver {server} does not exist ({exc})" |
| 137 |
) from None |
| 138 |
|
| 139 |
return f |
| 140 |
|
| 141 |
def run( |
| 142 |
self, terms: list[t.Any], variables: t.Any | None = None, **kwargs: t.Any |
| 143 |
) -> list[str]: |
| 144 |
assert_requirements_present_dnspython("community.dns.reverse_lookup", "lookup") |
| 145 |
|
| 146 |
self.set_options(var_options=variables, direct=kwargs) |
| 147 |
|
| 148 |
resolver = SimpleResolver( |
| 149 |
timeout=self.get_option("query_timeout"), |
| 150 |
timeout_retries=self.get_option("query_retry"), |
| 151 |
servfail_retries=self.get_option("servfail_retries"), |
| 152 |
) |
| 153 |
|
| 154 |
server_addresses: list[str] | None = None |
| 155 |
if self.get_option("server"): |
| 156 |
server_addresses = [] |
| 157 |
servers: list[str] = self.get_option("server") |
| 158 |
for server in servers: |
| 159 |
if is_ip_address(server): |
| 160 |
server_addresses.append(server) |
| 161 |
continue |
| 162 |
server_addresses.extend( |
| 163 |
guarded_run( |
| 164 |
self._get_resolver(resolver, server), |
| 165 |
error_class=AnsibleLookupError, |
| 166 |
server=server, |
| 167 |
) |
| 168 |
) |
| 169 |
|
| 170 |
ip_adresses = [] |
| 171 |
for ip_address in terms: |
| 172 |
try: |
| 173 |
ipaddr = ipaddress.ip_address(to_text(ip_address)) |
| 174 |
name = ipaddr.reverse_pointer |
| 175 |
if not name.endswith("."): |
| 176 |
name += "." |
| 177 |
else: |
| 178 |
pass # pragma: no cover |
| 179 |
ip_adresses.append(name) |
| 180 |
except Exception as e: |
| 181 |
raise AnsibleLookupError( |
| 182 |
f"Cannot parse IP address {ip_address!r}: {e}" |
| 183 |
) from e |
| 184 |
|
| 185 |
result = [] |
| 186 |
for name in ip_adresses: |
| 187 |
result.extend( |
| 188 |
self._resolve(resolver, name, dns.rdatatype.PTR, server_addresses) |
| 189 |
) |
| 190 |
return result |