diff --git a/changelogs/fragments/4973-introduce-dig-lookup-argument.yaml b/changelogs/fragments/4973-introduce-dig-lookup-argument.yaml new file mode 100644 index 0000000000..6dcaa26000 --- /dev/null +++ b/changelogs/fragments/4973-introduce-dig-lookup-argument.yaml @@ -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). diff --git a/plugins/lookup/dig.py b/plugins/lookup/dig.py index 291bac5e45..a95aeb0189 100644 --- a/plugins/lookup/dig.py +++ b/plugins/lookup/dig.py @@ -42,6 +42,15 @@ DOCUMENTATION = ''' 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. @@ -279,6 +288,7 @@ class LookupModule(LookupBase): domain = None qtype = 'A' flat = True + fail_on_error = False rdclass = dns.rdataclass.from_text('IN') for t in terms: @@ -317,6 +327,8 @@ class LookupModule(LookupBase): 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 @@ -364,16 +376,24 @@ class LookupModule(LookupBase): rd['class'] = dns.rdataclass.to_text(rdata.rdclass) ret.append(rd) - except Exception as e: - ret.append(str(e)) + except Exception as err: + 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') - 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