# Dns lookup record and fail if no record exists

**URL:** <https://forum.ansible.com/t/dns-lookup-record-and-fail-if-no-record-exists/5179>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [April 26, 2024, 8:55pm UTC](https://forum.ansible.com/t/dns-lookup-record-and-fail-if-no-record-exists/5179 "2024-04-26T20:55:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dsexton18](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dsexton18/32/4952_2.png) [@dsexton18](https://forum.ansible.com/u/dsexton18)\
**Post date:** [April 26, 2024, 8:55pm UTC](https://forum.ansible.com/t/dns-lookup-record-and-fail-if-no-record-exists/5179/1 "2024-04-26T20:55:21Z")

</div>

I am struggling with a task to 1 look up the DNS name of a host and to if no dns record exists for the host fail. I have the first part of the task with the second task.  
This is what I have so far

```auto
- name: Simple A record (IPV4 address) lookup for foreman.example.com
  ansible.builtin.debug:
    msg: "{{ lookup('community.general.dig', 'foreman.example.com.')}}"

- assert:
    that:
      - ansible_facts["foreman.example.com"] in dns_result.[0].values
    fail_msg: "Hostname does not match DNS record"
    quiet: yes

```

---

<div class="post-metadata">

**Author:** ![kurokobo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kurokobo/32/182_2.png) [@kurokobo](https://forum.ansible.com/u/kurokobo)\
**Post date:** [April 27, 2024, 2:01pm UTC](https://forum.ansible.com/t/dns-lookup-record-and-fail-if-no-record-exists/5179/2 "2024-04-27T14:01:00Z")

</div>

Hi, just as an idea, you can do that by this single task, for example.

```yaml
- ansible.builtin.assert:
    that:
      - dig != ""
      - dig != "NXDOMAIN"
  vars:
    dig: "{{ lookup('community.general.dig', 'foreman.example.com.') }}"

```

As a side note, I’m curious about the intent of `ansible_facts["foreman.example.com"]` and `dns_result.[0].values` in your snippet. Perhaps there might be a slight misunderstanding about variables and ansible\_facts.  
You can refer to the official docs about facts and variables:

- [Discovering variables: facts and magic variables — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_vars_facts.html)
- [Using Variables — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html)
- [ansible.builtin.set\_fact module – Set host variable(s) and fact(s). — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/set_fact_module.html)

---

<div class="post-metadata">

**Author:** ![dsexton18](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dsexton18/32/4952_2.png) [@dsexton18](https://forum.ansible.com/u/dsexton18)\
**Post date:** [May 4, 2024, 9:45am UTC](https://forum.ansible.com/t/dns-lookup-record-and-fail-if-no-record-exists/5179/3 "2024-05-04T09:45:33Z")

</div>

Thanks that’s just what I needed for the task. Appreciate it.

---

<div class="post-metadata">

**Author:** ![jpmens](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jpmens/32/5543_2.png) [@jpmens](https://forum.ansible.com/u/jpmens)\
**Post date:** [May 4, 2024, 10:30am UTC](https://forum.ansible.com/t/dns-lookup-record-and-fail-if-no-record-exists/5179/4 "2024-05-04T10:30:58Z")

</div>

> [@kurokobo](#):
>
> just as an idea, you can do that by this single task

do note, however, that the `vars` specified for the `assert` task are local to the task, i.e. the variable `dig` is not available after the assert is evaluated.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [June 3, 2024, 10:31am UTC](https://forum.ansible.com/t/dns-lookup-record-and-fail-if-no-record-exists/5179/5 "2024-06-03T10:31:35Z")

</div>

Closed on June 3, 2024, 10:31am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
