# Using compound AWS tags from dynamic inventory on a jinja template

**URL:** <https://forum.ansible.com/t/using-compound-aws-tags-from-dynamic-inventory-on-a-jinja-template/22765>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [October 11, 2016, 1:57am UTC](https://forum.ansible.com/t/using-compound-aws-tags-from-dynamic-inventory-on-a-jinja-template/22765 "2016-10-11T01:57:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Max\_Leon](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Max\_Leon](https://forum.ansible.com/u/Max_Leon)\
**Post date:** [October 11, 2016, 1:57am UTC](https://forum.ansible.com/t/using-compound-aws-tags-from-dynamic-inventory-on-a-jinja-template/22765/1 "2016-10-11T01:57:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Max\_Leon](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Max\_Leon](https://forum.ansible.com/u/Max_Leon)\
**Post date:** [October 11, 2016, 6:31pm UTC](https://forum.ansible.com/t/using-compound-aws-tags-from-dynamic-inventory-on-a-jinja-template/22765/2 "2016-10-11T18:31:19Z")

</div>

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

Any idea on how I can get arround this?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [October 11, 2016, 7:15pm UTC](https://forum.ansible.com/t/using-compound-aws-tags-from-dynamic-inventory-on-a-jinja-template/22765/3 "2016-10-11T19:15:04Z")

</div>

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 %}
