Note
This lookup plugin is part of the community.general collection (version 10.7.3).
You might already have this collection installed if you are using the ansible package. It is not included in ansible-core. To check whether it is installed, run ansible-galaxy collection list.
To install it, use: ansible-galaxy collection install community.general. You need further requirements to be able to use this lookup plugin, see Requirements for details.
To use it in a playbook, specify: community.general.dig.
wantlist=true to the lookup call, or alternatively use query instead of lookup, which will result in the record values being returned as a list over which you can iterate later on.The below requirements are needed on the local controller node that executes this lookup.
Parameter | Comments |
|---|---|
Terms list / elements=string | Domain(s) to query. |
This describes keyword parameters of the lookup. These are the values key1=value1, key2=value2 and so on in the following examples: lookup('community.general.dig', key1=value1, key2=value2, ...) and query('community.general.dig', key1=value1, key2=value2, ...)
Parameter | Comments |
|---|---|
class string | Class. Default: |
fail_on_error boolean added in community.general 5.4.0 | Abort execution on lookup errors. The default for this option will likely change to Choices:
|
flat integer | If 0 each record is returned as a dictionary, otherwise a string. Default: |
port integer added in community.general 9.5.0 | Use port as target port when looking up DNS records. Default: |
qtype string | Record type to query.
Choices:
|
real_empty boolean added in community.general 6.0.0 | Return empty result without empty strings, and return empty list instead of The default for this option will likely change to This option will be forced to Choices:
|
retry_servfail boolean added in community.general 3.6.0 | Retry a nameserver if it returns SERVFAIL. Choices:
|
tcp boolean added in community.general 7.5.0 | Use TCP to lookup DNS records. Choices:
|
Note
lookup('community.general.dig', term1, term2, key1=value1, key2=value2) and query('community.general.dig', term1, term2, key1=value1, key2=value2)
ALL is not a record in itself, merely the listed fields are available for any record results you retrieve in the form of a dictionary.dnspython supports out of the box, only a subset can be converted into a dictionary.- name: Simple A record (IPV4 address) lookup for example.com
ansible.builtin.debug:
msg: "{{ lookup('community.general.dig', 'example.com.')}}"
- name: "The TXT record for example.org."
ansible.builtin.debug:
msg: "{{ lookup('community.general.dig', 'example.org.', qtype='TXT') }}"
- name: "The TXT record for example.org, alternative syntax."
ansible.builtin.debug:
msg: "{{ lookup('community.general.dig', 'example.org./TXT') }}"
- name: use in a loop
ansible.builtin.debug:
msg: "MX record for gmail.com {{ item }}"
with_items: "{{ lookup('community.general.dig', 'gmail.com./MX', wantlist=true) }}"
- name: Lookup multiple names at once
ansible.builtin.debug:
msg: "A record found {{ item }}"
loop: "{{ query('community.general.dig', 'example.org.', 'example.com.', 'gmail.com.') }}"
- name: Lookup multiple names at once (from list variable)
ansible.builtin.debug:
msg: "A record found {{ item }}"
loop: "{{ query('community.general.dig', *hosts) }}"
vars:
hosts:
- example.org.
- example.com.
- gmail.com.
- ansible.builtin.debug:
msg: "Reverse DNS for 192.0.2.5 is {{ lookup('community.general.dig', '192.0.2.5/PTR') }}"
- ansible.builtin.debug:
msg: "Reverse DNS for 192.0.2.5 is {{ lookup('community.general.dig', '5.2.0.192.in-addr.arpa./PTR') }}"
- ansible.builtin.debug:
msg: "Reverse DNS for 192.0.2.5 is {{ lookup('community.general.dig', '5.2.0.192.in-addr.arpa.', qtype='PTR') }}"
- ansible.builtin.debug:
msg: "Querying 198.51.100.23 for IPv4 address for example.com. produces {{ lookup('dig', 'example.com', '@198.51.100.23') }}"
- ansible.builtin.debug:
msg: "XMPP service for gmail.com. is available at {{ item.target }} on port {{ item.port }}"
with_items: "{{ lookup('community.general.dig', '_xmpp-server._tcp.gmail.com./SRV', flat=0, wantlist=true) }}"
- name: Retry nameservers that return SERVFAIL
ansible.builtin.debug:
msg: "{{ lookup('community.general.dig', 'example.org./A', retry_servfail=true) }}"
Key | Description |
|---|---|
Return value list / elements=any | List of composed strings or of dictionaries, with fields depending on query type. Returned: success |
|
A string |
Returned: success |
|
AAAA string |
Returned: success |
|
ALL string |
Returned: success |
|
CAA string added in community.general 6.3.0 |
Returned: success |
|
CNAME string |
Returned: success |
|
DNAME string |
Returned: success |
|
DNSKEY string |
Returned: success |
|
DS string |
Returned: success |
|
HINFO string |
Returned: success |
|
LOC string |
Returned: success |
|
MX string |
Returned: success |
|
NAPTR string |
Returned: success |
|
NS string |
Returned: success |
|
NSEC3PARAM string |
Returned: success |
|
PTR string |
Returned: success |
|
RP string |
Returned: success |
|
SOA string |
Returned: success |
|
SPF string |
Returned: success |
|
SRV string |
Returned: success |
|
SSHFP string |
Returned: success |
|
TLSA string |
Returned: success |
|
TXT string |
Returned: success |
© 2012–2018 Michael DeHaan
© 2018–2025 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/latest/collections/community/general/dig_lookup.html