copy: Implement recursive copying if src is a directory.

If src param to copy is a directory, all files under it are collected
and pushed one by one to target. Source dir path handled in a way
simalar to rsync: if it ends with slash, only inside contents of
directory are copied to destination, otherwise the dir itself is
copied (with all contents of course). Original idea and implementation
by https://github.com/ansible/ansible/pull/1809 . Rewritten to address
review comments and simplify/correct logic.
reviewable/pr18780/r1
Paul Sokolovsky 11 years ago
parent 57cec2a88b
commit e2b38ff41a

@ -31,6 +31,10 @@ options:
src: src:
description: description:
- Local path to a file to copy to the remote server; can be absolute or relative. - Local path to a file to copy to the remote server; can be absolute or relative.
If path is a directory, it is copied recursively. In this case, if path ends
with "/", only inside contents of that directory are copied to destination.
Otherwise, if it does not end with "/", the directory itself with all contents
is copied. This behavior is similar to Rsync.
required: false required: false
default: null default: null
aliases: [] aliases: []
@ -42,7 +46,8 @@ options:
default: null default: null
dest: dest:
description: description:
- Remote absolute path where the file should be copied to. - Remote absolute path where the file should be copied to. If src is a directory,
this must be a directory too.
required: true required: true
default: null default: null
backup: backup:
@ -76,8 +81,8 @@ options:
required: false required: false
author: Michael DeHaan author: Michael DeHaan
notes: notes:
- The "copy" module can't be used to recursively copy directory structures to the target machine. Please see the - The "copy" module recursively copy facility does not scale to lots (>hundreds) of files.
"Delegation" section of the Advanced Playbooks documentation for a better approach to recursive copies. For alternative, see "Delegation" section of the Advanced Playbooks documentation.
''' '''
EXAMPLES = ''' EXAMPLES = '''
@ -122,6 +127,13 @@ def main():
md5sum_src = module.md5(src) md5sum_src = module.md5(src)
md5sum_dest = None md5sum_dest = None
# Special handling for recursive copy - create intermediate dirs
if original_basename and dest.endswith("/"):
dest = os.path.join(dest, original_basename)
dirname = os.path.dirname(dest)
if not os.path.exists(dirname):
os.makedirs(dirname)
if os.path.exists(dest): if os.path.exists(dest):
if not force: if not force:
module.exit_json(msg="file already exists", src=src, dest=dest, changed=False) module.exit_json(msg="file already exists", src=src, dest=dest, changed=False)

Loading…
Cancel
Save