4

The below code is rejected as syntactically incorrect:

{%
    if inventory_hostname in groups.aptcache
        set cachehost = 'localhost'
    else
        set cachehost = groups['aptcache'] | first
    endif
%}
cache={{ cachehost }}

I hope, my intent is clear enough for a Jinja2 guru to correct me... Please?

Mikhail T.
  • 2,272
  • 1
  • 22
  • 49

1 Answers1

7

You can't put the if-then-else in one block unless it is an if-expression. Either:

{% if inventory_hostname in groups.aptcache %}
{%      set cachehost = 'localhost' %}
{% else %}
{%      set cachehost = groups['aptcache'] | first %}
{% endif %}
cache={{ cachehost }}

or

cache={{ 'localhost' if inventory_hostname in groups.aptcache else groups['aptcache'] | first }}
Mark Wagner
  • 17,764
  • 2
  • 30
  • 47
  • 2
    I know, I can -- but that seems ugly -- is there really no way to keep it in a _single_ {% ... %} block spanning multiple lines? – Mikhail T. Aug 25 '17 at 18:40