Using compound AWS tags from dynamic inventory on a jinja template

Hello everyone,

I’m trying to use a combine call to AWS tags within a jinja template so I can select nodes for a cassandra cluster.

  • seeds: {% set comma = joiner(“,”) %}
    {% for server in groups[ ‘tag_env_{{ env }}:&tag_role_{{ role }}’ ] -%}
    {{ comma() }}{{ hostvars[server].ansible_eth0.ipv4.address }}
    {%- endfor %}

Where {{ env }} and {{ role }} are passed as extra-varas when running the playbook but seems that is not being reconginzed as a valid, here is the error:

{“src”: “templates/cassandra.yaml.j2”}, “module_name”: “template”}, “msg”: “AnsibleUndefinedVariable: ‘dict object’ has no attribute ‘tag_env_{{ env }}:&tag_role_{{ role }}’”

But I use that same combination to select initially the hosts on which to run the playbook as in:

hosts: tag_env_{{ env }}:&tag_role_{{ role }}

Any ideas on why or what may be failing with the jinja template?

Thank you

OK, so I’ve lernt that host variables don’t work in Jinja2 templates.

Any idea on how I can get arround this?

host variables DO work in jinja template, host patterns do not. Just use set theory filters to do the intersection.

Also you can save many moustaches as {% %} already implies variable interpolation.

  • seeds: {% set comma = joiner(“,”) %}
    {% for server in groups[ ‘tag_env_’ + env]|intersection(groups[‘tag_role_’ + role]) -%}
    {{ comma() }}{{ hostvars[server].ansible_eth0.ipv4.address }}
    {%- endfor %}