# How to wait for DNS record?

**URL:** <https://forum.ansible.com/t/how-to-wait-for-dns-record/28816>\
**Category:** Ansible Project\
**Created:** [October 18, 2018, 7:32am UTC](https://forum.ansible.com/t/how-to-wait-for-dns-record/28816 "2018-10-18T07:32:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pavel\_Martynov](https://avatars.discourse-cdn.com/v4/letter/p/cdc98d/32.png) [@Pavel\_Martynov](https://forum.ansible.com/u/Pavel_Martynov)\
**Post date:** [October 18, 2018, 7:32am UTC](https://forum.ansible.com/t/how-to-wait-for-dns-record/28816/1 "2018-10-18T07:32:19Z")

</div>

Hi!

I want to wait for some DNS record registered in DNS server. For example, A record. So I try to use this task:

- name: Wait DNS A record [host.example.com](http://host.example.com) registered  
set\_fact:  
lookup\_result: “{{ lookup(‘dig’, ‘[host.example.com](http://host.example.com).’) }}”  
until: lookup\_result != ‘NXDOMAIN’  
retries: 20  
delay: 3

But looks like lookup runs only single time at start and never again. I checked it with tcpdump:  
$ sudo tcpdump udp port 53

Only single request/response on first loop iteration.  
So, looks like lookup\_result setted to value ‘NXDOMAIN’ and this value checked with effectively “until: ‘NXDOMAIN’ != ‘NXDOMAIN’” 20 times without success.

How can I force run lookup on every loop iteration?

Ansible version 2.6.5

Thanks.

---

<div class="post-metadata">

**Author:** ![Pavel\_Martynov](https://avatars.discourse-cdn.com/v4/letter/p/cdc98d/32.png) [@Pavel\_Martynov](https://forum.ansible.com/u/Pavel_Martynov)\
**Post date:** [October 18, 2018, 7:51am UTC](https://forum.ansible.com/t/how-to-wait-for-dns-record/28816/2 "2018-10-18T07:51:01Z")

</div>

Actually, example without lookup at all:

- name: Test random  
set\_fact:  
rand\_result: “{{ 5 | random }}”  
until: rand\_result == ‘NEVER’  
retries: 10  
delay: 1

If I run ansible in verbose mode I see that on all iterations rand\_result var have the same value, so jinja template evaluated once.  
How can I force reevaluate it on every iteration?

чт, 18 окт. 2018 г. в 10:32, Pavel Martynov \<[mr.xkurt@gmail.com](mailto:mr.xkurt@gmail.com)\>:

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [October 18, 2018, 8:34am UTC](https://forum.ansible.com/t/how-to-wait-for-dns-record/28816/3 "2018-10-18T08:34:00Z")

</div>

Try this:

# Test random filter

- hosts: localhost

tasks:

- name: Test random  
set\_fact:  
rand\_result: “{{ 5 |random }}”  
with\_sequence: count=10

Basically the change of item forces a re-evaluation, even though nothing is changing inside the loop.

However: You may still have a problem with DNS. A failed lookup (NXDOMAIN) will set a negative cache time on the result; no nameserver will issue a new query until the negative cache time has expired. The negative cache time is commonly set to values between five minutes and a few hours; very stable zones may set higher values. If the zone is under your control - which it looks like it is - set the negative cache time low when you are working on the zone. It’s the last field in the SOA.

There are two ways around this. One way is to add “+trace” to your dig command to force the query to begin at the root every time. It is poor form to do this rapidly or frequently, so put a nice long pause between the name setup and your first attempt to check the new name (aim for the time it usually takes for a name to propagate for that zone), then use another nice long pause between checks - at least a minute. Checking once per second for an hour is very uncivilised indeed.

A better alternative is to do an SOA lookup, get the negative cache time value, add one minute, and use that as your pause time.

Regards, K.

---

<div class="post-metadata">

**Author:** ![Pavel\_Martynov](https://avatars.discourse-cdn.com/v4/letter/p/cdc98d/32.png) [@Pavel\_Martynov](https://forum.ansible.com/u/Pavel_Martynov)\
**Post date:** [October 18, 2018, 9:33am UTC](https://forum.ansible.com/t/how-to-wait-for-dns-record/28816/4 "2018-10-18T09:33:15Z")

</div>

> Try this:
> 
> # Test random filter
> 
> - hosts: localhost
> 
> tasks:
> 
> - name: Test random  
> set\_fact:  
> rand\_result: “{{ 5 |random }}”  
> with\_sequence: count=10
> 
> Basically the change of item forces a re-evaluation, even though nothing is changing inside the loop.

I tried to use this:

- name: Wait DNS A record [host.example.com](http://host.example.com) registered  
set\_fact:  
lookup\_result: “{{ lookup(‘dig’, ‘[host.example.com](http://host.example.com).’) }}”  
until: lookup\_result != ‘NXDOMAIN’  
retries: 1  
delay: 3  
with\_sequence: count=20

But the overall result of the task is a failure (I think this is because there is fail iterations at start).  
Another flaw of this workaround is that task runs all 20 iterations even if there is DNS record, say, on 7 iteration (no ‘break loop’ logic).

I replace my task with this workaround:

- name: Wait DNS A record [host.example.com](http://host.example.com) registered  
command: “dig [host.example.com](http://host.example.com) +short”  
register: dig\_result  
until: dig\_result.stdout != ‘’  
retries: 20  
delay: 3

JFYI there is related issue on Github: [https://github.com/ansible/ansible/issues/44128](https://github.com/ansible/ansible/issues/44128)

> However: You may still have a problem with DNS. A failed lookup (NXDOMAIN) will set a negative cache time on the result; no nameserver will issue a new query until the negative cache time has expired. The negative cache time is commonly set to values between five minutes and a few hours; very stable zones may set higher values. If the zone is under your control - which it looks like it is - set the negative cache time low when you are working on the zone. It’s the last field in the SOA.

Yes, this is private zone under my control and negative cache already set to 60 seconds which is fine for me.

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [October 18, 2018, 9:50am UTC](https://forum.ansible.com/t/how-to-wait-for-dns-record/28816/5 "2018-10-18T09:50:53Z")

</div>

Sorry, I wasn’t suggesting you use a sequence! 🙂 That was just to illustrate the effect in a small test program.

I was just saying that to get the template re-evaluated you need to use a loop control that changes. Which you have achieved with a check of stdout.

Regards, K.
