# Variable Substitution in a YAML List

**URL:** <https://forum.ansible.com/t/variable-substitution-in-a-yaml-list/14364>\
**Category:** Ansible Project\
**Created:** [January 16, 2014, 9:43pm UTC](https://forum.ansible.com/t/variable-substitution-in-a-yaml-list/14364 "2014-01-16T21:43:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AmiableAlbion](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@AmiableAlbion](https://forum.ansible.com/u/AmiableAlbion)\
**Post date:** [January 16, 2014, 9:43pm UTC](https://forum.ansible.com/t/variable-substitution-in-a-yaml-list/14364/1 "2014-01-16T21:43:22Z")

</div>

Is it possible to do a variable substitution in a YAML list. For instance

- { hostname: ‘myhost’, groups: ‘webservers’, image\_id: ‘{{image\_name}}’}

The above example does not work, at least using vars or includes that define “image\_name”.

What I am trying to do is pass VM parameters to OpenStack, and one of those is the image ID which is a very long non-sensical string. I would like to define those with human-readable names. Curiously the Nova compute module in Ansible doesn’t like the name, only the ID, where as command line nova will accept the name as well. Perhaps this is a limitation in YAML.

Thanks  
Albion

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [January 16, 2014, 9:59pm UTC](https://forum.ansible.com/t/variable-substitution-in-a-yaml-list/14364/2 "2014-01-16T21:59:57Z")

</div>

What you have is in fact fine, we’d have to see your playbook in context to say what’s up.

If hostname is meant to be something that Ansible recognizes, or groups, that’s not something ansible knows what to do with, which may be why you percieve something to not work.

Also please share any output you may have that makes you think the variable isn’t being used, as well as the version of Ansible you are using.

Playbooks most of all though.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [January 16, 2014, 10:31pm UTC](https://forum.ansible.com/t/variable-substitution-in-a-yaml-list/14364/3 "2014-01-16T22:31:18Z")

</div>

FYI, { key: value } denotes a hash/dictionary, lists are [ element1,  
element2 ], both should be templateable depending on context.

---

<div class="post-metadata">

**Author:** ![Albion\_Baucom](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@Albion\_Baucom](https://forum.ansible.com/u/Albion_Baucom)\
**Post date:** [January 17, 2014, 12:16am UTC](https://forum.ansible.com/t/variable-substitution-in-a-yaml-list/14364/4 "2014-01-17T00:16:40Z")

</div>

I am using version 1.3.3

Here is the main playbook with a simple experiment to set the variable “centos-6.4” to the image id. I would prefer to do it with a role variable file or include, but this eliminates any other issues and just highlights the variable substitution:

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [January 17, 2014, 12:43am UTC](https://forum.ansible.com/t/variable-substitution-in-a-yaml-list/14364/5 "2014-01-17T00:43:11Z")

</div>

If you can reproduce this on 1.4.X please file a ticket.

We generally consider any traceback a bug.

Thanks!

---

<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:** [January 17, 2014, 12:50am UTC](https://forum.ansible.com/t/variable-substitution-in-a-yaml-list/14364/6 "2014-01-17T00:50:09Z")

</div>

I cannot replicate the issue on 1.4, even exactly as you have it, but I would recommend a few changes regardless:

1. In general, variables cannot contain things like hyphens or periods, so trying to use centos-6.4 will be problematic
2. Consolidate you ‘vars’ sections, currently with 2 vars sections you are overwriting the first ‘vars’ section with the second one
3. Use standard YAML formatting instead of a hash like value (I think this will be more readable)

Here is what I would recommend:

---

<div class="post-metadata">

**Author:** ![Albion\_Baucom](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@Albion\_Baucom](https://forum.ansible.com/u/Albion_Baucom)\
**Post date:** [January 17, 2014, 4:43am UTC](https://forum.ansible.com/t/variable-substitution-in-a-yaml-list/14364/7 "2014-01-17T04:43:34Z")

</div>

Hi Matt, Michael,  
Thanks, Matts suggested refactoring works 1.4.4.

It is much cleaner now and does exactly what I wanted it to.

Thanks again for the help.  
Albion
