Variable Substitution in a YAML List

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

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.

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

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:

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

We generally consider any traceback a bug.

Thanks!

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:

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