# Skip empty lookup results in template

**URL:** <https://forum.ansible.com/t/skip-empty-lookup-results-in-template/18629>\
**Category:** Ansible Project\
**Created:** [June 28, 2015, 11:19am UTC](https://forum.ansible.com/t/skip-empty-lookup-results-in-template/18629 "2015-06-28T11:19:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anand\_Buddhdev](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@Anand\_Buddhdev](https://forum.ansible.com/u/Anand_Buddhdev)\
**Post date:** [June 28, 2015, 11:19am UTC](https://forum.ansible.com/t/skip-empty-lookup-results-in-template/18629/1 "2015-06-28T11:19:43Z")

</div>

Hi folk,

I have a BIND template in which I want to insert the result of the new “dig” lookup, like this:

masters blah {  
{% for host in hosts %}  
{{ lookup(‘dig’,h }} port P key K;  
{{ lookup(‘dig’,h+‘/aaaa’) }} port P key K;  
{% endfor %}  
};

One problem with this is that not all hosts have a AAAA record (and in the near future, some of them may have the opposite, in not having an A record). Alternatively, the lookup could result in an NXDOMAIN or SERVFAIL response.

How could I skip lines for which the lookup returns a result that isn’t an IP address (empty, NXDOMAIN, SERVFAIL, etc)?

Anand

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [June 28, 2015, 3:14pm UTC](https://forum.ansible.com/t/skip-empty-lookup-results-in-template/18629/2 "2015-06-28T15:14:26Z")

</div>

This is really a jinja2 question, just use the 'if'

{% if lookup('dig',h+'/aaaa')|default('SERVFAIL') not in ['NXDOMAIN',  
'SERVFAIL','']%} lookup('dig',h+'/aaaa') }} port P key K; {% endif %}

^ or something like that

---

<div class="post-metadata">

**Author:** ![Anand\_Buddhdev](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@Anand\_Buddhdev](https://forum.ansible.com/u/Anand_Buddhdev)\
**Post date:** [June 28, 2015, 7:00pm UTC](https://forum.ansible.com/t/skip-empty-lookup-results-in-template/18629/3 "2015-06-28T19:00:23Z")

</div>

Brian,

> This is really a jinja2 question, just use the ‘if’
> 
> {% if lookup(‘dig’,h+‘/aaaa’)|default(‘SERVFAIL’) not in [‘NXDOMAIN’,  
> ‘SERVFAIL’,‘’]%} lookup(‘dig’,h+‘/aaaa’) }} port P key K; {% endif %}

Thank you! This works perfectly! It causes a double lookup, but that’s fine. I’m using the “ipaddr” filter, and my final version looks like this:

{% if lookup(‘dig’,h) | ipaddr %}  
{{ lookup(‘dig’,h) }};  
{% endif %}

Anand

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [June 28, 2015, 9:05pm UTC](https://forum.ansible.com/t/skip-empty-lookup-results-in-template/18629/4 "2015-06-28T21:05:08Z")

</div>

To avoid the double you could set a car in the loop and test/print it instead.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [June 28, 2015, 9:05pm UTC](https://forum.ansible.com/t/skip-empty-lookup-results-in-template/18629/5 "2015-06-28T21:05:50Z")

</div>

s/car/var/…autocorrect!!!

---

<div class="post-metadata">

**Author:** ![Anand\_Buddhdev](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@Anand\_Buddhdev](https://forum.ansible.com/u/Anand_Buddhdev)\
**Post date:** [June 29, 2015, 12:16am UTC](https://forum.ansible.com/t/skip-empty-lookup-results-in-template/18629/6 "2015-06-29T00:16:35Z")

</div>

Great suggestion!

I've implemented it thus:

{% for host in hosts %}  
{% set addrs = ( lookup('dig', host), lookup('dig', host+'/aaaa') ) %}  
{% for addr in addrs | ipaddr %}  
&nbsp;&nbsp;&nbsp;&nbsp;{{ addr }} key blah;  
{% endfor %}

Now there's only one lookup per qname/qtype combination.
