Access dynamically loaded variables by name

15 views
Skip to first unread message

Uwe Sauter

unread,
Aug 14, 2017, 12:44:59 PM8/14/17
to Ansible Project
Hi again,

I'm looking for a way to merge multiple lists into one. Difficulty is that the lists have dynamic names.

E.g.

#### cluster.yaml ####
---
racks: ['rack01', 'rack02'] # might be many more racks
######################

#### rack01.yaml ####
---
rack_number: 1
compute_nodes: ['alpha', 'bravo']
#####################

#### rack02.yaml ####
---
rack_number: 2
compute_nodes: ['charlie', 'delta']
#####################

#### playbook ####
---
- hosts: localhost
connection: local
gather_facts: false
tasks:
- include_vars:
file: cluster.yaml
name: cluster

- include_vars:
file: '{{ item }}.yaml'
name: '{{ item }}'
with_items: '{{ cluster.racks }}'

- set_fact: all_nodes='some powerful magic that merges rack01.compute_nodes … rackNN.compute_nodes'

- debug: var=all_nodes
##################

Expected result: all_nodes: ['alpha', 'bravo', 'charlie', 'delta']


This snippet doesn't work due to undefined variables (which I was trying to capture with default filter):

####
- set_fact: all_nodes='{{ all_nodes | default(None) }} + {{ item.compute_nodes }}'
with_items: '{{ cluster.racks }}'
####

But I suspect that the undefined variable isn't "all_nodes" but that item (cluster.racks.[X]) is a string and not the
rackXX variable.

So the question probably is "how to access a variable who's name is stored in another variable"? And how to do so in a loop?

Any suggestions on how to implement this in a different way?

Regards,

Uwe

Reply all
Reply to author
Forward
0 new messages