16

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?

21

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

  • 1
    By the way, this goes only one level deep (leaves out subdirectories) and ignores directories in general so if that's not something you desire, just change the shell command acordingly. – Kęstutis May 11 '15 at 10:16
  • What will happen when I run on a bunch of servers? will each register it's own findings? and fetch the right ones? – Amir Mehler Apr 25 '16 at 13:10
  • any clue how to do this with win_find? I can't figure out how to extra path from the file list it returns – Peter Kahn Apr 21 '17 at 1:01
25

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.

  • 1
    I have found the synchronise module to be much more reliable and scalable than the other methods ansible has to copy files. – chicks Sep 30 '15 at 22:27
  • 3
    This is definitely a better way to do it than the accepted answer. – childofsoong Feb 2 '16 at 0:40
5

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.

2

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

well, if you are using latest ansible version, like 2.2.1.0, 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 }}"
0
- hosts: srv-test
  tasks:
    - find: paths="/var/tmp/collect" recurse=no patterns="*.tar"
      register: file_to_copy
    - fetch: src={{ item }} dest=/tmp
      with_items: files_to_copy.stdout_lines
  • This doesn't work at all. Has this been tried? – Basil A Jul 8 at 17:16
0

I use this: 1. Pull directories from remote host to specific hosts

- name: Gather hosts stats from other hosts
  shell: " scp -r {{results_root_dir_src}} root@{{groups['profiling_server'][0]}}:{{results_root_dir_dest}}/abc/"
  when: "'profiling_server' not in group_names"
#It will not run on the node where the directories need to be copied.
  1. Pull directories from node to localhost
- name: Gather from host to local
  delegate_to: 127.0.0.1
  run_once: true
  become: false
  shell: "scp -r root@{{groups['profiling_server'][0]}}:{{results_root_dir}} ./results_local_location "

inventory

[nodes]
server1
server2
server3
[profiling_server]
server1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.