1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Introduce dig lookup argument fail_on_error (#4973)

with default False for backwards compatibility.

Allows fail-fast behavior on lookup failures instead of returning strings and continuing.
This commit is contained in:
Benjamin 2022-07-24 12:08:47 +02:00 committed by GitHub
parent e2426707e2
commit 2662bc881f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 7 deletions

View file

@ -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).

View file

@ -42,6 +42,15 @@ DOCUMENTATION = '''
default: false default: false
type: bool type: bool
version_added: 3.6.0 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: 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. - 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. - While the 'dig' lookup plugin supports anything which dnspython supports out of the box, only a subset can be converted into a dictionary.
@ -273,6 +282,7 @@ class LookupModule(LookupBase):
domain = None domain = None
qtype = 'A' qtype = 'A'
flat = True flat = True
fail_on_error = False
rdclass = dns.rdataclass.from_text('IN') rdclass = dns.rdataclass.from_text('IN')
for t in terms: for t in terms:
@ -311,6 +321,8 @@ class LookupModule(LookupBase):
raise AnsibleError("dns lookup illegal CLASS: %s" % to_native(e)) raise AnsibleError("dns lookup illegal CLASS: %s" % to_native(e))
elif opt == 'retry_servfail': elif opt == 'retry_servfail':
myres.retry_servfail = bool(arg) myres.retry_servfail = bool(arg)
elif opt == 'fail_on_error':
fail_on_error = bool(arg)
continue continue
@ -353,16 +365,24 @@ class LookupModule(LookupBase):
rd['class'] = dns.rdataclass.to_text(rdata.rdclass) rd['class'] = dns.rdataclass.to_text(rdata.rdclass)
ret.append(rd) ret.append(rd)
except Exception as e: except Exception as err:
ret.append(str(e)) if fail_on_error:
raise AnsibleError("Lookup failed: %s" % str(err))
ret.append(str(err))
except dns.resolver.NXDOMAIN: except dns.resolver.NXDOMAIN as err:
if fail_on_error:
raise AnsibleError("Lookup failed: %s" % str(err))
ret.append('NXDOMAIN') 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("") 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('') ret.append('')
except dns.exception.DNSException as e: except dns.exception.DNSException as err:
raise AnsibleError("dns.resolver unhandled exception %s" % to_native(e)) raise AnsibleError("dns.resolver unhandled exception %s" % to_native(err))
return ret return ret