Passing a list on the command line

4,144 views
Skip to first unread message

Rob White

unread,
Mar 26, 2015, 9:37:28 PM3/26/15
to ansible...@googlegroups.com
Is it possible to pass a list as an extra-vars variable on the command line?

I have tried various syntax but it doesn't work.

E.g.

ansible-playbook my_playbook.yml -i local --extra-vars roles_to_deploy=role1,role2
ansible-playbook my_playbook.yml -i local --extra-vars roles_to_deploy=[role1,role2]
ansible-playbook my_playbook.yml -i local --extra-vars roles_to_deploy=['role1','role2']


James Cammarata

unread,
Mar 27, 2015, 8:48:28 AM3/27/15
to ansible...@googlegroups.com
Hi Rob, this is possible, you just need to format the extra variables as JSON, as documented here: http://docs.ansible.com/playbooks_variables.html#passing-variables-on-the-command-line

James Cammarata
Director, Ansible Core Engineering
github: jimi-c

--
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/a1c3c3ee-f51e-42c1-99bf-677ef6eaa8e5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

O. T. Suarez

unread,
Mar 27, 2015, 2:06:38 PM3/27/15
to ansible...@googlegroups.com
Hi Rob,

Like James said, something like this should work:

ansible-playbook my_playbook.yml -i local  -e '{ "roles_to_deploy" : ['role1','role2']}'

An alternative approach using tags:

variables file: host_vars/ubuntu-trusty64-01
~~~
---
roles:
 - collectd
 - nginx
~~~

playbook file: setup.yml

~~~
  roles:
   - { role: collectd, when: "'collectd' in roles and roles is defined", tags: [ 'collectd' ]  }
   - { role: nginx, when: "'nginx' in roles and roles is defined", tags: [ 'nginx' ]  }
   - { role: mariadb, when: "'mariadb' in roles and roles is defined", tags: [ 'mariadb' ]  }
~~~

# to install roles collectd and nginx

~~~
ansible-playbook setup.yml --limit ubuntu-trusty64-01
~~~

# to install only role collectd

~~~
ansible-playbook setup.yml --limit ubuntu-trusty64-01 -t collectd
~~~

Keeping the list of roles allowed to be installed on a server in a variable enforces desired state.
Regards,
Osvaldo

Rob White

unread,
Mar 29, 2015, 7:33:25 PM3/29/15
to ansible...@googlegroups.com
Hmmm, this doesn't seem to work for the roles parameter.

I tried the following command:

ansible-playbook test.yml -i inventories/local -e '{"roles_to_deploy":["role1","role2"]}'

The test.yml playbook just had:

tasks:
   
- name: Debug
      debug
: msg="{{ item }}"
      with_items
:
       
"{{ roles_to_deploy }}"

All good.

I then tried the same command with my original playbook which has:

roles:
   
"{{ roles_to_deploy }}"

I get an error "ERROR: value of 'roles:' must be a list"

O. T. Suarez

unread,
Mar 30, 2015, 11:35:27 AM3/30/15
to ansible...@googlegroups.com
Hi Rob,


On Sun, Mar 29, 2015 at 8:33 PM, Rob White <robwh...@gmail.com> wrote:
Hmmm, this doesn't seem to work for the roles parameter.

I tried the following command:

ansible-playbook test.yml -i inventories/local -e '{"roles_to_deploy":["role1","role2"]}'

The test.yml playbook just had:

tasks:
   
- name: Debug
      debug
: msg="{{ item }}"
      with_items
:
       
"{{ roles_to_deploy }}"

All good.

I then tried the same command with my original playbook which has:

roles:
   
"{{ roles_to_deploy }}"


Couldn't find a way to make it work that way.
Here's an alternative:


  roles:
   - { role: role1, when 'role1' in roles_to_deploy}
   - { role: role2, when 'role2' in roles_to_deploy}

It might seems too much work to edit the playbook file each time you add a new role, but it's just a single line and when comparing the number of times you'll be executing that playbook file against the number of edits, it should worth the shot.

Regards,
Osvaldo

James Cammarata

unread,
Mar 30, 2015, 11:41:02 AM3/30/15
to ansible...@googlegroups.com
That error occurs because roles were not really designed to be included dynamically like that, so the value is not run through the template engine. This is similar to the problem of using includes + with_* loops in v1, where the variables could come from an inventory source, which would lead to errors or unexpected behavior, as inventory sources are not available at the time the roles are parsed/loaded.

I would recommend using Oswaldo's method, where available, or simple create more specific playbooks depending on your situation.

James Cammarata
Director, Ansible Core Engineering
github: jimi-c

--
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.
Reply all
Reply to author
Forward
0 new messages