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

Introduce dig lookup argument fail_on_error #4973

Merged
merged 1 commit into from
Jul 24, 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/4973-introduce-dig-lookup-argument.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- dig lookup plugin - add option ``fail_on_error`` to allow stopping execution on lookup failures (https://github.com/ansible-collections/community.general/pull/4973).
36 changes: 28 additions & 8 deletions plugins/lookup/dig.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@
default: false
type: bool
version_added: 3.6.0
fail_on_error:
description:
- Abort execution on lookup errors.
- The default for this option will likely change to C(true) in the future.
The current default, C(false), is used for backwards compatibility, and will result in empty strings
or the string C(NXDOMAIN) in the result in case of errors.
default: false
type: bool
version_added: 5.4.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 @@ -273,6 +282,7 @@ def run(self, terms, variables=None, **kwargs):
domain = None
qtype = 'A'
flat = True
fail_on_error = False
rdclass = dns.rdataclass.from_text('IN')

for t in terms:
Expand Down Expand Up @@ -311,6 +321,8 @@ def run(self, terms, variables=None, **kwargs):
raise AnsibleError("dns lookup illegal CLASS: %s" % to_native(e))
elif opt == 'retry_servfail':
myres.retry_servfail = bool(arg)
elif opt == 'fail_on_error':
fail_on_error = bool(arg)

continue

Expand Down Expand Up @@ -353,16 +365,24 @@ def run(self, terms, variables=None, **kwargs):
rd['class'] = dns.rdataclass.to_text(rdata.rdclass)

ret.append(rd)
except Exception as e:
ret.append(str(e))

except dns.resolver.NXDOMAIN:
except Exception as err:
if fail_on_error:
raise AnsibleError("Lookup failed: %s" % str(err))
ret.append(str(err))

except dns.resolver.NXDOMAIN as err:
if fail_on_error:
raise AnsibleError("Lookup failed: %s" % str(err))
ret.append('NXDOMAIN')
except dns.resolver.NoAnswer:
except dns.resolver.NoAnswer as err:
if fail_on_error:
raise AnsibleError("Lookup failed: %s" % str(err))
ret.append("")
except dns.resolver.Timeout:
except dns.resolver.Timeout as err:
if fail_on_error:
raise AnsibleError("Lookup failed: %s" % str(err))
ret.append('')
except dns.exception.DNSException as e:
raise AnsibleError("dns.resolver unhandled exception %s" % to_native(e))
except dns.exception.DNSException as err:
raise AnsibleError("dns.resolver unhandled exception %s" % to_native(err))

return ret