如何在单个块中执行多行Jinja2条件?

下面的代码由于语法错误而被拒绝:

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

我希望,我的意图已经足够清楚,让Jinja2的专家来纠正我…请吗?

除非它是一个if-expression式,否则不能将if-then-else放在一个块中。 或者:

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

要么

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