Linux – How to fetch multiple files from remote machine to local with Ansible

ansiblecopylinux

I would like to copy files from remote directory to local directory with Ansible but fetch module allows me to copy only one file. I have many servers from which I need files (same directory each server) and I don't now how to do this with Ansible.

Any ideas?

Best Answer

You will probably need to register remote content and than loop over it, something like this should work:

- shell: (cd /remote; find . -maxdepth 1 -type f) | cut -d'/' -f2
  register: files_to_copy

- fetch: src=/remote/{{ item }} dest=/local/
  with_items: "{{ files_to_copy.stdout_lines }}"

where /remote should be changed with directory path on your remote server and /local/ with directory on your master