Re: [ansible-project] How to access group variables of the same host in different groups?

2020-05-20 Thread Brian Coca
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
-- 
--
Brian Coca

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CACVha7dJeyThrcaioi4SO785omnKU9hk%3DE905ziomZs7ho8m9w%40mail.gmail.com.


[ansible-project] How to access group variables of the same host in different groups?

2020-05-20 Thread Durai Arasan


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.


-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/14f0a25f-f0d3-4f95-8ffd-fd143348529d%40googlegroups.com.