| 1 |
# Copyright (c) 2012, Jan-Piet Mens <jpmens(at)gmail.com> |
| 2 |
# Copyright (c) 2017 Ansible Project |
| 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 |
from __future__ import annotations |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
name: dnstxt |
| 9 |
author: Jan-Piet Mens (@jpmens) <jpmens(at)gmail.com> |
| 10 |
short_description: Query a domain(s)'s DNS txt fields |
| 11 |
requirements: |
| 12 |
- dns/dns.resolver (python library) |
| 13 |
description: |
| 14 |
- Uses a python library to return the DNS TXT record for a domain. |
| 15 |
options: |
| 16 |
_terms: |
| 17 |
description: Domain or list of domains to query TXT records from. |
| 18 |
required: true |
| 19 |
type: list |
| 20 |
elements: string |
| 21 |
real_empty: |
| 22 |
description: |
| 23 |
- Return empty result without empty strings, and return empty list instead of V(NXDOMAIN). |
| 24 |
- The default for this option is likely to change to V(true) in the future. |
| 25 |
default: false |
| 26 |
type: bool |
| 27 |
version_added: 6.0.0 |
| 28 |
""" |
| 29 |
|
| 30 |
EXAMPLES = r""" |
| 31 |
- name: show txt entry |
| 32 |
ansible.builtin.debug: |
| 33 |
msg: "{{lookup('community.general.dnstxt', ['test.example.com'])}}" |
| 34 |
|
| 35 |
- name: iterate over txt entries |
| 36 |
ansible.builtin.debug: |
| 37 |
msg: "{{item}}" |
| 38 |
with_community.general.dnstxt: |
| 39 |
- 'test.example.com' |
| 40 |
- 'other.example.com' |
| 41 |
- 'last.example.com' |
| 42 |
|
| 43 |
- name: iterate of a comma delimited DNS TXT entry |
| 44 |
ansible.builtin.debug: |
| 45 |
msg: "{{item}}" |
| 46 |
with_community.general.dnstxt: "{{lookup('community.general.dnstxt', ['test.example.com']).split(',')}}" |
| 47 |
""" |
| 48 |
|
| 49 |
RETURN = r""" |
| 50 |
_list: |
| 51 |
description: |
| 52 |
- Values returned by the DNS TXT record. |
| 53 |
type: list |
| 54 |
""" |
| 55 |
|
| 56 |
HAVE_DNS = False |
| 57 |
try: |
| 58 |
import dns.resolver |
| 59 |
from dns.exception import DNSException |
| 60 |
|
| 61 |
HAVE_DNS = True |
| 62 |
except ImportError: |
| 63 |
pass |
| 64 |
|
| 65 |
from ansible.errors import AnsibleError |
| 66 |
from ansible.plugins.lookup import LookupBase |
| 67 |
|
| 68 |
from ansible_collections.community.general.plugins.plugin_utils._lookup import check_for_wrong_terms |
| 69 |
|
| 70 |
# ============================================================== |
| 71 |
# DNSTXT: DNS TXT records |
| 72 |
# |
| 73 |
# key=domainname |
| 74 |
# TODO: configurable resolver IPs |
| 75 |
# -------------------------------------------------------------- |
| 76 |
|
| 77 |
|
| 78 |
class LookupModule(LookupBase): |
| 79 |
def run(self, terms, variables=None, **kwargs): |
| 80 |
self.set_options(var_options=variables, direct=kwargs) |
| 81 |
check_for_wrong_terms(self, direct=kwargs) |
| 82 |
|
| 83 |
if HAVE_DNS is False: |
| 84 |
raise AnsibleError("Can't LOOKUP(dnstxt): module dns.resolver is not installed") |
| 85 |
|
| 86 |
real_empty = self.get_option("real_empty") |
| 87 |
|
| 88 |
ret = [] |
| 89 |
for term in terms: |
| 90 |
domain = term.split()[0] |
| 91 |
string = [] |
| 92 |
try: |
| 93 |
answers = dns.resolver.query(domain, "TXT") |
| 94 |
for rdata in answers: |
| 95 |
s = rdata.to_text() |
| 96 |
string.append(s[1:-1]) # Strip outside quotes on TXT rdata |
| 97 |
|
| 98 |
except dns.resolver.NXDOMAIN: |
| 99 |
if real_empty: |
| 100 |
continue |
| 101 |
string = "NXDOMAIN" |
| 102 |
except dns.resolver.Timeout: |
| 103 |
if real_empty: |
| 104 |
continue |
| 105 |
string = "" |
| 106 |
except dns.resolver.NoAnswer: |
| 107 |
if real_empty: |
| 108 |
continue |
| 109 |
string = "" |
| 110 |
except DNSException as e: |
| 111 |
raise AnsibleError(f"dns.resolver unhandled exception {e}") from e |
| 112 |
|
| 113 |
ret.append("".join(string)) |
| 114 |
|
| 115 |
return ret |