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


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


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?


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


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

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.