# indexing variable list using a variable

**URL:** <https://forum.ansible.com/t/indexing-variable-list-using-a-variable/19218>\
**Category:** Ansible Project\
**Created:** [September 9, 2015, 10:10pm UTC](https://forum.ansible.com/t/indexing-variable-list-using-a-variable/19218 "2015-09-09T22:10:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [September 9, 2015, 10:10pm UTC](https://forum.ansible.com/t/indexing-variable-list-using-a-variable/19218/1 "2015-09-09T22:10:22Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [September 9, 2015, 10:12pm UTC](https://forum.ansible.com/t/indexing-variable-list-using-a-variable/19218/2 "2015-09-09T22:12:40Z")

</div>

set\_fact: mybackend="{{hostvars[inventory\_hostname]['db'][varwithbackendnumber]}}"

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [September 9, 2015, 10:25pm UTC](https://forum.ansible.com/t/indexing-variable-list-using-a-variable/19218/3 "2015-09-09T22:25:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [September 9, 2015, 10:38pm UTC](https://forum.ansible.com/t/indexing-variable-list-using-a-variable/19218/4 "2015-09-09T22:38:59Z")

</div>

hm, maybe I’d be better off splitting the different location vars into separate files, and then use

```auto
with_first_found

```

```auto
I could then loop over group_names until the node found a valid variable file. 

```

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [September 9, 2015, 10:59pm UTC](https://forum.ansible.com/t/indexing-variable-list-using-a-variable/19218/5 "2015-09-09T22:59:35Z")

</div>

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”

1. in each var file I used the following structure:

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [September 10, 2015, 1:15pm UTC](https://forum.ansible.com/t/indexing-variable-list-using-a-variable/19218/6 "2015-09-10T13:15:22Z")

</div>

i was going to say, you cannot reference lists by name  
so

db:  
&nbsp;&nbsp;- first:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stuff: lasdjkf  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;la: dida  
&nbsp;&nbsp;- second:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stuff: aslkdfjsk  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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:  
&nbsp;&nbsp;first:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stuff: lasdjkf  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;la: dida  
&nbsp;&nbsp;second:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stuff: aslkdfjsk  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;la: lala

now you can do db['first']
