From e2b38ff41a8ce397a3c281017e91e5c74e996ee7 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 4 Oct 2013 21:58:49 +0300 Subject: [PATCH 1/2] 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. --- files/copy | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/files/copy b/files/copy index 8f2843b314d..99c3391083b 100644 --- a/files/copy +++ b/files/copy @@ -31,6 +31,10 @@ options: src: description: - 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 default: null aliases: [] @@ -42,7 +46,8 @@ options: default: null dest: 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 default: null backup: @@ -76,8 +81,8 @@ options: required: false author: Michael DeHaan notes: - - The "copy" module can't be used to recursively copy directory structures to the target machine. Please see the - "Delegation" section of the Advanced Playbooks documentation for a better approach to recursive copies. + - The "copy" module recursively copy facility does not scale to lots (>hundreds) of files. + For alternative, see "Delegation" section of the Advanced Playbooks documentation. ''' EXAMPLES = ''' @@ -122,6 +127,13 @@ def main(): md5sum_src = module.md5(src) 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 not force: module.exit_json(msg="file already exists", src=src, dest=dest, changed=False) From 2b081b517e9ee777c845018cd8be526434c5113d Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 14 Oct 2013 22:10:27 +0300 Subject: [PATCH 2/2] copy: Handle "no copy/propagate attrs only" for recursive mode well. For this, add internal "original_basename" param to file module, similar to copy module. (Param name is a bit misnormer now, should be treated as "original basepath"). --- files/file | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/files/file b/files/file index adbc21061af..4ea527cd501 100644 --- a/files/file +++ b/files/file @@ -144,6 +144,7 @@ def main(): argument_spec = dict( state = dict(choices=['file','directory','link','hard','touch','absent'], default='file'), path = dict(aliases=['dest', 'name'], required=True), + original_basename = dict(required=False), # Internal use only, for recursive ops recurse = dict(default='no', type='bool'), force = dict(required=False,default=False,type='bool'), diff_peek = dict(default=None), @@ -179,7 +180,11 @@ def main(): src = os.path.expanduser(src) if src is not None and os.path.isdir(path) and state != "link": - params['path'] = path = os.path.join(path, os.path.basename(src)) + if params['original_basename']: + basename = params['original_basename'] + else: + basename = os.path.basename(src) + params['path'] = path = os.path.join(path, basename) file_args = module.load_file_common_arguments(params)