How to use a variable to define a list (to use in docker_container volumes)

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!

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) 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

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: