Position of a host inside an inventory group

77 views
Skip to first unread message

Alejandro Comisario

unread,
Sep 10, 2015, 8:25:30 AM9/10/15
to Ansible Project, in...@nubeliu.com
Hi everyone, so happy to be here, this is my first post.
Here's what i need.

I have two groups:

[group_master]
host1
host2
host3

[group_slaves]
host4
host5
host6

i need to create a template that reflect a pair of "couples" like 
host1 and host4
host2 and host4
host3 and host6

for that i thought going through the group_master and then do something like this on the template.

possition_in_the_group = ANSIBLE_VARIABLE_THAT_DONT_KNOW_IF_EXISTS
master = {{ ansible_hostname }}
slave = {{ groups['group_slaves'][possition_in_the_group] }}

so, can i know through a simple variable and not having to jinja "script" to know it, the position of the host im running the task on, has in the group it belongs ?
thanks so much !

Brian Coca

unread,
Sep 10, 2015, 9:35:29 AM9/10/15
to Ansible Project, in...@nubeliu.com
i think you want:
groups['group_slaves'][groups['group_masters].index(inventory_hostname)]

supposing you are using hosts: group_masters, reverse it if you are
looping through slaves.
> --
> 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/cccab836-89c4-4ad7-a514-5d422d31686a%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca

Alejandro Comisario

unread,
Sep 10, 2015, 10:43:10 AM9/10/15
to Ansible Project
Brian, that's exactly what i needed !!
thanks so much !
Reply all
Reply to author
Forward
0 new messages