ansible.builtin.dig (v2.9.27) — lookup

query DNS using the dnspython library

| "added in version" 1.9 of ansible.builtin"

Authors: Jan-Piet Mens (@jpmens) <jpmens(at)gmail.com>

Install Ansible via pip

Install with pip install ansible==2.9.27

Description

The dig lookup runs queries against DNS servers to retrieve DNS records for a specific name (FQDN - fully qualified domain name). It is possible to lookup any DNS record in this manner.

There is a couple of different syntaxes that can be used to specify what record should be retrieved, and for which name. It is also possible to explicitly specify the DNS server(s) to use for lookups.

In its simplest form, the dig lookup plugin can be used to retrieve an IPv4 address (DNS A record) associated with FQDN

In addition to (default) A record, it is also possible to specify a different record type that should be queried. This can be done by either passing-in additional parameter of format qtype=TYPE to the dig lookup, or by appending /TYPE to the FQDN being queried.

If multiple values are associated with the requested record, the results will be returned as a comma-separated list. In such cases you may want to pass option wantlist=True to the plugin, which will result in the record values being returned as a list over which you can iterate later on.

By default, the lookup will rely on system-wide configured DNS servers for performing the query. It is also possible to explicitly specify DNS servers to query using the @DNS_SERVER_1,DNS_SERVER_2,...,DNS_SERVER_N notation. This needs to be passed-in as an additional parameter to the lookup


Requirements

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Simple A record (IPV4 address) lookup for example.com
  debug: msg="{{ lookup('dig', 'example.com.')}}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: "The TXT record for example.org."
  debug: msg="{{ lookup('dig', 'example.org.', 'qtype=TXT') }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: "The TXT record for example.org, alternative syntax."
  debug: msg="{{ lookup('dig', 'example.org./TXT') }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: use in a loop
  debug: msg="MX record for gmail.com {{ item }}"
  with_items: "{{ lookup('dig', 'gmail.com./MX', wantlist=True) }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- debug: msg="Reverse DNS for 192.0.2.5 is {{ lookup('dig', '192.0.2.5/PTR') }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- debug: msg="Reverse DNS for 192.0.2.5 is {{ lookup('dig', '5.2.0.192.in-addr.arpa./PTR') }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- debug: msg="Reverse DNS for 192.0.2.5 is {{ lookup('dig', '5.2.0.192.in-addr.arpa.', 'qtype=PTR') }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- debug: msg="Querying 198.51.100.23 for IPv4 address for example.com. produces {{ lookup('dig', 'example.com', '@198.51.100.23') }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- debug: msg="XMPP service for gmail.com. is available at {{ item.target }} on port {{ item.port }}"
  with_items: "{{ lookup('dig', '_xmpp-server._tcp.gmail.com./SRV', 'flat=0', wantlist=True) }}"

Inputs

    
flat:
    default: 1
    description: If 0 each record is returned as a dictionary, otherwise a string

qtype:
    choices:
    - A
    - ALL
    - AAAA
    - CNAME
    - DNAME
    - DLV
    - DNSKEY
    - DS
    - HINFO
    - LOC
    - MX
    - NAPTR
    - NS
    - NSEC3PARAM
    - PTR
    - RP
    - RRSIG
    - SOA
    - SPF
    - SRV
    - SSHFP
    - TLSA
    - TXT
    default: A
    description: record type to query

_terms:
    description: domain(s) to query

Outputs

_list:
  description:
  - list of composed strings or dictonaries with key and value If a dictionary, fields
    shows the keys returned depending on query type
  fields:
    A: address
    AAAA: address
    ALL: owner, ttl, type
    CNAME: target
    DLV: algorithm, digest_type, key_tag, digest
    DNAME: target
    DNSKEY: flags, algorithm, protocol, key
    DS: algorithm, digest_type, key_tag, digest
    HINFO: cpu, os
    LOC: latitude, longitude, altitude, size, horizontal_precision, vertical_precision
    MX: preference, exchange
    NAPTR: order, preference, flags, service, regexp, replacement
    NS: target
    NSEC3PARAM: algorithm, flags, iterations, salt
    PTR: target
    RP: mbox, txt
    SOA: mname, rname, serial, refresh, retry, expire, minimum
    SPF: strings
    SRV: priority, weight, port, target
    SSHFP: algorithm, fp_type, fingerprint
    TLSA: usage, selector, mtype, cert
    TXT: strings