# How to use a variable to define a list (to use in docker\_container volumes)

**URL:** <https://forum.ansible.com/t/how-to-use-a-variable-to-define-a-list-to-use-in-docker-container-volumes/30993>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [August 8, 2019, 7:32pm UTC](https://forum.ansible.com/t/how-to-use-a-variable-to-define-a-list-to-use-in-docker-container-volumes/30993 "2019-08-08T19:32:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roger\_Firpo](https://avatars.discourse-cdn.com/v4/letter/r/958977/32.png) [@Roger\_Firpo](https://forum.ansible.com/u/Roger_Firpo)\
**Post date:** [August 8, 2019, 7:32pm UTC](https://forum.ansible.com/t/how-to-use-a-variable-to-define-a-list-to-use-in-docker-container-volumes/30993/1 "2019-08-08T19:32:00Z")

</div>

I’m trying to setup a docker container from ansible with its volumes defined in a yml file passed as extra\_vars.  
The setup is quite simple:

config.yml

`  
shared\_folders:

- /Users/roger/Code/one:/one
- /Users/roger/Code/two:/two

`

inventory.yml

`all: hosts: docker: vars: ansible_connection: docker`

playbook.yml

`

- name: Create our container  
hosts: localhost  
tasks:

- name: Debug shared\_folders  
debug:  
msg: ‘{{ shared\_folders }}’

- debug: msg=“{{ item }}”  
with\_items: ‘{{shared\_folders}}’

- name: Create our container  
hosts: localhost  
tasks:

- docker\_container:  
name: docker  
hostname: docker  
image: ‘ubuntu:trusty’  
command: [“sleep”, “1d”]  
devices: ‘{{ shared\_folders }}’  
`

the debug shows the proper info, but the devices parameter is not rendered properly.

`

> ansible-playbook playbook.yml -i inventory.yml --extra-vars “@config.yml”

PLAY [Create our container] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [Debug shared\_folders] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: [  
“/Users/roger/Code/one:/one”,  
“/Users/roger/Code/two:/two”  
]  
}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item=/Users/roger/Code/one:/one) =\> {  
“msg”: “/Users/roger/Code/one:/one”  
}  
ok: [localhost] =\> (item=/Users/roger/Code/two:/two) =\> {  
“msg”: “/Users/roger/Code/two:/two”  
}

PLAY [Create our container] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [docker\_container] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {“changed”: false, “msg”: “Error starting container 4dd1d94a025a0104a90713c528a8bac4b1c07241f91a6838127dced2383ad7fe: 500 Server Error: Internal Server Error ("b’{"message":"error gathering device information while adding custom device \\"/Users/roger/Code/one\\": not a device node"}'")”}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=4 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

`

Looks like each list member is extra escaped.

Any idea how to overcome this problem?

Thanks!

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [August 8, 2019, 7:52pm UTC](https://forum.ansible.com/t/how-to-use-a-variable-to-define-a-list-to-use-in-docker-container-volumes/30993/2 "2019-08-08T19:52:43Z")

</div>

Hi,

> I'm trying to setup a docker container from ansible with its volumes  
> defined in a yml file passed as extra\_vars.

are you trying to mount these directories as volumes / mounts in the  
docker\_container, or are /Users/roger/Code/one and  
/Users/roger/Code/two actual device files  
([https://en.wikipedia.org/wiki/Device\_file](https://en.wikipedia.org/wiki/Device_file)) which should be made  
available in the container?

When using the "devices:" parameter of docker\_container, you tell  
docker to treat them as proper devices. The docker daemon complains  
that /Users/roger/Code/one does not seem to be a device file.

In case /Users/roger/Code/one and /Users/roger/Code/two are regular  
directories (or files), you want to use the "volumes:" parameter of  
docker\_container. That will simply mount them into the container.

Cheers,  
Felix

---

<div class="post-metadata">

**Author:** ![Roger\_Firpo](https://avatars.discourse-cdn.com/v4/letter/r/958977/32.png) [@Roger\_Firpo](https://forum.ansible.com/u/Roger_Firpo)\
**Post date:** [August 8, 2019, 8:04pm UTC](https://forum.ansible.com/t/how-to-use-a-variable-to-define-a-list-to-use-in-docker-container-volumes/30993/3 "2019-08-08T20:04:42Z")

</div>

Hi,

you’re absolutely right!

I tested the setup setting the volumes variable and the shared volumes explicitly at some point,

but somehow I changed the key to devices and I assumed it was a problem with the variable.

Thanks a lot!  
Roger

El dijous, 8 agost de 2019 21:52:57 UTC+2, Felix Fontein va escriure:
