Skip to content

Commit

Permalink
dnstxt lookup - add option to return empty list. (ansible-collections…
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein authored and Dušan Markovič committed Nov 7, 2022
1 parent e2265ec commit bb01174
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/5457-dnstxt-empty.yml
@@ -0,0 +1,2 @@
bugfixes:
- dnstxt lookup plugin - add option to return empty result without empty strings, and return empty list instead of ``NXDOMAIN`` (https://github.com/ansible-collections/community.general/pull/5457, https://github.com/ansible-collections/community.general/issues/5428).
15 changes: 15 additions & 0 deletions plugins/lookup/dnstxt.py
Expand Up @@ -20,6 +20,13 @@
required: true
type: list
elements: string
real_empty:
description:
- Return empty result without empty strings, and return empty list instead of C(NXDOMAIN).
- The default for this option will likely change to C(true) in the future.
default: false
type: bool
version_added: 6.0.0
'''

EXAMPLES = """
Expand Down Expand Up @@ -76,6 +83,8 @@ def run(self, terms, variables=None, **kwargs):
if HAVE_DNS is False:
raise AnsibleError("Can't LOOKUP(dnstxt): module dns.resolver is not installed")

real_empty = self.get_option('real_empty')

ret = []
for term in terms:
domain = term.split()[0]
Expand All @@ -87,10 +96,16 @@ def run(self, terms, variables=None, **kwargs):
string.append(s[1:-1]) # Strip outside quotes on TXT rdata

except dns.resolver.NXDOMAIN:
if real_empty:
continue
string = 'NXDOMAIN'
except dns.resolver.Timeout:
if real_empty:
continue
string = ''
except dns.resolver.NoAnswer:
if real_empty:
continue
string = ''
except DNSException as e:
raise AnsibleError("dns.resolver unhandled exception %s" % to_native(e))
Expand Down

0 comments on commit bb01174

Please sign in to comment.