From 47cc2a4e8e010bdf6b181be82489a3db9831ff8f Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Tue, 1 Nov 2022 22:19:33 +0100 Subject: [PATCH] dnstxt lookup - add option to return empty list. (#5457) --- changelogs/fragments/5457-dnstxt-empty.yml | 2 ++ plugins/lookup/dnstxt.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 changelogs/fragments/5457-dnstxt-empty.yml diff --git a/changelogs/fragments/5457-dnstxt-empty.yml b/changelogs/fragments/5457-dnstxt-empty.yml new file mode 100644 index 00000000000..db4b87bca47 --- /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 396b93f4366..55067dc82bd 100644 --- a/plugins/lookup/dnstxt.py +++ b/plugins/lookup/dnstxt.py @@ -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 = """ @@ -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] @@ -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))