问题
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