1

I want files in the src directory to copy to the dest directory but I don't want the files with the same name to copy over. Does anyone know how I can do that?

- name: Copy files but not the ones that have the same name
  copy:
    src:  <src_dir>
    dest: <dest_dir>
Zeitounator
  • 38,476
  • 7
  • 53
  • 66

2 Answers2

3

Use the backup option of the copy module.

- copy:
    src: foo
    dest: /tmp/bar
    backup: true

This will create multiple files, of which, the one with the name specified in dest parameters will be the one representing the last run of Ansible.

For example:

$ ls -l /tmp/
total 12
-rw-r--r--   1 root   root   150 Mar 20 16:30 bar
-rw-r--r--   1 root   root   127 Mar 20 16:20 bar.249.2023-03-20@16:20:44~
-rw-r--r--   1 root   root   148 Mar 20 16:20 bar.329.2023-03-20@16:30:17~
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
3

Q: "Don't copy the files with the same name."

A: Set the parameter force to false. Quoting

If false, the file will only be transferred if the destination does not exist.

    - copy:
        src: /tmp/test/src/
        dest: /tmp/test/dest/
        force: false

Example.

Given the files

shell> tree /tmp/test/
/tmp/test/
├── dest
│   └── a
└── src
    ├── a
    ├── b
    └── c

2 directories, 4 files
shell> cat /tmp/test/src/*
src
src
src
shell> cat /tmp/test/dest/*
dest

The playbook

- hosts: localhost
  tasks:
    - copy:
        src: /tmp/test/src/
        dest: /tmp/test/dest/
        force: false

will copy the files b and c only because the file a exists

shell> tree /tmp/test/
/tmp/test/
├── dest
│   ├── a
│   ├── b
│   └── c
└── src
    ├── a
    ├── b
    └── c

2 directories, 6 files
shell> cat /tmp/test/dest/*
dest
src
src
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63