'terms contains a string with things to `dig\' for. We support the
following formats:
example.com                                     # A record
example.com  qtype=A                            # same
example.com/TXT                                 # specific qtype
example.com  qtype=txt                          # same
192.0.2.23/PTR                                 # reverse PTR
^^ shortcut for 23.2.0.192.in-addr.arpa/PTR
example.net/AAAA  @nameserver                   # query specified server
^^^ can be comma-sep list of names/addresses
... flat=0                                      # returns a dict; default is 1 == string'

def run(self, terms, variables=None, **kwargs):

    if (HAVE_DNS is False):
        raise AnsibleError("Can't    LOOKUP(dig):    module    dns.resolver    is    not    installed")
    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('@'):
            nsset = t[1:].split(',')
            for ns in nsset:
                nameservers = []
                try:
                    socket.inet_aton(ns)
                    nameservers.append(ns)
                except:
                    try:
                        nsaddr = dns.resolver.query(ns)[0].address
                        nameservers.append(nsaddr)
                    except Exception as e:
                        raise AnsibleError(('dns    lookup    NS:    %s' % to_native(e)))
                myres.nameservers = nameservers
            continue
        if ('=' in t):
            try:
                (opt, arg) = t.split('=')
            except:
                pass
            if (opt == 'qtype'):
                qtype = arg.upper()
            elif (opt == 'flat'):
                flat = int(arg)
            elif (opt == 'class'):
                try:
                    rdclass = dns.rdataclass.from_text(arg)
                except Exception as e:
                    raise AnsibleError(('dns    lookup    illegal    CLASS:    %s' % to_native(e)))
            continue
        if ('/' in t):
            try:
                (domain, qtype) = t.split('/')
            except:
                domain = t
        else:
            domain = t
    ret = []
    if (qtype.upper() == 'PTR'):
        try:
            n = dns.reversename.from_address(domain)
            domain = n.to_text()
        except dns.exception.SyntaxError:
            pass
        except Exception as e:
            raise AnsibleError(('dns.reversename    unhandled    exception    %s' % to_native(e)))
    try:
        answers = myres.query(domain, qtype, rdclass=rdclass)
        for rdata in answers:
            s = rdata.to_text()
            if (qtype.upper() == 'TXT'):
                s = s[1:(-1)]
            if flat:
                ret.append(s)
            else:
                try:
                    rd = make_rdata_dict(rdata)
                    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:
                    ret.append(str(e))
    except dns.resolver.NXDOMAIN:
        ret.append('NXDOMAIN')
    except dns.resolver.NoAnswer:
        ret.append('')
    except dns.resolver.Timeout:
        ret.append('')
    except dns.exception.DNSException as e:
        raise AnsibleError(('dns.resolver    unhandled    exception    %s' % to_native(e)))
    return ret
