# Need help to refer child group in jinja2 template file

**URL:** <https://forum.ansible.com/t/need-help-to-refer-child-group-in-jinja2-template-file/27612>\
**Category:** Ansible Project\
**Created:** [May 24, 2018, 7:33am UTC](https://forum.ansible.com/t/need-help-to-refer-child-group-in-jinja2-template-file/27612 "2018-05-24T07:33:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Balaji1](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@Balaji1](https://forum.ansible.com/u/Balaji1)\
**Post date:** [May 24, 2018, 7:33am UTC](https://forum.ansible.com/t/need-help-to-refer-child-group-in-jinja2-template-file/27612/1 "2018-05-24T07:33:35Z")

</div>

Hello All,

Please help or suggest how I can solve this problem.  
I will pass

-e group=“1” when calling playbook.

**Playbook** - file.yml

hosts: servers\_{{group}}

- task:  
…  
…

**inventory**

[servers\_grp1]  
server1

[servers\_grp2]  
server2

…

[mainservers\_1:children]  
[servers\_grp1]  
[servers\_grp2]  
[servers\_grp3]  
[servers\_grp4]

[mainservers\_2:children]  
[servers\_grp5]  
[servers\_grp6]  
[servers\_grp7]  
[servers\_grp8]

In Jinja2 template file, I am trying to assign a value to variable “pod\_num”. But I am unable to get this

If (servers\_grp1) is in mainservers\_1  
then  
pod\_num = 2  
else  
pod\_num = 4

I tried a couple of things but did not work out.

{% if hosts is in groups[mainservers\_1]  
{% set pod\_num = 2 %}

{% else %}  
{% set pod\_num = 4 %}

Thanks for helping and guiding.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 25, 2018, 6:07pm UTC](https://forum.ansible.com/t/need-help-to-refer-child-group-in-jinja2-template-file/27612/2 "2018-05-25T18:07:35Z")

</div>

> Hello All,
> 
> Please help or suggest how I can solve this problem.  
> I will pass
> 
> -e group="1" when calling playbook.
> 
> \*Playbook\* - file.yml
> 
> hosts: servers\_{{group}}

This will give you servers\_1, but your host is servers\_grp1, typo?

> - task:  
> ....  
> .....
> 
> \*inventory\*
> 
> [servers\_grp1]  
> server1
> 
> [servers\_grp2]  
> server2
> 
> ....
> 
> [mainservers\_1:children]  
> [servers\_grp1]  
> [servers\_grp2]  
> [servers\_grp3]  
> [servers\_grp4]
> 
> [mainservers\_2:children]  
> [servers\_grp5]  
> [servers\_grp6]  
> [servers\_grp7]  
> [servers\_grp8]
> 
> In Jinja2 template file, I am trying to assign a value to variable  
> "pod\_num". But I am unable to get this
> 
> If (servers\_grp1) is in mainservers\_1  
> then  
> pod\_num = 2  
> else  
> pod\_num = 4
> 
> I tried a couple of things but did not work out.
> 
> {% if hosts is in groups[mainservers\_1]  
> {% set pod\_num = 2 %}
> 
> {% else %}  
> {% set pod\_num = 4 %}

This work great, but the variable is only available in the template, not outside of it.  
You'll see it work by this code

{% if hosts is in groups[mainservers\_1]  
{% set pod\_num = 2 %}

{% else %}  
{% set pod\_num = 4 %}  
{{ pod\_num }}

If you need the variable in the scope of Ansible global you should check out group\_vars.  
Create a file group\_vars/servers\_grp1.yml and group\_vars/servers\_grp1.yml with the appropriate and variables with values.
