# Passing a list on the command line

**URL:** <https://forum.ansible.com/t/passing-a-list-on-the-command-line/17854>\
**Category:** Ansible Project\
**Created:** [March 27, 2015, 1:37am UTC](https://forum.ansible.com/t/passing-a-list-on-the-command-line/17854 "2015-03-27T01:37:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rob\_White](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@Rob\_White](https://forum.ansible.com/u/Rob_White)\
**Post date:** [March 27, 2015, 1:37am UTC](https://forum.ansible.com/t/passing-a-list-on-the-command-line/17854/1 "2015-03-27T01:37:28Z")

</div>

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’]

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [March 27, 2015, 12:48pm UTC](https://forum.ansible.com/t/passing-a-list-on-the-command-line/17854/2 "2015-03-27T12:48:24Z")

</div>

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](http://docs.ansible.com/playbooks_variables.html#passing-variables-on-the-command-line)

---

<div class="post-metadata">

**Author:** ![O\_T\_Suarez](https://avatars.discourse-cdn.com/v4/letter/o/c89c15/32.png) [@O\_T\_Suarez](https://forum.ansible.com/u/O_T_Suarez)\
**Post date:** [March 27, 2015, 6:06pm UTC](https://forum.ansible.com/t/passing-a-list-on-the-command-line/17854/3 "2015-03-27T18:06:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rob\_White](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@Rob\_White](https://forum.ansible.com/u/Rob_White)\
**Post date:** [March 29, 2015, 11:33pm UTC](https://forum.ansible.com/t/passing-a-list-on-the-command-line/17854/4 "2015-03-29T23:33:25Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![O\_T\_Suarez](https://avatars.discourse-cdn.com/v4/letter/o/c89c15/32.png) [@O\_T\_Suarez](https://forum.ansible.com/u/O_T_Suarez)\
**Post date:** [March 30, 2015, 3:35pm UTC](https://forum.ansible.com/t/passing-a-list-on-the-command-line/17854/5 "2015-03-30T15:35:03Z")

</div>

Hi Rob,

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [March 30, 2015, 3:40pm UTC](https://forum.ansible.com/t/passing-a-list-on-the-command-line/17854/6 "2015-03-30T15:40:59Z")

</div>

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.
