# Jinja template with group and subgroup?

**URL:** <https://forum.ansible.com/t/jinja-template-with-group-and-subgroup/18938>\
**Category:** Ansible Project\
**Created:** [August 4, 2015, 3:13pm UTC](https://forum.ansible.com/t/jinja-template-with-group-and-subgroup/18938 "2015-08-04T15:13:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zachary\_Buckholz](https://avatars.discourse-cdn.com/v4/letter/z/f1d935/32.png) [@Zachary\_Buckholz](https://forum.ansible.com/u/Zachary_Buckholz)\
**Post date:** [August 4, 2015, 3:13pm UTC](https://forum.ansible.com/t/jinja-template-with-group-and-subgroup/18938/1 "2015-08-04T15:13:59Z")

</div>

I have multiple elasticsearch locations, each is a different cluster, but all managed by a tribe leader.

I am trying to create just one elasticsearch.yml template that can look at my inventory and determine of the host is part of the elasticsearch-tribe-member group or a tribe-leader, then determine which cluster it’s part of.

My inventory follows this pattern:

[elasticsearch-tribe-leader]  
FQDN  
[elasticsearch-tribe-leader:vars]  
location=geo location

[ny-elasticsearch-servers]  
FQDN  
FQDN  
FQDN

[ny-elasticsearch-servers:vars]

location=ny  
cluster\_name=ny-servers

[ch-elasticsearch-servers]  
FQDN  
FQDN  
FQDN  
[ch-elasticsearch-servers:vars]

location=ch  
cluster\_name=ch-servers

[bz-elasticsearch-servers]  
FQDN  
FQDN  
FQDN  
[bz-elasticsearch-servers:vars]

location=bz  
cluster\_name=bz-servers

[elasticsearch-servers:children]

elasticsearch-tribe-leader  
ny-elasticsearch-servers

ch-elasticsearch-servers

bz-elasticsearch-servers

[elasticsearch-tribe-member:children]  
ny-elasticsearch-servers

ch-elasticsearch-servers

bz-elasticsearch-servers

This is where I am at right now.

I was hoping to say; if host is in the elasticsearch-tribe-member group, assign the subgroup name to some variable and then iterate through those cluster nodes. So each ny cluster node will be expanded and used in the zen unicast hosts section.

cluster.name: {{ cluster\_name }}  
node.name: {{ ansible\_fqdn }}

# Some logic to determine if tribe leader

{% if host in groups[‘elasticsearch-tribe-leader’] %}  
tribe:

# loop cluster groups

{% for cluster in groups[‘elasticsearch-tribe-member’] %}  
{{ cluster.cluster\_name }}:  
cluster.name: {{ cluster.cluster\_name }}  
discovery.zen.ping.multicase.enabled: false

# loop for each IP

discovery.zen.ping.unicast.hosts: [{% for node in cluster %}“{{ node.ipaddr(‘address’) }}{% if not loop.last %}”,{% endif %}{% endfor %}"]  
{% endfor %}  
{% endif %}

# If elasticsearch-tribe-member

{% if host in groups[‘elasticsearch-tribe-member’] %}  
discovery.zen.ping.unicast.hosts: [{% for node in groups[‘elasticsearch-tribe-member’] %}“{{ node.ipaddr(‘address’) }}{% if not loop.last %}”,{% endif %}{% endfor %}"]  
{% endif %}  
http.cors.allow-origin: “/.\*/”  
http.cors.enabled: true  
path.logs: /var/log/elasticsearch  
path.data: /data//elasticsearch/data  
node.rack: {{ node\_rack }}  
bootstrap.mlockall: true  
node.master: {% if host in groups[‘elasticsearch-tribe-leader’] %} false {% else %} true {% endif %}  
node.data: {% if host in groups[‘elasticsearch-tribe-leader’] %} false {% else %} true {% endif %}

Has anyone seen an example like this? Any recommendations on how to do this. I know I could have multiple static elasticsearch.yml files for each location/cluster but I was trying to avoid that.

Thanks
