Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to force dig lookup to return empty list #5439

Merged
merged 1 commit into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/5439-dig-return-empty-result.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- dig 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/5439, https://github.com/ansible-collections/community.general/issues/5428).
19 changes: 16 additions & 3 deletions plugins/lookup/dig.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@
default: false
type: bool
version_added: 5.4.0
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
notes:
- ALL is not a record per-se, merely the listed fields are available for any record results you retrieve in the form of a dictionary.
- While the 'dig' lookup plugin supports anything which dnspython supports out of the box, only a subset can be converted into a dictionary.
Expand Down Expand Up @@ -285,6 +292,7 @@ def run(self, terms, variables=None, **kwargs):
qtype = 'A'
flat = True
fail_on_error = False
real_empty = False
rdclass = dns.rdataclass.from_text('IN')

for t in terms:
Expand Down Expand Up @@ -325,6 +333,8 @@ def run(self, terms, variables=None, **kwargs):
myres.retry_servfail = boolean(arg)
elif opt == 'fail_on_error':
fail_on_error = boolean(arg)
elif opt == 'real_empty':
real_empty = boolean(arg)

continue

Expand Down Expand Up @@ -375,15 +385,18 @@ def run(self, terms, variables=None, **kwargs):
except dns.resolver.NXDOMAIN as err:
if fail_on_error:
raise AnsibleError("Lookup failed: %s" % str(err))
ret.append('NXDOMAIN')
if not real_empty:
ret.append('NXDOMAIN')
except dns.resolver.NoAnswer as err:
if fail_on_error:
raise AnsibleError("Lookup failed: %s" % str(err))
ret.append("")
if not real_empty:
ret.append("")
except dns.resolver.Timeout as err:
if fail_on_error:
raise AnsibleError("Lookup failed: %s" % str(err))
ret.append('')
if not real_empty:
ret.append("")
except dns.exception.DNSException as err:
raise AnsibleError("dns.resolver unhandled exception %s" % to_native(err))

Expand Down