Question: I run a Ansible Playbook that gathers machine specific information and store the date within a file. One for each host. So I end up with a buch of files which should now be send to my Docker-Based Application for further processing.

Actuall I need to store it in a specific folder and create a volume so the container is able to read the files.

This requires the existence/creation of /tmp/incoming ...
Now if the Monitor app gets moved or a second Instance is needed, you'll have to access the filesystem and create the dirctory.

So I'd like to create a more dynamic volume:

```
docker volume create --name monitor-incoming:/var/www/monitor/incoming

```

Now Docker containers will be able to access the volume. But can I use Ansible to "copy" the files to this remote volume ? Sending them to monitor-incoming instead of /tmp/incoming ?

Answer: 
You could use any of the following methods:

1. Ansible non-SSH docker connection with standard Ansible `file/copy` modules. Example taken from from [Ansible documentation](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#non-ssh-connection-types):

```default
- name: create jenkins container
  docker_container:
    docker_host: myserver.net:4243
    name: my_jenkins
    image: jenkins

- name: add container to inventory
  add_host:
    name: my_jenkins
    ansible_connection: docker
    ansible_docker_extra_args: "--tlsverify --tlscacert=/path/to/ca.pem --tlscert=/path/to/client-cert.pem --tlskey=/path/to/client-key.pem -H=tcp://myserver.net:4243"
    ansible_user: jenkins
  changed_when: false

- name: create directory for ssh keys
  delegate_to: my_jenkins
  file:
    path: "/var/jenkins_home/.ssh/jupiter"
    state: directory

```

2. Ansible `command` module using the docker cli. I have had more success with this method.

```default
- name: remove temporary data-only container
  become: yes
  docker_container:
    name: your_temp_data_container
    state: absent

- name: recreate data volume
  become: yes
  docker_volume:
    name: your_data_volume
    state: present

- name: create temporary data-only container
  become: yes
  docker_container:
    name: your_temp_data_container
    image: tianon/true
    state: present
    volumes:
    - your_data_volume:/data

- name: copy folder contents to data volume via temporary data-only container
  become: yes
  command: docker cp /some_data_path_on_this_ansible_host/. your_temp_data_container:/data

- name: remove temporary data-only container
  become: yes
  docker_container:
    name: your_temp_data_container
    state: absent

```
I wish we could do it with a built-in module, but unless we have one, you can add a file into a named volume in a single task:

```
- name: actual config file is in the volume
  command:
    cmd: docker run --rm -iv your-config-volume:/v busybox sh -c 'cat > /v/config.yml'
    stdin: "{{ lookup('file', 'config.yml') | regex_replace('\\r\\n', '\\n') }}"

```

Here we use [command module](https://docs.ansible.com/ansible/latest/modules/command_module.html) to create a temporary container with the `your-config-volume` volume attached.

The `sh -c 'cat > /v/config.yml'` command saves all `stdin` into a file.

Finally, we set `stdin` to be the `config.yml` file from your role.

> 
> The `regex_replace('\\r\\n', '\\n')` part is required only if you run Windows.
> 
> 
>