indexing variable list using a variable

83 views
Skip to first unread message

Trond Hindenes

unread,
Sep 9, 2015, 6:10:22 PM9/9/15
to Ansible Project
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:
---
db:
  - backend_oslo:
      - name: db01
        primary: server1
        secondaries:
          - server2
          - server3
  - backend_stockholm:
      - name: db02
        primary: server4
        secondaries:
          - server5
          - server6

At a certain point in my playbook I have for instance "backend_oslo" in a fact (I'm picking up this thru group_names earlier in the play)
I'd like to be able to register the "contents" of the "backend_oslo" item in a dynamic fact so that I can access:
      - name: db01
        primary: server1
        secondaries:
          - server2
          - server3

This is an exercise in trying to keep my intentory files as light as possible. I'm not 100% comfortable with the list logic of yaml and how to index into them dynamically. Is what I'm trying to do even possible using the built-in filters?

Brian Coca

unread,
Sep 9, 2015, 6:12:43 PM9/9/15
to Ansible Project
set_fact: mybackend="{{hostvars[inventory_hostname]['db'][varwithbackendnumber]}}"




--
Brian Coca

Trond Hindenes

unread,
Sep 9, 2015, 6:25:14 PM9/9/15
to Ansible Project
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.

Trond Hindenes

unread,
Sep 9, 2015, 6:38:59 PM9/9/15
to Ansible Project
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. 

Trond Hindenes

unread,
Sep 9, 2015, 6:59:35 PM9/9/15
to Ansible Project
Just for reference, this is what I ended up doing:
1. 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"

2. in each var file I used the following structure:
---
  databases: 
    - 
      name: "DB1"
      primary: "Server1"
      secondaries: null
    - 
      name: "DB2"
      primary: "Server1"
      secondaries: null

3. this allowed me to access for example each db's name like so:
    - name: print stufs
      debug: msg="{{ item }}.name"
      with_items: databases

Brian Coca

unread,
Sep 10, 2015, 9:15:28 AM9/10/15
to Ansible Project
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']

--
Brian Coca
Reply all
Reply to author
Forward
0 new messages