1

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!

4

1 回答 1

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] %}
于 2017-12-07T15:19:01.953 回答