7

i ve an ansible role which looks like this :

my-role
├─── files
│       my-file-one
│       my-file-two
│       my-file-...
│       my-file-n
└─── tasks
        main.yml

in my main.yml , i ve this recursive copy task , and i want to copy all files without the need of listing them manually :

- name: copy all files
  copy:
    src: "{{ item }}"
    dest: /dest/
  with_items:
    - ????

Suggestions ??

firasKoubaa
  • 6,439
  • 25
  • 79
  • 148

3 Answers3

7

If your files directory is flat (i.e., you don't need to worry about recursing directories), you can just use with_fileglob to get the list of files:

---
- name: copy all files
  copy:
    src: "{{ item }}"
    dest: /dest/
  with_fileglob: "files/*"

If you need a recursive copy, you can't use with_fileglob because it only returns a list of files. You can use the find module instead like this:

---
- name: list files
  find:
    paths: "{{ role_path }}/files/"
    file_type: any
  register: files

- name: copy files
  copy:
    src: "{{ item.path }}"
    dest: /dest/
  loop: "{{ files.files }}"
larsks
  • 277,717
  • 41
  • 399
  • 399
4

From the copy module docs.

Local path to a file to copy to the remote server. This 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.

If you place the files into a subdirectory of the files directory (e.g. my_files) then you can use my_files/ as the src argument to the copy module.

my-role
├─── files
|  └───my_files
│         my-file-one
│         my-file-two
│         my-file-...
│         my-file-n
└─── tasks
        main.yml
- name: copy all files
  copy:
    src: my_files/
    dest: /dest/
Calum Halpin
  • 1,945
  • 1
  • 10
  • 20
4

Using ./ as the src argument worked for me. It copies recursively all files and directories from the role files directory to target. This solution does not require to list the files with another task before copying them.

---
- name: Copy all role files to target
  copy:
    src: ./
    dest: <destination_dir>
Plankton
  • 41
  • 2