<p>Following you comment: no I cannot reproduce your problem. Since you did not provide the rest of your files (inventory, etc...) and I cannot guess where is your problem, here is a quick proof by example using the <code>php:7.0-apache</code> image.</p>

<ul>
<li>The inventory (<code>inventories/docker_test/hosts.yml</code>)</li>
</ul>

<pre><code>---
all:
  hosts:
    docker_test:
      ansible_connection: docker
</code></pre>

<ul>
<li>Launching the test container</li>
</ul>

<pre><code>docker run -d --rm --name docker_test php:7.0-apache
</code></pre>

<ul>
<li>The test <code>playbook.yml</code></li>
</ul>

<pre><code>---
- hosts: docker_test
  gather_facts: false

  tasks:
    # Your image does not have python which is mandatory for ansible
    # Please note this is for demo only. You should get python installed
    # by extending the image through a Dockerfile and build it.
    - name: Dirty low-level command to get python installed
      raw: apt-get update &amp;&amp; apt-get install -y python3

    - name: Gather facts now we can
      setup:

    - name: Copy a dummy file
      copy:
        dest: /etc/apache2/sites-available/dummy.conf
        content: "# I'm a dummy file"
</code></pre>

<ul>
<li>Playbook run and output</li>
</ul>

<pre><code>$ ansible-playbook -i inventories/docker_test/ playbook.yml 

PLAY [docker_test] ********************************************************************************************************************************************************************************************************

TASK [Dirty low-level command to get python installed] ********************************************************************************************************************************************************************
changed: [docker_test]

TASK [Gather facts now we can] ********************************************************************************************************************************************************************************************
[WARNING]: Platform linux on host docker_test is using the discovered Python interpreter at /usr/bin/python3.5, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.

ok: [docker_test]

TASK [Copy a dummy file] **************************************************************************************************************************************************************************************************
changed: [docker_test]

PLAY RECAP ****************************************************************************************************************************************************************************************************************
docker_test                : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


</code></pre>

CC BY-SA 4.0
2020-01-28T00:11:03.763Z
This is really odd. I surely do have python installed in my container. And the error message is not related to the files that are being copied.. 
My provisioning file looks like this: https://pastebin.com/T1egQh3S and my packer script looks like this: https://pastebin.com/VFZAeyQE
Kyu96
CC BY-SA 4.0
2020-01-28T00:15:15.900Z
If you have python installed, your are not using `php:7.0-apache`. Maybe an image based on it.
Zeitounator
CC BY-SA 4.0
2020-01-28T00:16:32.397Z
I am. As you can see a shell provisioner is executed before ansible is ran.
Kyu96
CC BY-SA 4.0
2020-01-28T00:18:53.490Z
Rather than giving info away one by one in comment, I suggest your review totally your question and add all the necessary details. I just get your are doing this with packer and a provisionner script there which might have its importance. Once your have a clear question with all details, someone might be able to take over. Good night.
Zeitounator
CC BY-SA 4.0
2020-01-28T00:05:48.443Z
2020-01-28T00:05:48.443Z
Zeitounator
<p>Use the following source code for copy multiple files on your client machine.</p>

<pre><code>---
  - name: Copy data to the client machine
    hosts: hostname
    become_method: sudo
    become_user: root
    become: true
    tasks: 
      # Copy twice as sometimes files get skipped (mostly only one file skipped from a folder if the folder does not exist)
      - name: Copy UFO-Server 
        copy:
          src: "source files path"
          dest: "destination file path"
          owner: root
          group: root
          mode: 0644
          backup: yes
        ignore_errors: true
</code></pre>

<p>Note: </p>

<ol>
<li><p>If you are passing multiple paths by using variable then </p>

<p>src:  "/root/{{ item }}"</p></li>
<li><p>If you are passing path by using a variable for different items then</p>

<p>src:  "/root/{{ item.source_path }}" </p></li>
</ol>

CC BY-SA 4.0
2020-01-31T08:07:31.880Z
2020-01-31T08:07:31.880Z
Shaha Nawaj Mulla
<p>I am trying to copy multiple files to the target like so:</p>

<pre><code>  tasks:
  - name: "Copying files"
    copy:
      src: files/{{ item }}
      dest: /my/folder/
    with_items:
      - file.txt
</code></pre>

<p>The files directory is in the same directory as the Ansible playbook. However when running the script, I get this error:</p>

<pre><code>docker: TASK [Gathering Facts] *********************************************************
    docker: fatal: [test]: FAILED! =&gt; {"msg": "failed to transfer file to /home/user/.ansible/tmp/ansible-local-123331or28t2a/tmp6st_fxv1 ~user/.ansible/tmp/ansible-tmp-1579990845.0734024-189911264147319/AnsiballZ_setup.py:\n\n\n"}
    docker:
</code></pre>

<p>Why is that? Why can Ansible not copy the files to the target machine?</p>

CC BY-SA 4.0
2020-01-26T11:31:53.023Z
This is not even hitting your copy task. It fails on the first connection to your target when ansible is trying to gather facts. There is a connecting/privilege problem between your ansible controller and your target. Are you using the correct `remote_user` ?
Zeitounator
CC BY-SA 4.0
2020-01-26T12:19:00.027Z
This is a docker issue not an Ansible issue. In my experience it isn't normal to be mixing Docker and Ansible perhaps you are doing this only to learn about Ansible in a local machine? If so then using Vagrant and Test Kitchen is a more realistic local environment to learn how Ansible can configure VMs.
simbo1905
CC BY-SA 4.0
2020-01-26T13:00:30.677Z
It seemed that I had totally missed ansible-contrainer (since we dont use it with k8s). Reading the docs it looks very interesting. I am glad that your posts have brought it to my attention.
simbo1905
CC BY-SA 4.0
2020-01-26T21:32:33.080Z
@simbo1905 I test ansible roles several dozen times a day against docker containers with [molecule](https://molecule.readthedocs.io/en/stable/) with a normal ansible controller <-> target workflow. There no reason why it would not work as far as `ansible_connection=docker` is used and the inventory is correctly configured. Getting to know if using this is normal or not depends on what you actually want to do with it at the end. ansible-container is an other story, not part of OP's question as far as I can tell.
Zeitounator
CC BY-SA 4.0
2020-01-27T23:17:34.457Z
@Zeitounator I did try to set `remote_user: root`, which didn't change anything. The problem persists. Can you reproduce the problem? The docker container I am using is `php:7.0-apache`.
Kyu96
<docker><ansible><provisioning>
Ansible - Copy multiple files
Kyu96
# Ansible - Copy multiple files

(Asked by: username_0 on 2020-01-26)

I am trying to copy multiple files to the target like so:
  tasks:
  - name: "Copying files"
    copy:
      src: files/{{ item }}
      dest: /my/folder/
    with_items:
      - file.txt

The files directory is in the same directory as the Ansible playbook. However when running the script, I get this error:
docker: TASK [Gathering Facts] *********************************************************
    docker: fatal: [test]: FAILED! => {"msg": "failed to transfer file to /home/user/.ansible/tmp/ansible-local-123331or28t2a/tmp6st_fxv1 ~user/.ansible/tmp/ansible-tmp-1579990845.0734024-189911264147319/AnsiballZ_setup.py:\n\n\n"}
    docker:

Why is that? Why can Ansible not copy the files to the target machine?

**Tags:** docker, ansible, provisioning

> **Comments:**
> **username_1**: This is not even hitting your copy task. It fails on the first connection to your target when ansible is trying to gather facts. There is a connecting/privilege problem between your ansible controller and your target. Are you using the correct `remote_user` ?  
> **username_2**: This is a docker issue not an Ansible issue. In my experience it isn't normal to be mixing Docker and Ansible perhaps you are doing this only to learn about Ansible in a local machine? If so then using Vagrant and Test Kitchen is a more realistic local environment to learn how Ansible can configure VMs.  
> **username_2**: It seemed that I had totally missed ansible-contrainer (since we dont use it with k8s). Reading the docs it looks very interesting. I am glad that your posts have brought it to my attention.  
> **username_1**: @username_2 I test ansible roles several dozen times a day against docker containers with [molecule](https://molecule.readthedocs.io/en/stable/) with a normal ansible controller <-> target workflow. There no reason why it would not work as far as `ansible_connection=docker` is used and the inventory is correctly configured. Getting to know if using this is normal or not depends on what you actually want to do with it at the end. ansible-container is an other story, not part of OP's question as far as I can tell.  
> **username_0**: @username_1 I did try to set `remote_user: root`, which didn't change anything. The problem persists. Can you reproduce the problem? The docker container I am using is `php:7.0-apache`.  

---

## 2 answers

### Answer by username_1 on 2020-01-28. Score: 1

Following you comment: no I cannot reproduce your problem. Since you did not provide the rest of your files (inventory, etc...) and I cannot guess where is your problem, here is a quick proof by example using the php:7.0-apache image.

The inventory (inventories/docker_test/hosts.yml)

---
all:
  hosts:
    docker_test:
      ansible_connection: docker


Launching the test container

docker run -d --rm --name docker_test php:7.0-apache


The test playbook.yml

---
- hosts: docker_test
  gather_facts: false

  tasks:
    # Your image does not have python which is mandatory for ansible
    # Please note this is for demo only. You should get python installed
    # by extending the image through a Dockerfile and build it.
    - name: Dirty low-level command to get python installed
      raw: apt-get update && apt-get install -y python3

    - name: Gather facts now we can
      setup:

    - name: Copy a dummy file
      copy:
        dest: /etc/apache2/sites-available/dummy.conf
        content: "# I'm a dummy file"


Playbook run and output

$ ansible-playbook -i inventories/docker_test/ playbook.yml 

PLAY [docker_test] ********************************************************************************************************************************************************************************************************

TASK [Dirty low-level command to get python installed] ********************************************************************************************************************************************************************
changed: [docker_test]

TASK [Gather facts now we can] ********************************************************************************************************************************************************************************************
[WARNING]: Platform linux on host docker_test is using the discovered Python interpreter at /usr/bin/python3.5, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.

ok: [docker_test]

TASK [Copy a dummy file] **************************************************************************************************************************************************************************************************
changed: [docker_test]

PLAY RECAP ****************************************************************************************************************************************************************************************************************
docker_test                : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

> **Comments:**
> **username_0**: This is really odd. I surely do have python installed in my container. And the error message is not related to the files that are being copied..  My provisioning file looks like this: https://pastebin.com/T1egQh3S and my packer script looks like this: https://pastebin.com/VFZAeyQE  
> **username_1**: If you have python installed, your are not using `php:7.0-apache`. Maybe an image based on it.  
> **username_0**: I am. As you can see a shell provisioner is executed before ansible is ran.  
> **username_1**: Rather than giving info away one by one in comment, I suggest your review totally your question and add all the necessary details. I just get your are doing this with packer and a provisionner script there which might have its importance. Once your have a clear question with all details, someone might be able to take over. Good night.  

---

### Answer by username_3 on 2020-01-31. Score: -1

Use the following source code for copy multiple files on your client machine.
---
  - name: Copy data to the client machine
    hosts: hostname
    become_method: sudo
    become_user: root
    become: true
    tasks: 
      # Copy twice as sometimes files get skipped (mostly only one file skipped from a folder if the folder does not exist)
      - name: Copy UFO-Server 
        copy:
          src: "source files path"
          dest: "destination file path"
          owner: root
          group: root
          mode: 0644
          backup: yes
        ignore_errors: true

Note: 

If you are passing multiple paths by using variable then 
src:  "/root/{{ item }}"
If you are passing path by using a variable for different items then
src:  "/root/{{ item.source_path }}"


---

