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

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

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

---
deploy:
- host: host1
  portnum : {{ port }}
  groupname: {{ grpn }}
  type: {{ type }}
   env : {{ env }}

Kindly suggest how I can achieve this??

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

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 .