Copy directory contents with Ansible (without replace destination files)

There is a directory under /var/test1 with content:

.
..
.git
.gitignore
file1
file2

and I want to copy it on an other location /var/test2 with pre-existing content:

.
..
file1

If I use the Ansible copy:

- copy:
  # note the trailing `/` at `src: /var/test1/` in order to copy the contents
  src: /var/test1/
  dest: /var/test2

it will replace the file1 in the /var/test2

How can I copy the directory contents without replacing the files at the destination?


Solution 1:

by default ansible forces overwrites, maybe disabling it would help your case (force=no).

Solution 2:

You probably want to use synchronize_module. It has delete option:

Delete files in dest that don't exist (after transfer, not before) in the src path. This option requires recursive=yes.

- synchronize:
   src: /var/test1
   dest: /var/test2
   recursive: True
   delete: False