Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible/jinja2: Use filter result in if condition

is it possible to use the result of Jinja2 filters in conditions in ansible playbooks?

I'm trying to get this working, but without success:

{% if (item | ipv4) %}{{ item }}{% else %}{{ lookup('dig', item) }}{% endif %}}

where item in my current test is set to localhost (and could be any other private or public domain).

Should do: If item is an IPv4 address the adress should be returned, otherwise it should be "converted" (DNS lookup with dig) to an IPv4 address - but it always is returning the hostname.

Any idea?

Thanks in advance Matthias

like image 255
Matthias Lohr Avatar asked Mar 02 '17 12:03

Matthias Lohr


People also ask

How do you use a Jinja filter?

Jinja2 filter is something we use to transform data held in variables. We apply filters by placing pipe symbol | after the variable followed by name of the filter. Filters can change the look and format of the source data, or even generate new data derived from the input values.

Which filter will return the length of a sequence or hash?

Bloom Filter is a data structure that can do this job. For understanding bloom filters, you must know what is hashing. A hash function takes input and outputs a unique identifier of fixed length which is used for identification of input.


1 Answers

Try

{{ item if (item | ipv4) else lookup('dig',item) }}
like image 145
SztupY Avatar answered Sep 24 '22 00:09

SztupY