Skip to content

Instantly share code, notes, and snippets.

@alces
Created December 22, 2016 11:35
Show Gist options
  • Save alces/211c725c6d2df0e4083b17076ff4dd8e to your computer and use it in GitHub Desktop.
Save alces/211c725c6d2df0e4083b17076ff4dd8e to your computer and use it in GitHub Desktop.
Ansible playbook copying content of a remote directory to another recursively
---
- name: Recursive copy
hosts: all
vars:
my_src: /path/to/src/dir
my_dst: /path/to/dst/dir
tasks:
- name: find dirs
find:
paths: "{{ my_src }}"
file_type: directory
recurse: yes
register: dirs_list
- name: make directories
file:
name: "{{ item.path.replace(my_src, my_dst) }}"
state: directory
with_items: "{{ dirs_list.files }}"
- name: find files
find:
paths: "{{ my_src }}"
recurse: yes
register: files_list
- name: copy files
copy:
src: "{{ item.path }}"
dest: "{{ item.path.replace(my_src, my_dst) }}"
remote_src: yes
with_items: "{{ files_list.files }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment