Variable Substitution in a YAML List

3,173 views
Skip to first unread message

AmiableAlbion

unread,
Jan 16, 2014, 4:43:22 PM1/16/14
to ansible...@googlegroups.com
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

Michael DeHaan

unread,
Jan 16, 2014, 4:59:57 PM1/16/14
to ansible...@googlegroups.com
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.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Brian Coca

unread,
Jan 16, 2014, 5:31:18 PM1/16/14
to ansible...@googlegroups.com
FYI, { key: value } denotes a hash/dictionary, lists are  [ element1, element2 ], both should be templateable depending on context.


--
Brian Coca
Stultorum infinitus est numerus
0110000101110010011001010110111000100111011101000010000001111001011011110111010100100000011100110110110101100001011100100111010000100001
Pedo mellon a minno

Albion Baucom

unread,
Jan 16, 2014, 7:16:40 PM1/16/14
to ansible...@googlegroups.com
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:

--
- hosts:        AdminVM
  connection:   local
  gather_facts: false
  vars:
    centos-6.4: 52225cb3-441b-47b6-9cca-deb14d24d72f
  roles:
    - provision
  vars:
    hostlist:
      - { hostname: 'test-vm', groups: 'test-vm', image_id: '{{centos-6.4}}', security_group: 'default', flavor: 'n1.tiny' }

This throws python errors:

> ansible-playbook -i hosts vm.yml
Traceback (most recent call last):
  File "/usr/bin/ansible-playbook", line 268, in <module>
    sys.exit(main(sys.argv[1:]))
  File "/usr/bin/ansible-playbook", line 208, in main
    pb.run()
  File "/usr/lib/pymodules/python2.7/ansible/playbook/__init__.py", line 228, in run
    play = Play(self, play_ds, play_basedir)
  File "/usr/lib/pymodules/python2.7/ansible/playbook/play.py", line 90, in __init__
    ds = template(basedir, ds, self.vars)
  File "/usr/lib/pymodules/python2.7/ansible/utils/template.py", line 332, in template
    d[k] = template(basedir, v, vars, lookup_fatal, depth, expand_lists, fail_on_undefined=fail_on_undefined)
  File "/usr/lib/pymodules/python2.7/ansible/utils/template.py", line 332, in template
    d[k] = template(basedir, v, vars, lookup_fatal, depth, expand_lists, fail_on_undefined=fail_on_undefined)
  File "/usr/lib/pymodules/python2.7/ansible/utils/template.py", line 328, in template
    return [template(basedir, v, vars, lookup_fatal, depth, expand_lists, fail_on_undefined=fail_on_undefined) for v in varname]
  File "/usr/lib/pymodules/python2.7/ansible/utils/template.py", line 332, in template
    d[k] = template(basedir, v, vars, lookup_fatal, depth, expand_lists, fail_on_undefined=fail_on_undefined)
  File "/usr/lib/pymodules/python2.7/ansible/utils/template.py", line 310, in template
    varname = template_from_string(basedir, varname, vars, fail_on_undefined)
  File "/usr/lib/pymodules/python2.7/ansible/utils/template.py", line 512, in template_from_string
    res = jinja2.utils.concat(t.root_render_func(t.new_context(_jinja2_vars(basedir, vars, t.globals, fail_on_undefined), shared=True)))
  File "<template>", line 8, in root


--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/e4EUX3CbGso/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Albion Baucom
gRED IT Support
Pharma Informatics

Michael DeHaan

unread,
Jan 16, 2014, 7:43:11 PM1/16/14
to ansible...@googlegroups.com
If you can reproduce this on 1.4.X please file a ticket.

We generally consider any traceback a bug.

Thanks!


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--

Matt Martz

unread,
Jan 16, 2014, 7:50:09 PM1/16/14
to Albion Baucom, ansible...@googlegroups.com
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:

---
- hosts: AdminVM
  connection: local
  gather_facts: no
  vars:
    centos_64: 52225cb3-441b-47b6-9cca-deb14d24d72f
    hostlist:
      - hostname: test-vm
        groups: test-vm
        image_id: "{{ centos_64 }}"
        security_group: default
        flavor: n1.tiny
  roles:
    - role: provision

-- 
Matt Martz
ma...@sivel.net
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Albion Baucom

unread,
Jan 16, 2014, 11:43:34 PM1/16/14
to Matt Martz, Albion Baucom, ansible-project
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
Reply all
Reply to author
Forward
0 new messages