ansibleansible-2.xansible-facts

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


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


Solution

  • 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 }}"