Ansible/Jinja: condition with an undefined statement

岁酱吖の 提交于 2020-01-03 04:09:10

问题


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:


I think you're looking for the default filter:

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



来源:https://stackoverflow.com/questions/36989167/ansible-jinja-condition-with-an-undefined-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!