Hi guys,
I’m testing out some variable logic here, would really appreciate some help.
I have the following content in one of my vars_files:
set_fact: mybackend="{{hostvars[inventory_hostname]['db'][varwithbackendnumber]}}"
THanks Brian. I can’t seem to get this working. I can access the individual items using indexing
(this works:
- set_fact: mybackend=“{{ db[0] }}”
)
But I can’t for the life of me figure out how to access by “item name” (or if that’s even a valid thing in yaml). I can obviously redo the way I format the contents of my variable file, but I can’t seem to strike gold. Struggling to find a good example of this in the docs/examples.
hm, maybe I’d be better off splitting the different location vars into separate files, and then use
with_first_found
I could then loop over group_names until the node found a valid variable file.
Just for reference, this is what I ended up doing:
- in my play, I created separate vars files for each site, these correspond to the current host’s group membership:
- include_vars: “{{ item }}”
with_first_found: - “db_{{ autogroup2 }}.yml”
- in each var file I used the following structure:
i was going to say, you cannot reference lists by name
so
db:
- first:
stuff: lasdjkf
la: dida
- second:
stuff: aslkdfjsk
la: lala
is a list, if you wanted to reference by name you need to make it a
hash/dictionary so you can only get first through db[0].
db:
first:
stuff: lasdjkf
la: dida
second:
stuff: aslkdfjsk
la: lala
now you can do db['first']