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

15 views
Skip to first unread message

pnagar...@gmail.com

unread,
Jan 19, 2019, 11:04:47 AM1/19/19
to Ansible Project
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??

Mauricio Tavares

unread,
Jan 19, 2019, 11:35:38 PM1/19/19
to ansible...@googlegroups.com
Have you searched previous posts in the mailing list? Have you
read the loop examples in the ansible docs?

> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a63c9aa4-fd0b-41ce-ad5a-3ddda6a49c0e%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Nagaraj P

unread,
Jan 20, 2019, 3:14:49 AM1/20/19
to ansible...@googlegroups.com
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 . 

Reply all
Reply to author
Forward
0 new messages