Skip to content

Commit

Permalink
dig lookup plugin: Fix using only last nameserver specified (#8970)
Browse files Browse the repository at this point in the history
* dig plugin: Fix using only last nameserver given

Currently, when specifying multiple nameservers
either using multiple `@ns.example.com` arguments
or by specifying multiple nameservers in a single
argument (@ns1.example.com,ns2.example.com), due
to a bug only the very last nameserver that is
specified is actually used.
This is because for every iteration of the
    for ns in nsset
loop, the local list of nameservers is cleared
and after adding the currently processed nameserver
entry, the whole `nameservers` list of the Resolver
instance is overridden with that new list with just
one element. And as far as I can see, when setting
that `nameserver` property, the dnspython library
actually overrides the existing list and doesn't
do some trickery to append the new nameservers or
something like that.

Therefore, the assignment of the `nameservers`
property of the Resolver is moved after the argument
processing so all nameservers are added and then
collectively written to the `nameservers` property
of the Resolver.

* Add CHANGELOG fragment

(cherry picked from commit 8610223)
  • Loading branch information
JaegerMaKn authored and patchback[bot] committed Oct 5, 2024
1 parent 200ab04 commit e8ee421
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/8970-fix-dig-multi-nameservers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- dig lookup plugin - fix using only the last nameserver specified (https://github.com/ansible-collections/community.general/pull/8970).
6 changes: 4 additions & 2 deletions plugins/lookup/dig.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ def run(self, terms, variables=None, **kwargs):
myres.use_edns(0, ednsflags=dns.flags.DO, payload=edns_size)

domains = []
nameservers = []
qtype = self.get_option('qtype')
flat = self.get_option('flat')
fail_on_error = self.get_option('fail_on_error')
Expand All @@ -345,7 +346,6 @@ def run(self, terms, variables=None, **kwargs):
if t.startswith('@'): # e.g. "@10.0.1.2,192.0.2.1" is ok.
nsset = t[1:].split(',')
for ns in nsset:
nameservers = []
# Check if we have a valid IP address. If so, use that, otherwise
# try to resolve name to address using system's resolver. If that
# fails we bail out.
Expand All @@ -358,7 +358,6 @@ def run(self, terms, variables=None, **kwargs):
nameservers.append(nsaddr)
except Exception as e:
raise AnsibleError("dns lookup NS: %s" % to_native(e))
myres.nameservers = nameservers
continue
if '=' in t:
try:
Expand Down Expand Up @@ -397,6 +396,9 @@ def run(self, terms, variables=None, **kwargs):

# print "--- domain = {0} qtype={1} rdclass={2}".format(domain, qtype, rdclass)

if len(nameservers) > 0:
myres.nameservers = nameservers

if qtype.upper() == 'PTR':
reversed_domains = []
for domain in domains:
Expand Down

0 comments on commit e8ee421

Please sign in to comment.