3

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?

5

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 }}
1
  • 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

Not the answer you're looking for? Browse other questions tagged or ask your own question.