Ansible/Jinja: condition with an undefined stateme

2019-06-06 16:53发布

I need to iterate over all hosts and generate config file for hosts that are not contained in group somegroup:

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

But if somegroup does not exist, it fails (argument of type 'StrictUndefined' is not iterable).

How do I write this correctly to avoid two different for cycles:

{% 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 %}

1条回答
女痞
2楼-- · 2019-06-06 17:49

I think you're looking for the default filter:

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

查看更多
登录 后发表回答