Position of a host inside an inventory group

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 !

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.

Brian, that’s exactly what i needed !!
thanks so much !