Ansible iterate over hosts in inventory group set by variable

怎甘沉沦 提交于 2019-12-04 06:48:57

问题


I have the next snippet in my role template:

upstream portal {
 {% set nodes = groups["my_dev_cluster"] %}
 {% for node in nodes %}
 ...do something with nodes...
 {% endfor %}
}

And it works well.

But when I try to parametrize inventory group name like this:

upstream portal {
 {% set nodes = groups["{{cluster_name}}"] %}
 {% for node in nodes %}
 ...do something with nodes...
 {% endfor %}
}

I get an exception like:

 FAILED! => {"changed": false, "failed": true, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute '{{cluster_name}}'"}

Here, cluster_name - is a simple string variable defined in defaults section.

Is it possible to parametrize it at all?

Thanks in advance!


回答1:


You don't need {{...}} because you're already inside a jinja context (in this case, the {% set ... %} block. Just write:

{% set nodes = groups[cluster_name] %}


来源:https://stackoverflow.com/questions/47698210/ansible-iterate-over-hosts-in-inventory-group-set-by-variable

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