Ansible : How to copy everything from "Files" folders of a specific role
Asked Answered
C

4

9

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 ??

Coleridge answered 3/1, 2020 at 14:1 Comment(0)
S
8

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 }}"
Siloum answered 3/1, 2020 at 14:9 Comment(2)
This is an old answer. According to docs with_fileglob is a local lookup while find is a remote lookup so this might not do what you want.Oreilly
find can work locally if you add delegate_to: 127.0.0.1Greenland
G
6

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>
Gilmour answered 31/3, 2020 at 20:53 Comment(0)
S
5

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/
Shari answered 3/1, 2020 at 14:21 Comment(0)
U
0

This seems like the better solution to me:


- name: "Add directories"
  file:
    state: directory
    path: "/{{ item.path }}"
  loop: >-
    {{ lookup('filetree', '.')
     | selectattr('state', 'equalto', 'directory')
    }}

- name: "Add files"
  copy:
    src: "{{ item.path }}"
    dest: "/{{ item.path }}"
  loop: >-
    {{ lookup('filetree', '.')
     | selectattr('state', 'equalto', 'file')
     | selectattr('path', 'regex', '[^~#]$')
    }}

The selectattr('path', 'regex', '[^~#]$') at the end there is to filter out Emacs backup files.

Upswing answered 23/6, 2024 at 13:6 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.