Ansible: copy a directory content to another directory

ansible copy all files in directory
ansible copy example
ansible files directory
ansible copy directory from remote to local
ansible create directory
ansible copy with_items
ansible copy directories locally
ansible copy: content example

I am trying to copy the content of dist directory to nginx directory.

- name: copy html file
  copy: src=/home/vagrant/dist/ dest=/usr/share/nginx/html/

But when I execute the playbook it throws an error:

TASK [NGINX : copy html file] **************************************************
fatal: [172.16.8.200]: FAILED! => {"changed": false, "failed": true, "msg": "attempted to take checksum of directory:/home/vagrant/dist/"}

How can I copy a directory that has another directory and a file inside?


You could use the synchronize module. The example from the documentation:

# Synchronize two directories on one remote host.
- synchronize:
    src: /first/absolute/path
    dest: /second/absolute/path
  delegate_to: "{{ inventory_hostname }}"

This has the added benefit that it will be more efficient for large/many files.

Ansible: copy a directory content to another directory, The copy module copies a file from the local or remote machine to a location If src and dest are files, the parent directory of dest is not created and the task If yes , the remote file will be replaced when contents are different than the source. And then it will copy and paste the contents of the source folder to that directory. If you want this behaviour, then don ‘ t give the ‘/’ after the path in the src parameter. The below Ansible copy directory example will first create a directory named copy_dir_ex in the /tmp of the remote server.


Resolved answer: To copy a directory's content to another directory I use the next:

- name: copy consul_ui files
  command: cp -r /home/{{ user }}/dist/{{ item }} /usr/share/nginx/html
  with_items:
   - "index.html"
   - "static/"

It copies both items to the other directory. In the example, one of the items is a directory and the other is not. It works perfectly.

copy – Copy files to remote locations, Local path to a file to copy to the remote server; can be absolute or relative. If path is a directory, it is copied recursively. In this case, if path ends with "/", only inside  Local path to a file to copy to the remote server; can be absolute or relative. If path is a directory, it is copied recursively. In this case, if path ends with "/", only inside contents of that directory are copied to destination. Otherwise, if it does not end with "/", the directory itself with all contents is copied.


Ansible Copy module by default copies files/dirs from control machine to remote machine. If you want to copy files/dirs in remote machine and if you have Ansible 2.0, set remote_src to yes

- name: copy html file
  copy: src=/home/vagrant/dist/ dest=/usr/share/nginx/html/ remote_src=yes directory_mode=yes

Ansible copying folder contents to another folder? : ansible, Which model can be used to copy files from remote machine to control machine? I’m sure you face day to day situation in which you have copy files or scripts to thousands of remote hosts. IT department folks don’t just login to each thousands of hosts and run script manually. And that’s why Ansible comes into picture. Ansible is a very powerful IT management solution out there for us at free of cost.


The simplest solution I've found to copy the contents of a folder without copying the folder itself is to use the following:

- name: Move directory contents
  command: cp -r /<source_path>/. /<dest_path>/

This resolves @surfer190's follow-up question:

Hmmm what if you want to copy the entire contents? I noticed that * doesn't work – surfer190 Jul 23 '16 at 7:29

* is a shell glob, in that it relies on your shell to enumerate all the files within the folder before running cp, while the . directly instructs cp to get the directory contents (see https://askubuntu.com/questions/86822/how-can-i-copy-the-contents-of-a-folder-to-another-folder-in-a-different-directo)

Ansible Copy Command – Linux Hint, perform a lot of complex tasks with it. Let's dive into some tasks you need to perform regularly. Ansible remote_src does not support recursive copying.See remote_src description in Ansible copy docs. To recursively copy the contents of a folder and to make sure the task stays idempotent I usually do it this way:


Ansible remote_src does not support recursive copying.See remote_src description in Ansible copy docs

To recursively copy the contents of a folder and to make sure the task stays idempotent I usually do it this way:

- name: get file names to copy
  command: "find /home/vagrant/dist -type f"
  register: files_to_copy

- name: copy files
  copy:
    src: "{{ item }}" 
    dest: "/usr/share/nginx/html"
    owner: nginx
    group: nginx
    remote_src: True
    mode: 0644
  with_items:
   - "{{ files_to_copy.stdout_lines }}"

Downside is that the find command still shows up as 'changed'

How to Create a Directory in Ansible, by default ansible forces overwrites, maybe disabling it would help your case (​force=no). Copy a directory from ansible machine to remote destination-Ansible Copy Examples let us assume that we have a directory called scripts. In this directory we have two files called file1 and file2 and Linux directory, in Linux directory we have two files called file 3 and file 4.


Copy directory contents with Ansible (without replace destination , There's yet another module Template provided by Ansible which lets you use variables which can be modified like the IP address and then  The copy module copies a file from the local or remote machine to a location on the remote machine. Use the fetch module to copy files from remote locations to the local box. If you need variable interpolation in copied files, use the template module. Using a variable in the content field will result in unpredictable output.


How to copy directory and sub dirs's and files from source to remote , How to copy files from localhost to remote host using Ansible. As a part of How to copy a directory content to another directory? Just change  Ansible copy allows you to copy files from one directory to another on the same remote machine. But you can perform this only for files, you can’t copy directories. You have to use the remote_src parameter to let Ansible know your intentions.


Ansible: How to copy File, Directory or Script from localhost to , Ansible copy module is one of the modules in file modules in Ansible. Ansible Copy a directory from ansible machine to remote destination-Ansible Copy Examples By using the above code you can copy the file with different permissions. Deletes any files/directories found in the destination that do not exist in the source. Toggles the /purge flag to RoboCopy. If flags is set, this will be ignored. Includes all subdirectories (Toggles the /e flag to RoboCopy). If flags is set, this will be ignored. Source file/directory to sync.