mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
dig lookup-plugin: add support for specifying rdclass (#10493)
* add support for specifying rdclass * fix exception
This commit is contained in:
parent
597bca3129
commit
5a0b2fe3be
1 changed files with 12 additions and 4 deletions
|
@ -26,6 +26,7 @@ try:
|
||||||
import dns.name
|
import dns.name
|
||||||
import dns.resolver
|
import dns.resolver
|
||||||
import dns.reversename
|
import dns.reversename
|
||||||
|
import dns.rdataclass
|
||||||
from dns.rdatatype import (A, AAAA, CNAME, DLV, DNAME, DNSKEY, DS, HINFO, LOC,
|
from dns.rdatatype import (A, AAAA, CNAME, DLV, DNAME, DNSKEY, DS, HINFO, LOC,
|
||||||
MX, NAPTR, NS, NSEC3PARAM, PTR, RP, SOA, SPF, SRV, SSHFP, TLSA, TXT)
|
MX, NAPTR, NS, NSEC3PARAM, PTR, RP, SOA, SPF, SRV, SSHFP, TLSA, TXT)
|
||||||
HAVE_DNS = True
|
HAVE_DNS = True
|
||||||
|
@ -120,19 +121,20 @@ class LookupModule(LookupBase):
|
||||||
raise AnsibleError("Can't LOOKUP(dig): module dns.resolver is not installed")
|
raise AnsibleError("Can't LOOKUP(dig): module dns.resolver is not installed")
|
||||||
|
|
||||||
# Create Resolver object so that we can set NS if necessary
|
# Create Resolver object so that we can set NS if necessary
|
||||||
myres = dns.resolver.Resolver()
|
myres = dns.resolver.Resolver(configure=True)
|
||||||
edns_size = 4096
|
edns_size = 4096
|
||||||
myres.use_edns(0, ednsflags=dns.flags.DO, payload=edns_size)
|
myres.use_edns(0, ednsflags=dns.flags.DO, payload=edns_size)
|
||||||
|
|
||||||
domain = None
|
domain = None
|
||||||
qtype = 'A'
|
qtype = 'A'
|
||||||
flat = True
|
flat = True
|
||||||
|
rdclass = dns.rdataclass.from_text('IN')
|
||||||
|
|
||||||
for t in terms:
|
for t in terms:
|
||||||
if t.startswith('@'): # e.g. "@10.0.1.2,192.0.2.1" is ok.
|
if t.startswith('@'): # e.g. "@10.0.1.2,192.0.2.1" is ok.
|
||||||
nsset = t[1:].split(',')
|
nsset = t[1:].split(',')
|
||||||
nameservers = []
|
|
||||||
for ns in nsset:
|
for ns in nsset:
|
||||||
|
nameservers = []
|
||||||
# Check if we have a valid IP address. If so, use that, otherwise
|
# 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
|
# try to resolve name to address using system's resolver. If that
|
||||||
# fails we bail out.
|
# fails we bail out.
|
||||||
|
@ -157,6 +159,11 @@ class LookupModule(LookupBase):
|
||||||
qtype = arg.upper()
|
qtype = arg.upper()
|
||||||
elif opt == 'flat':
|
elif opt == 'flat':
|
||||||
flat = int(arg)
|
flat = int(arg)
|
||||||
|
elif opt == 'class':
|
||||||
|
try:
|
||||||
|
rdclass = dns.rdataclass.from_text(arg)
|
||||||
|
except Exception as e:
|
||||||
|
raise errors.AnsibleError("dns lookup illegal CLASS: ", str(e))
|
||||||
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
@ -168,7 +175,7 @@ class LookupModule(LookupBase):
|
||||||
else:
|
else:
|
||||||
domain = t
|
domain = t
|
||||||
|
|
||||||
# print "--- domain = {0} qtype={1}".format(domain, qtype)
|
# print "--- domain = {0} qtype={1} rdclass={2}".format(domain, qtype, rdclass)
|
||||||
|
|
||||||
ret = []
|
ret = []
|
||||||
|
|
||||||
|
@ -182,7 +189,7 @@ class LookupModule(LookupBase):
|
||||||
raise AnsibleError("dns.reversename unhandled exception", str(e))
|
raise AnsibleError("dns.reversename unhandled exception", str(e))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
answers = myres.query(domain, qtype)
|
answers = myres.query(domain, qtype, rdclass=rdclass)
|
||||||
for rdata in answers:
|
for rdata in answers:
|
||||||
s = rdata.to_text()
|
s = rdata.to_text()
|
||||||
if qtype.upper() == 'TXT':
|
if qtype.upper() == 'TXT':
|
||||||
|
@ -196,6 +203,7 @@ class LookupModule(LookupBase):
|
||||||
rd['owner'] = answers.canonical_name.to_text()
|
rd['owner'] = answers.canonical_name.to_text()
|
||||||
rd['type'] = dns.rdatatype.to_text(rdata.rdtype)
|
rd['type'] = dns.rdatatype.to_text(rdata.rdtype)
|
||||||
rd['ttl'] = answers.rrset.ttl
|
rd['ttl'] = answers.rrset.ttl
|
||||||
|
rd['class'] = dns.rdataclass.to_text(rdata.rdclass)
|
||||||
|
|
||||||
ret.append(rd)
|
ret.append(rd)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
Loading…
Reference in a new issue