Ansible/jinja2: Use filter result in if condition
Asked Answered
B

1

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

Blinkers answered 2/3, 2017 at 12:58 Comment(0)
S
12

Try

{{ item if (item | ipv4) else lookup('dig',item) }}
Sholokhov answered 2/3, 2017 at 18:23 Comment(1)
Thank you! With Ubuntu 16.04 you need also the packages python-netaddr and python-dnspythonBlinkers

© 2022 - 2024 — McMap. All rights reserved.