# Accessing elements in a list

**URL:** <https://forum.ansible.com/t/accessing-elements-in-a-list/17790>\
**Category:** Ansible Project\
**Created:** [March 19, 2015, 3:20pm UTC](https://forum.ansible.com/t/accessing-elements-in-a-list/17790 "2015-03-19T15:20:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Vosbury](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@David\_Vosbury](https://forum.ansible.com/u/David_Vosbury)\
**Post date:** [March 19, 2015, 3:20pm UTC](https://forum.ansible.com/t/accessing-elements-in-a-list/17790/1 "2015-03-19T15:20:19Z")

</div>

I’m not sure if this is the right mailing list group to ask general questions or if this is just for developers. If not, then let me know what the correct group is. Anyway, my question is I’m trying to update some old playbooks to conform to the newer syntax for variables ie. {{ foo }} instead of $foo. In many of these playbooks I pass in a variable from a master includes playbook to sub-playbooks and I then use that variable as a positional parameter to access a specific element in a list. So instead of writing ${servername[0]} I do ${servername[$iteration]} where iteration is my position in the list I want to access in my vars file. I may just rewrite the whole bunch of playbooks to accomplish my task differently but for now I’d just like to update my playbooks to use the newer standard. (Since updating Ansible last time I get the warning that the old variable style will not be supported soon) I don’t want to upgrade and find out all my playbooks don’t work anymore.

Anyway, I’ve been trying to figure out how to change the ${servername[$iteration]} to meet the new standards and just can’t figure it out. Does anybody know how to do this now?

---

<div class="post-metadata">

**Author:** ![David\_Vosbury](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@David\_Vosbury](https://forum.ansible.com/u/David_Vosbury)\
**Post date:** [March 20, 2015, 4:56am UTC](https://forum.ansible.com/t/accessing-elements-in-a-list/17790/2 "2015-03-20T04:56:03Z")

</div>

Since nobody has answered back on this post, does my question not make sense or need more explanation? Or does no one know the answer?

---

<div class="post-metadata">

**Author:** ![Dmitry\_Malinovsky](https://avatars.discourse-cdn.com/v4/letter/d/ecccb3/32.png) [@Dmitry\_Malinovsky](https://forum.ansible.com/u/Dmitry_Malinovsky)\
**Post date:** [March 20, 2015, 6:54am UTC](https://forum.ansible.com/t/accessing-elements-in-a-list/17790/3 "2015-03-20T06:54:03Z")

</div>

Hi David,

Try {{ servername[iteration] }}

---

<div class="post-metadata">

**Author:** ![David\_Vosbury](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@David\_Vosbury](https://forum.ansible.com/u/David_Vosbury)\
**Post date:** [March 20, 2015, 12:44pm UTC](https://forum.ansible.com/t/accessing-elements-in-a-list/17790/4 "2015-03-20T12:44:44Z")

</div>

Dmitry,

I think I already tried that, but I went ahead and tried it again. I get back this error when setting iteration=0 in my calling includes playbook:

fatal: [servername here] =\> One or more undefined variables: ‘list’ object has no attribute ‘0’

---

<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:** [March 20, 2015, 12:55pm UTC](https://forum.ansible.com/t/accessing-elements-in-a-list/17790/5 "2015-03-20T12:55:30Z")

</div>

that means that your list is empty and has no elements, you could  
check that before hand by looking at list|length

---

<div class="post-metadata">

**Author:** ![David\_Vosbury](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@David\_Vosbury](https://forum.ansible.com/u/David_Vosbury)\
**Post date:** [March 20, 2015, 1:17pm UTC](https://forum.ansible.com/t/accessing-elements-in-a-list/17790/6 "2015-03-20T13:17:38Z")

</div>

But I’m referencing the same list/vars file that I’ve always used with this script. I have data in it. If I actually put in the script {{ servername[0] }} it pulls the first item in the list.yml file and the script works. But when I try to do a variable substitution for the position in the list it gives me the error. When I use the old syntax ${servername[$iteration]} it works. Could this be a bug or am I just missing something here?

---

<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:** [March 20, 2015, 1:21pm UTC](https://forum.ansible.com/t/accessing-elements-in-a-list/17790/7 "2015-03-20T13:21:52Z")

</div>

I just tested this and i cannot reproduce your issue, i would need to  
see the actual template having the problem

---

<div class="post-metadata">

**Author:** ![David\_Vosbury](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@David\_Vosbury](https://forum.ansible.com/u/David_Vosbury)\
**Post date:** [March 20, 2015, 3:08pm UTC](https://forum.ansible.com/t/accessing-elements-in-a-list/17790/8 "2015-03-20T15:08:28Z")

</div>

Here is an example of one of the scripts where I use this method of calling an item from a list where it works fine using the old $var syntax but fails when changing to the jinja2 style variables.

First I call the script from my main .yml file that is a bunch of includes

- include: vm\_provision.yml [server=vcenter.mydomain.com](http://server=vcenter.mydomain.com) [username=vma@mydomain.com](mailto:username=vma@mydomain.com) [vmhost=esxi1.mydomain.com](http://vmhost=esxi1.mydomain.com) vmhost\_destination=[esxi1.mydomain.com](http://esxi1.mydomain.com) template=‘vm-ubuntu124apl-x’ datastore=‘iSCSI-datastore-1’ iteration=0

That in turn calls this script

---

<div class="post-metadata">

**Author:** ![David\_Vosbury](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@David\_Vosbury](https://forum.ansible.com/u/David_Vosbury)\
**Post date:** [March 20, 2015, 3:15pm UTC](https://forum.ansible.com/t/accessing-elements-in-a-list/17790/9 "2015-03-20T15:15:23Z")

</div>

I should also include a copy of the script as it works today.
