Skip to content

Ansible: copy a directory recursive - take two

Another slow attempt at copying directories and files on a remote system from one place to another, using just Ansible operations.

 

This time I'm not using "with_filetree", but building the tree beforehand, using the "find" module:

- name: Find entries
  find:
    path: /path/to/source/directory
    recurse: yes
    file_type: any
  register: source_tree

Afterwards the operation is split into two parts. First create all directories:

- name: Create the directories
  file:
    path: "{{ item.path | regex_replace('/path/to/source/directory/','/path/to/destination/directory/') }}"
    state: directory
    mode: "{{ item.mode }}"
  loop: "{{ source_tree.files | flatten(levels=1) }}"
  loop_control:
    label: "{{ item.path | regex_replace('/path/to/source/directory', '') }}"
  when:
    - item.isdir

The performance of this task is not too bad, but not fast either. The second step is copying all the files:

- name: Copy the files
  copy:
    src: "{{ item.path }}"
    dest: "{{ item.path | regex_replace('/path/to/source/directory/','/path/to/destination/directory/') }}"
    remote_src: yes
    mode: "{{ item.mode }}"
  loop: "{{ source_tree.files | flatten(levels=1) }}"
  loop_control:
    label: "{{ item.path | regex_replace('/path/to/source/directory', '') }}"
  when:
    - item.isdir == False

This task is really slow, needs between 1-2 seconds per file. Just documenting this here, and probably moving on to call "rsync" directly.

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

No comments

Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.
Form options