2

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?

5
  • 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 ? Jan 26 '20 at 11:31
  • 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
    Jan 26 '20 at 12:19
  • 1
    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
    Jan 26 '20 at 13:00
  • @simbo1905 I test ansible roles several dozen times a day against docker containers with molecule 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. Jan 26 '20 at 21:32
  • @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
    Jan 27 '20 at 23:17
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   


4
  • 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: pastebin.com/T1egQh3S and my packer script looks like this: pastebin.com/VFZAeyQE
    – Kyu96
    Jan 28 '20 at 0:11
  • If you have python installed, your are not using php:7.0-apache. Maybe an image based on it. Jan 28 '20 at 0:15
  • I am. As you can see a shell provisioner is executed before ansible is ran.
    – Kyu96
    Jan 28 '20 at 0:16
  • 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. Jan 28 '20 at 0:18
-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:

  1. If you are passing multiple paths by using variable then

    src: "/root/{{ item }}"

  2. If you are passing path by using a variable for different items then

    src: "/root/{{ item.source_path }}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.