[PR #8966/5e6b8e53 backport][stable-9] dig lookup: Allow to pass port for DNS lookup (#9004)

dig lookup: Allow to pass port for DNS lookup (#8966)

dnspython accepts a port as part of the nameserver.

Currently, the nameservers are passed as strings which
leads dnspython to create Nameserver objects out of them
using the port that is currently set in the Resolver instance.
That creation of Nameserver objects is done right when the
`nameservers` property is set.
If a port is to be set by us, the `port` attribute of the
Resolver needs to be set before the nameservers are passed
to the Resolver so when the nameservers are passed, that new
port is used to create the Nameserver objects.
Therefore, the assignment of the `nameservers` property of the
Resolver is moved after the argument processing so the `port`
attribute is (if it's given in the lookup-call) definitely set
before the `nameservers` property.

(cherry picked from commit 5e6b8e5327)

Co-authored-by: JaegerMaKn <max.jaeger@knauf.com>
pull/9014/head
patchback[bot] 2024-10-07 23:10:04 +02:00 committed by GitHub
parent 154b5f86fd
commit cc2794ad05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 0 deletions

View File

@ -0,0 +1,4 @@
---
minor_changes:
- dig lookup plugin - add ``port`` option to specify DNS server port (https://github.com/ansible-collections/community.general/pull/8966).
...

View File

@ -75,6 +75,11 @@ DOCUMENTATION = '''
default: false default: false
type: bool type: bool
version_added: 7.5.0 version_added: 7.5.0
port:
description: Use port as target port when looking up DNS records.
default: 53
type: int
version_added: 9.5.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.
@ -336,6 +341,7 @@ class LookupModule(LookupBase):
fail_on_error = self.get_option('fail_on_error') fail_on_error = self.get_option('fail_on_error')
real_empty = self.get_option('real_empty') real_empty = self.get_option('real_empty')
tcp = self.get_option('tcp') tcp = self.get_option('tcp')
port = self.get_option('port')
try: try:
rdclass = dns.rdataclass.from_text(self.get_option('class')) rdclass = dns.rdataclass.from_text(self.get_option('class'))
except Exception as e: except Exception as e:
@ -396,6 +402,8 @@ class LookupModule(LookupBase):
# print "--- domain = {0} qtype={1} rdclass={2}".format(domain, qtype, rdclass) # print "--- domain = {0} qtype={1} rdclass={2}".format(domain, qtype, rdclass)
if port:
myres.port = port
if len(nameservers) > 0: if len(nameservers) > 0:
myres.nameservers = nameservers myres.nameservers = nameservers