# ansible jinja2 template to loop through variables that has multiple values for each variable on a per host basis

**URL:** <https://forum.ansible.com/t/ansible-jinja2-template-to-loop-through-variables-that-has-multiple-values-for-each-variable-on-a-per-host-basis/29477>\
**Category:** Ansible Project\
**Created:** [January 19, 2019, 4:04pm UTC](https://forum.ansible.com/t/ansible-jinja2-template-to-loop-through-variables-that-has-multiple-values-for-each-variable-on-a-per-host-basis/29477 "2019-01-19T16:04:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pnagaraj7656](https://avatars.discourse-cdn.com/v4/letter/p/cab0a1/32.png) [@pnagaraj7656](https://forum.ansible.com/u/pnagaraj7656)\
**Post date:** [January 19, 2019, 4:04pm UTC](https://forum.ansible.com/t/ansible-jinja2-template-to-loop-through-variables-that-has-multiple-values-for-each-variable-on-a-per-host-basis/29477/1 "2019-01-19T16:04:47Z")

</div>

My Requirement is as follows

Below is the list of variables and its multiple values :

host1  
env : qa,dev,uat  
grpn: g1  
type: t1  
port: 1401  
type: t2  
port: 1402  
type: t3  
port: 1403

host2  
env : qa,dev,uat  
grpn: g21  
type: t1  
port: 1605  
type: t2  
port: 1606

host3  
env : qa,dev,uat  
grpn: g1  
type: t1  
port: 1503  
type: t2  
port: 1504

etc and the list goes on for many hosts(more than 100 hosts and each host has min 3 applications at 3 diff ports)

I need to do application deployment based on above concept:  
i,e host1 belongs to g1 and one application will be of type1(t1) running under port 1401 and another application of type t2 running at port 1402. I also need to capture the groupname(g1,g2,g2 etc) for every application.

Thus for each host there is a dictionary of type and port, For each type , the port is different.

The above list is huge and its difficult to write so many stanzas in yml format in vars\_file. So I thought I’ll use host\_variables in inventory like below after placing the hosts in different environments under the inventory.

host1 grpn: g1 port: [1401,1401,1403] type: [t1, t2, t3]  
host2 grpn: g2 port: [1605,1606] type: [t1, t2]

My expectation is that for each host it should pick the groupname(grpn) and also for type 1 it should pick port1 and for type2 it should pick port2 and deploy the application respectively…

But Im not aware how to read the variables from the inventory and how to ensure the type - port pair is maintained while deployment.

I thought I’ll create a jinja2 template like below but Im still confused how to achieve the type and port match.

test.yml.j2

---

<div class="post-metadata">

**Author:** ![Mauricio\_Tavares](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Mauricio\_Tavares](https://forum.ansible.com/u/Mauricio_Tavares)\
**Post date:** [January 20, 2019, 4:33am UTC](https://forum.ansible.com/t/ansible-jinja2-template-to-loop-through-variables-that-has-multiple-values-for-each-variable-on-a-per-host-basis/29477/2 "2019-01-20T04:33:54Z")

</div>

> My Requirement is as follows
> 
> Below is the list of variables and its multiple values :
> 
> host1  
> env : qa,dev,uat  
> grpn: g1  
> &nbsp;&nbsp;type: t1  
> &nbsp;&nbsp;&nbsp;&nbsp;port: 1401  
> &nbsp;&nbsp;type: t2  
> &nbsp;&nbsp;&nbsp;&nbsp;port: 1402  
> &nbsp;&nbsp;type: t3  
> &nbsp;&nbsp;&nbsp;&nbsp;port: 1403
> 
> host2  
> env : qa,dev,uat  
> grpn: g21  
> &nbsp;&nbsp;type: t1  
> &nbsp;&nbsp;&nbsp;&nbsp;port: 1605  
> &nbsp;&nbsp;type: t2  
> &nbsp;&nbsp;&nbsp;&nbsp;port: 1606
> 
> host3  
> env : qa,dev,uat  
> grpn: g1  
> &nbsp;&nbsp;type: t1  
> &nbsp;&nbsp;&nbsp;&nbsp;port: 1503  
> &nbsp;&nbsp;type: t2  
> &nbsp;&nbsp;&nbsp;&nbsp;port: 1504
> 
> etc and the list goes on for many hosts(more than 100 hosts and each host has min 3 applications at 3 diff ports)
> 
> I need to do application deployment based on above concept:  
> i,e host1 belongs to g1 and one application will be of type1(t1) running under port 1401 and another application of type t2 running at port 1402. I also need to capture the groupname(g1,g2,g2 etc) for every application.
> 
> Thus for each host there is a dictionary of type and port, For each type , the port is different.
> 
> The above list is huge and its difficult to write so many stanzas in yml format in vars\_file. So I thought I'll use host\_variables in inventory like below after placing the hosts in different environments under the inventory.
> 
> host1 grpn: g1 port: [1401,1401,1403] type: [t1, t2, t3]  
> host2 grpn: g2 port: [1605,1606] type: [t1, t2]
> 
> My expectation is that for each host it should pick the groupname(grpn) and also for type 1 it should pick port1 and for type2 it should pick port2 and deploy the application respectively..
> 
> But Im not aware how to read the variables from the inventory and how to ensure the type - port pair is maintained while deployment.
> 
> I thought I'll create a jinja2 template like below but Im still confused how to achieve the type and port match.
> 
> test.yml.j2
> 
> ---  
> deploy:  
> - host: host1  
> &nbsp;&nbsp;portnum : {{ port }}  
> &nbsp;&nbsp;groupname: {{ grpn }}  
> &nbsp;&nbsp;type: {{ type }}  
> &nbsp;&nbsp;&nbsp;env : {{ env }}
> 
> Kindly suggest how I can achieve this??

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Have you searched previous posts in the mailing list? Have you  
read the loop examples in the ansible docs?

---

<div class="post-metadata">

**Author:** ![pnagaraj7656](https://avatars.discourse-cdn.com/v4/letter/p/cab0a1/32.png) [@pnagaraj7656](https://forum.ansible.com/u/pnagaraj7656)\
**Post date:** [January 20, 2019, 8:14am UTC](https://forum.ansible.com/t/ansible-jinja2-template-to-loop-through-variables-that-has-multiple-values-for-each-variable-on-a-per-host-basis/29477/3 "2019-01-20T08:14:25Z")

</div>

Hi Mauricio,

Yes I have read for loop in ansible.

But I’m not aware how to loop through the variables thats placed in the host inventory file.

I need assistance in knowing where to place my variable list .
