Ansible/Jinja:具有未定义语句的条件



我需要迭代所有主机,并为不包含在组somegroup:中的主机生成配置文件

{% for host in groups.all if host not in groups['somegroup'] %}

但是,如果somegroup不存在,它就会失败("StrictUndefined"类型的参数不可迭代)。

我如何正确地写这篇文章以避免两个不同的for循环:

{% if groups['somegroup'] is defined %}
{% for host in groups.all if host not in groups['somegroup'] %}
...
{% endfor %}
{% else %}
{% for host in groups.all %}
...
{% endfor %}
{% endif %}

我想您正在寻找default过滤器:

{% for host in groups.all if host not in groups['somegroup'] | default([]) %}

最新更新