# Ansible: How to use a Variable as an Index?

**URL:** <https://forum.ansible.com/t/ansible-how-to-use-a-variable-as-an-index/18999>\
**Category:** Ansible Project\
**Created:** [August 11, 2015, 9:19pm UTC](https://forum.ansible.com/t/ansible-how-to-use-a-variable-as-an-index/18999 "2015-08-11T21:19:52Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![lilferna](https://avatars.discourse-cdn.com/v4/letter/l/838e76/32.png) [@lilferna](https://forum.ansible.com/u/lilferna)\
**Post date:** [August 11, 2015, 9:19pm UTC](https://forum.ansible.com/t/ansible-how-to-use-a-variable-as-an-index/18999/1 "2015-08-11T21:19:52Z")

</div>

Hi Ansible Users,

I am trying to use a variable passed in via “–extra-vars” to index into ‘nics’ which is defined in nics.yml.

It works fine in the “when” clause, but not in the “set\_fact”. Any suggestions?

Thanks,  
Lilian

host# cat nics.yml

---

<div class="post-metadata">

**Author:** ![Timothy\_Appnel1](https://avatars.discourse-cdn.com/v4/letter/t/278dde/32.png) [@Timothy\_Appnel1](https://forum.ansible.com/u/Timothy_Appnel1)\
**Post date:** [August 11, 2015, 10:48pm UTC](https://forum.ansible.com/t/ansible-how-to-use-a-variable-as-an-index/18999/2 "2015-08-11T22:48:44Z")

</div>

Don’t use nested mustaches. In other words try this:

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [August 12, 2015, 12:38am UTC](https://forum.ansible.com/t/ansible-how-to-use-a-variable-as-an-index/18999/3 "2015-08-12T00:38:02Z")

</div>

> Don't use nested mustaches.

This should probably be a FAQ. :^) FWIW, what helped me understand this is  
that the mustaches aren't saying "here's a variable" (in which case you  
might think, incorrectly, that you need to put them around all variables  
all the time everywhere), but rather "start parsing this as Jinja", and  
that once you say that, it keeps happening until you close the mustaches  
(which means "stop parsing this as Jinja"). One of the things that happens  
when you're parsing as Jinja is that variables get expanded, but thinking  
of it as "start Jinja" and "stop Jinja" helped me keep it straight when I  
first started with this stuff, anyway.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.

---

<div class="post-metadata">

**Author:** ![Timothy\_Appnel1](https://avatars.discourse-cdn.com/v4/letter/t/278dde/32.png) [@Timothy\_Appnel1](https://forum.ansible.com/u/Timothy_Appnel1)\
**Post date:** [August 12, 2015, 1:34am UTC](https://forum.ansible.com/t/ansible-how-to-use-a-variable-as-an-index/18999/4 "2015-08-12T01:34:17Z")

</div>

That is a good way of thinking of explaining it in the context of Ansible – start Jinja, stop Jinja. At the same time, how would you have represented this in a Jinja template? Jinja doesn’t allow for nested mustaches in any context that I am aware of.

I’m sure a patch to the docs to make light of this would be appreciated.

---

<div class="post-metadata">

**Author:** ![lilferna](https://avatars.discourse-cdn.com/v4/letter/l/838e76/32.png) [@lilferna](https://forum.ansible.com/u/lilferna)\
**Post date:** [August 12, 2015, 4:27pm UTC](https://forum.ansible.com/t/ansible-how-to-use-a-variable-as-an-index/18999/5 "2015-08-12T16:27:41Z")

</div>

Hi Guys,

Thank you for the responses. I should have mentioned this - I had already tried several combinations (including the suggestion above) with and without nested {{ }}, but still can’t get a working syntax. Please see below for some attempts and their results.

Putting an example/note in the docs would be really helpful as well.

Thanks,

Lilian

host# ansible-playbook test.yml --extra-vars “num=0”

Format Attempt:

cvmip: “{{nics[num].ip}}”

Result:

TASK: [Static IP Case - Find out Static Overlay IP] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [127.0.0.1] =\> One or more undefined variables: ‘list object’ has no attribute u’0’

Format Attempt:

cvmip: “{{nics[‘num’].ip}}”

Result:

TASK: [Static IP Case - Find out Static Overlay IP] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [127.0.0.1] =\> One or more undefined variables: ‘list object’ has no attribute ‘num’

Format Attempt:

cvmip: “{{nics}}”[“{{num}}”].ip}}

Result:

ERROR: Syntax Error while loading YAML script, test.yml

---

<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:** [August 12, 2015, 5:09pm UTC](https://forum.ansible.com/t/ansible-how-to-use-a-variable-as-an-index/18999/6 "2015-08-12T17:09:46Z")

</div>

sounds like nics is an empty list, your first example is correct syntax, the rest are not.

---

<div class="post-metadata">

**Author:** ![lilferna](https://avatars.discourse-cdn.com/v4/letter/l/838e76/32.png) [@lilferna](https://forum.ansible.com/u/lilferna)\
**Post date:** [August 12, 2015, 6:08pm UTC](https://forum.ansible.com/t/ansible-how-to-use-a-variable-as-an-index/18999/7 "2015-08-12T18:08:05Z")

</div>

Hi Brian,

nics.yml is not an empty list - as I pointed out, in the “when:” clause, the playbook correctly detects it as type static:

host# cat nics.yml

---

<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:** [August 12, 2015, 6:09pm UTC](https://forum.ansible.com/t/ansible-how-to-use-a-variable-as-an-index/18999/8 "2015-08-12T18:09:28Z")

</div>

when are you setting num?

---

<div class="post-metadata">

**Author:** ![lilferna](https://avatars.discourse-cdn.com/v4/letter/l/838e76/32.png) [@lilferna](https://forum.ansible.com/u/lilferna)\
**Post date:** [August 12, 2015, 6:19pm UTC](https://forum.ansible.com/t/ansible-how-to-use-a-variable-as-an-index/18999/9 "2015-08-12T18:19:27Z")

</div>

Let me paste the output of all files & the invoking call again here:

# ansible-playbook test.yml --extra-vars “num=0”

[SNIP]

TASK: [Static IP Case - Find out Static Overlay IP] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [127.0.0.1] =\> One or more undefined variables: ‘list object’ has no attribute u’0’

# cat nics.yml

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [August 12, 2015, 6:46pm UTC](https://forum.ansible.com/t/ansible-how-to-use-a-variable-as-an-index/18999/10 "2015-08-12T18:46:13Z")

</div>

I believe the original error holds the answer:

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

That states that the list object has not attribute of the Unicode string “0”.

So “num” appears to be a string and needs to be an int.

Try using

“{{ nics[num|int].ip }}”

The |int will cast the string to an int.

---

<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:** [August 12, 2015, 7:37pm UTC](https://forum.ansible.com/t/ansible-how-to-use-a-variable-as-an-index/18999/11 "2015-08-12T19:37:48Z")

</div>

also, change the conditional:  
when: nics[num|int].type == “static”

---

<div class="post-metadata">

**Author:** ![lilferna](https://avatars.discourse-cdn.com/v4/letter/l/838e76/32.png) [@lilferna](https://forum.ansible.com/u/lilferna)\
**Post date:** [August 12, 2015, 8:51pm UTC](https://forum.ansible.com/t/ansible-how-to-use-a-variable-as-an-index/18999/12 "2015-08-12T20:51:41Z")

</div>

Hello,

That was indeed the problem!! The play works great now. Thanks so much for your help everyone.

It would be excellent if we could also add a note to the docs about nested moustaches.

Thanks again,  
Lilian
