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

Solution 1:

You should use the synchronise module to do this. This uses the awesome power of rsync. It will copy file & directory structures of any depth, is bulletproof and highly efficient - only copying the actual bytes that have changed:

- name: Fetch stuff from the remote and save to local
  synchronize:  src={{ item }} dest=/tmp/ mode=pull
  with_items:
    - "folder/one"
    - "folder/two"

The key is the mode parameter:

Specify the direction of the synchronization. In push mode the localhost or delegate is the source; In pull mode the remote host in context is the source.

Solution 2:

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


Solution 3:

i dont have enough rep to comment otherwise i would add it.

I used what Kęstutis posted. i had to make a slight modification

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

The with_items was the area i had to change. it was not able to locate the files otherwise.


Solution 4:

Fixing the example above

- hosts: srv-test
  tasks:
    - find: paths="/var/tmp/collect" recurse=no patterns="*.tar"
      register: files_to_copy
    - fetch: src={{ item.path }} dest=/tmp
      with_items: "{{ files_to_copy.files }}"

Solution 5:

well, if you are using latest ansible version, like 2.9.9, I think we need quotes to the item

- name: use find to get the files list which you want to copy/fetch
  find: 
    paths: /etc/
    patterns: ".*passwd$"
    use_regex: True   
  register: file_2_fetch

- name: use fetch to get the files
  fetch:
    src: "{{ item.path }}"
    dest: /tmp/
    flat: yes
  with_items: "{{ file_2_fetch.files }}"