1
0
Fork 0
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:
JP Mens 2017-04-04 21:00:56 +02:00 committed by Brian Coca
parent 597bca3129
commit 5a0b2fe3be

View file

@ -26,6 +26,7 @@ try:
import dns.name
import dns.resolver
import dns.reversename
import dns.rdataclass
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)
HAVE_DNS = True
@ -120,19 +121,20 @@ class LookupModule(LookupBase):
raise AnsibleError("Can't LOOKUP(dig): module dns.resolver is not installed")
# Create Resolver object so that we can set NS if necessary
myres = dns.resolver.Resolver()
myres = dns.resolver.Resolver(configure=True)
edns_size = 4096
myres.use_edns(0, ednsflags=dns.flags.DO, payload=edns_size)
domain = None
qtype = 'A'
flat = True
rdclass = dns.rdataclass.from_text('IN')
for t in terms:
if t.startswith('@'): # e.g. "@10.0.1.2,192.0.2.1" is ok.
nsset = t[1:].split(',')
nameservers = []
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.
@ -157,6 +159,11 @@ class LookupModule(LookupBase):
qtype = arg.upper()
elif opt == 'flat':
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
@ -168,7 +175,7 @@ class LookupModule(LookupBase):
else:
domain = t
# print "--- domain = {0} qtype={1}".format(domain, qtype)
# print "--- domain = {0} qtype={1} rdclass={2}".format(domain, qtype, rdclass)
ret = []
@ -182,7 +189,7 @@ class LookupModule(LookupBase):
raise AnsibleError("dns.reversename unhandled exception", str(e))
try:
answers = myres.query(domain, qtype)
answers = myres.query(domain, qtype, rdclass=rdclass)
for rdata in answers:
s = rdata.to_text()
if qtype.upper() == 'TXT':
@ -196,6 +203,7 @@ class LookupModule(LookupBase):
rd['owner'] = answers.canonical_name.to_text()
rd['type'] = dns.rdatatype.to_text(rdata.rdtype)
rd['ttl'] = answers.rrset.ttl
rd['class'] = dns.rdataclass.to_text(rdata.rdclass)
ret.append(rd)
except Exception as e: