Ansible iterate over hosts in inventory group set by variable
Asked Answered
S

1

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!

Stereochrome answered 7/12, 2017 at 15:16 Comment(0)
T
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] %}
Touch answered 7/12, 2017 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.