11

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

Matthias Lohr
  • 1,696
  • 2
  • 20
  • 32

1 Answers1

12

Try

{{ item if (item | ipv4) else lookup('dig',item) }}
SztupY
  • 10,291
  • 8
  • 64
  • 87