Skip empty lookup results in template

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

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

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

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

s/car/var/…autocorrect!!!

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 %}
    {{ addr }} key blah;
{% endfor %}

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