How to access group variables of the same host in different groups?

This is my ansible inventory file:

[slurm_master]
slurm-gpu-0 ansible_host=192.168.0.24 ansible_connection=local

[slurm_compute]
slurm-gpu-1 ansible_host=192.168.0.200 cpus=16 gpus=2
slurm-gpu-2 ansible_host=192.168.0.124 cpus=1 gpus=0

[slurm_partition_gpu]                                                                               
slurm-gpu-1

[slurm_partition_gpu:vars]
allow_accounts=['whitelist','gpu_users']
default_partition=no

[slurm_partition_debug]
slurm-gpu-1
slurm-gpu-2

[slurm_partition_debug:vars]
allow_accounts=['whitelist']
default_partition='yes'

[allnodes:children]
slurm_master
slurm_compute

[allnodes:vars]
slurm_partitions=['slurm_partition_gpu','slurm_partition_debug']

I have 2 partitions - “slurm_partition_gpu” and “slurm_partition_debug”. All partitions have the properties “allow_accounts” and “default_partition”. So obviously these variables have the same name across partitions. Also a host is common to both partitions (groups). When I try to access these using variables using hostvars the variable evaluates (incorrectly) to the first occurrence in the host file. For example, inside jinja2 templating:

{{ hostvars[groups['slurm_partition_debug'][0]].allow_accounts }}

results in:

['whitelist','gpu_users']

instead of expected:

['whitelist']

This is obviously because it is looking for “slurm-gpu-1.allow_accounts” and because slurm-gpu-1 is present in both partitions (groups) it is going for the first occurrence instead of discriminating between partitions.

How do I get ansible to recognize that the variable is in a different group even though it is associated with the same host name? I would like to take advantage of the fact that even though the variables and host are same, the group name is unique and hence would like to use the group name to access the variables.

They don't exist outside inventory, all variables are either per host
or per play object, inventory always gets flattened to the host, so if
a host is part of 2 groups and both have the same variable, you will
only get 1 value

https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#group-variables