diff --git a/changelogs/fragments/5457-dnstxt-empty.yml b/changelogs/fragments/5457-dnstxt-empty.yml new file mode 100644 index 0000000000..db4b87bca4 --- /dev/null +++ b/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). diff --git a/plugins/lookup/dnstxt.py b/plugins/lookup/dnstxt.py index 396b93f436..55067dc82b 100644 --- a/plugins/lookup/dnstxt.py +++ b/plugins/lookup/dnstxt.py @@ -20,6 +20,13 @@ DOCUMENTATION = ''' 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 = """ @@ -76,6 +83,8 @@ class LookupModule(LookupBase): 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] @@ -87,10 +96,16 @@ class LookupModule(LookupBase): 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))