How do I pass a list as an --extra-vars

6,907 views
Skip to first unread message

Mikael Sandström

unread,
Dec 11, 2014, 12:22:20 PM12/11/14
to ansible...@googlegroups.com
Hi,

I got a list that looks like this

listname:                  
      - name: ble
        version: 1
        path: /some/path
        ...
        ...


which is accessed as

- name: do something
  shell: run some command
  with_items: listname


If I want to pass the list to a playbook as an --extra-vars, how would I do that?

regards
/M

James Cammarata

unread,
Dec 11, 2014, 12:36:19 PM12/11/14
to ansible...@googlegroups.com
Hi Mikael,

There are essentially two ways you can do this:

1) Use quoted JSON
2) Put the data in either a JSON or YAML file.

Please refer to the documentation here for more details: http://docs.ansible.com/playbooks_variables.html#passing-variables-on-the-command-line

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/bd3b24c6-0936-41a6-87ed-581142f0b354%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mikael Sandström

unread,
Dec 11, 2014, 2:30:51 PM12/11/14
to ansible...@googlegroups.com
Thanks!

I cant get it to behave as a 'normal' list though, so obviously I'm doing something wrong.

If I run this playbook without extra vars:


---
- hosts: localhost
  connection
: local
  gather_facts
: false
  vars
:
    listname
:                  
     
- name: ble
        version
: 1
        path
: /some/path

  tasks
:
 
- name: test extravars
    debug
: msg="version - {{ item.version }}"
   
#debug: msg=""
    with_items
: listname



It works as expected, and I get this:

TASK: [test extravars] ******************************************************** 
ok: [localhost] => (item={'path': '/some/path', 'version': 1, 'name': 'ble'}) => {
    "item": {
        "name": "ble", 
        "path": "/some/path", 
        "version": 1
    }, 
    "msg": "version - 1"
}


However, if I try to do this 

ansible-playbook extravar.yml -e '{"listname":["name: foo, path: /asd/bsdfr, version: 2"]}'


PLAY
[localhost] **************************************************************


TASK
: [test extravars] ********************************************************
fatal
: [localhost] => One or more undefined variables: 'unicode object' has no attribute 'version'


And I guess that is because everything in 'item' is treated like one string



TASK
: [test extravars] ********************************************************
ok
: [localhost] => (item=name: foo, path: /asd/bsdfr, version: 2) => {
   
"item": "name: foo, path: /asd/bsdfr, version: 2",
   
"msg": ""
}


If I quote each pair, I get 3 different 'items'

ansible-playbook extravar.yml -e '{"listname":["name: foo", "path: /asd/bsdfr", "version: 2"]}'


PLAY
[localhost] **************************************************************


TASK
: [test extravars] ********************************************************
ok
: [localhost] => (item=name: foo) => {
   
"item": "name: foo",
   
"msg": ""
}
ok
: [localhost] => (item=path: /asd/bsdfr) => {
   
"item": "path: /asd/bsdfr",
   
"msg": ""
}
ok
: [localhost] => (item=version: 2) => {
   
"item": "version: 2",
   
"msg": ""



So, how should the quotes be placed to get it to behave like a 'normal' list? Or is there something else I need to do?

regards
/M

Michael DeHaan

unread,
Dec 11, 2014, 2:44:48 PM12/11/14
to ansible...@googlegroups.com
JSON needs to be valid JSON :)



Mikael Sandström

unread,
Dec 11, 2014, 3:10:23 PM12/11/14
to ansible...@googlegroups.com
Well,  I guess that helps ;-) it's been a long day...

All good now, thanks!
/M

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/Mm9qtgOrq7E/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.

Mikael Sandström

unread,
Dec 11, 2014, 3:56:23 PM12/11/14
to ansible...@googlegroups.com
And just for completeness sake:

ansible-playbook extravar.yml -e '{"listname":[{"name":"foo","path":"/asd/bsdfr", "version":"2"}]}'


PLAY
[localhost] **************************************************************


TASK
: [test extravars] ********************************************************

ok
: [localhost] => (item={u'path': u'/asd/bsdfr', u'version': u'2', u'name': u'foo'}) => {
   
"item": {
       
"name": "foo",
       
"path": "/asd/bsdfr",
       
"version": "2"
   
},
   
"msg": "version - 2"
}


Thanks!
/M
Reply all
Reply to author
Forward
0 new messages