Search code examples
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

  • 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.