1

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.

1 Answer 1

0

How do I get ansible to recognize that the variable is in a different group even though it has the same name? Also the additional difficulty is that the host name is also identical in both partitions.

Looks like you have stepped onto the realm of How variables are merged. You can either rename the groups to change ansible's default variable merge order or use ansible_group_priority to define how merging should happen. For example, using ansible_group_priority like below you can tell ansible to give higher priority to slurm_partition_debug group than slurm_partition_gpu. Higher value of ansible_group_priority gives higher priority and the default is 1 (if not specified).

[slurm_partition_debug:vars]
allow_accounts=['whitelist']
default_partition='yes'
ansible_group_priority=2
1
  • This is useful when the number of groups is low. But It may happen that the number of partitions (i.e. groups) may be large (and the same host may exist in multiple groups), so defining priority for each group might be tedious. I am looking for a solution where you can reference the variable through the group name. I want to take advantage of the fact that even though the variables and host(s) are same, the group name is unique and hence would like to use the group name to access the variables. May 19, 2020 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.