# Intersection of group variables inside a Jinja template.

**URL:** <https://forum.ansible.com/t/intersection-of-group-variables-inside-a-jinja-template/15637>\
**Category:** Ansible Project\
**Created:** [July 9, 2014, 7:28pm UTC](https://forum.ansible.com/t/intersection-of-group-variables-inside-a-jinja-template/15637 "2014-07-09T19:28:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ehtesh\_Choudhury](https://avatars.discourse-cdn.com/v4/letter/e/58956e/32.png) [@Ehtesh\_Choudhury](https://forum.ansible.com/u/Ehtesh_Choudhury)\
**Post date:** [July 9, 2014, 7:28pm UTC](https://forum.ansible.com/t/intersection-of-group-variables-inside-a-jinja-template/15637/1 "2014-07-09T19:28:40Z")

</div>

I’m using ec2.py as my hostfile and I’m writing the following templating in  
Jinja2.

{% for host in groups[“tag\_provides\_logstash-server”] %}

# {{ hostvars[host][“ec2\_ip\_address”] }} {{ hostvars[host][“ec2\_tag\_Name”] }}

{% endfor %}

# This isn’t valid in Jinja2, but is what I"m looking for:

# groups[“tag\_provides\_logstash-server:&tag\_env\_dev”]

Other people have aimed for this behavior before:

[https://groups.google.com/forum/#!topic/Ansible-project/QuqXUV\_BEMM](https://groups.google.com/forum/#!topic/Ansible-project/QuqXUV_BEMM)

Gustavo’s resolution isn’t as simple as I would like, though.  
This is what I have settled on for now:

{% for host in groups[“tag\_provides\_logstash-server”] | intersect(groups[“tag\_env\_dev”]) %}

# {{ hostvars[host][“ec2\_ip\_address”] }} {{ hostvars[host][“ec2\_tag\_Name”] }}

{% endfor %}

It looks good, but I would like to know if there’s anything grossly wrong or  
devious with this approach.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 9, 2014, 7:59pm UTC](https://forum.ansible.com/t/intersection-of-group-variables-inside-a-jinja-template/15637/2 "2014-07-09T19:59:37Z")

</div>

In your template above I’d probably just assign a “my\_hosts” variable to the expression to clean up the loop a bit.

{# set my\_hosts = groups[“tag\_provides\_logstash-server”] | intersect(groups[“tag\_env\_dev”]) #}  
{% for host in my\_hosts %}

etc

The other thing you have is the “play\_hosts” variable in Ansible which should return all the hosts in the current play.

So to your question, yes, there are easier ways now that can make things easier to follow.

---

<div class="post-metadata">

**Author:** ![Mark\_Maas](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@Mark\_Maas](https://forum.ansible.com/u/Mark_Maas)\
**Post date:** [January 14, 2015, 12:11pm UTC](https://forum.ansible.com/t/intersection-of-group-variables-inside-a-jinja-template/15637/3 "2015-01-14T12:11:21Z")

</div>

Gents,

Could you elaborate a bit on how to use this?

You see, i’m trying to do the same thing:

```auto
# Cluster connection URL 
{# set mysql_hosts = groups["tag_type_mysql"] | intersect(groups["tag_environment_dev"]) #}
wsrep_cluster_address=gcomm://{% for host in mysql_hosts %}{{ hostvars[host]['ansible_eth0']['ipv4']['address'] }},{% endfor %}

```

But the variable “mysql\_hosts” does not seem to get populated:

```auto
fatal: [10.220.226.243] => {'msg': "AnsibleUndefinedVariable: One or more undefined variables: 'mysql_hosts' is undefined", 'failed': True

```

When I look at the two groups:

```auto
type mysql group
{{ groups.tag_type_mysql | to_nice_yaml }}
env dev group
{{ groups.tag_environment_dev | to_nice_yaml }}

```

They result in:

```auto
type mysql group

- 10.220.226.243
- 10.220.227.5
- 10.220.225.203
env dev group

- 10.220.2.242
- 10.220.3.129
- 10.220.227.77
- 10.220.222.24
- 10.220.221.8
- 10.220.226.9
- 10.220.226.243
- 10.220.226.70
- 10.220.227.5
- 10.220.225.203
- 10.220.227.20
- 10.220.1.169
- 10.220.225.84
- 10.220.220.247
- 10.220.225.126

```

so I know they exist and are filled…

Thanks for looking!

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [January 14, 2015, 12:35pm UTC](https://forum.ansible.com/t/intersection-of-group-variables-inside-a-jinja-template/15637/4 "2015-01-14T12:35:36Z")

</div>

I think there was a typo, instead of using {# #} you should probably be using {% %}

The {# #} characters indicate a jinja2 comment.
