Commit 33242cac by James Tanner

Merge pull request #4375 from pfalcon/ansible

copy: Implement recursive copying if src is a directory.
parents 9a7765da 3ad61ef3
......@@ -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)
......
......@@ -145,6 +145,7 @@ def main():
argument_spec = dict(
state = dict(choices=['file','directory','link','hard','touch','absent'], default=None),
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),
......@@ -200,7 +201,11 @@ def main():
src = os.path.expanduser(src)
if src is not None and os.path.isdir(path) and state not in ["link", "absent"]:
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)
......
......@@ -393,6 +393,23 @@ class TestPlaybook(unittest.TestCase):
assert utils.jsonify(expected, format=True) == utils.jsonify(actual,format=True)
def test_recursive_copy(self):
pb = 'test/playbook-recursive-copy.yml'
actual = self._run(pb)
expected = {
"localhost": {
"changed": 65,
"failures": 0,
"ok": 73,
"skipped": 0,
"unreachable": 0
}
}
assert utils.jsonify(expected, format=True) == utils.jsonify(actual,format=True)
def _compare_file_output(self, filename, expected_lines):
actual_lines = []
with open(filename) as f:
......
---
# To run me manually, use: -i "localhost,"
- hosts: localhost
connection: local
gather_facts: no
vars:
- testdir: /tmp/ansible-rcopy
- filesdir: test_recursive_copy/files
tasks:
#
# First, regression tests for single-file behavior
#
- name: "src single file, dest file"
command: rm -rf {{testdir}}
- file: state=directory dest={{testdir}}
- copy: src={{filesdir}}/subdir/subdir2/subdir3/test1 dest={{testdir}}/file1
register: res
- command: test -f {{testdir}}/file1
- command: test "{{res.changed}}" == "True"
- copy: src={{filesdir}}/subdir/subdir2/subdir3/test1 dest={{testdir}}/file1
register: res
- command: test "{{res.changed}}" == "False"
- name: "src single file, dest dir w/trailing slash"
command: rm -rf {{testdir}}
- file: state=directory dest={{testdir}}
- copy: src={{filesdir}}/subdir/subdir2/subdir3/test1 dest={{testdir}}/
register: res
- command: test -f {{testdir}}/test1
- command: test "{{res.changed}}" == "True"
- copy: src={{filesdir}}/subdir/subdir2/subdir3/test1 dest={{testdir}}/
register: res
- command: test "{{res.changed}}" == "False"
- name: "src single file, dest dir wo/trailing slash - doesn't behave in sane way"
command: rm -rf {{testdir}}
- file: state=directory dest={{testdir}}
- copy: src={{filesdir}}/subdir/subdir2/subdir3/test1 dest={{testdir}}
register: res
- shell: test -f {{testdir}}/test1
- command: test "{{res.changed}}" == "True"
- copy: src={{filesdir}}/subdir/subdir2/subdir3/test1 dest={{testdir}}
register: res
- command: test "{{res.changed}}" == "False"
#
# Now, test recursive behavior
#
- name: "src dir w/trailing slash, dest w/trailing slash"
command: rm -rf {{testdir}}
- file: state=directory dest={{testdir}}
- copy: src={{filesdir}}/subdir/ dest={{testdir}}/
register: res
- command: test -d {{testdir}}/subdir2
- command: test -d {{testdir}}/subdir2/subdir3
- command: test -d {{testdir}}/subdir2/subdir3
- command: test -f {{testdir}}/subdir2/subdir3/test1
- command: test -f {{testdir}}/subdir2/subdir3/test2
- command: test "{{res.changed}}" == "True"
- copy: src={{filesdir}}/subdir/ dest={{testdir}}/
register: res
- command: test "{{res.changed}}" == "False"
# Expecting the same behavior
- name: "src dir w/trailing slash, dest wo/trailing slash"
command: rm -rf {{testdir}}
- file: state=directory dest={{testdir}}
- copy: src={{filesdir}}/subdir/ dest={{testdir}}
register: res
- command: test -d {{testdir}}/subdir2
- command: test -d {{testdir}}/subdir2/subdir3
- command: test -d {{testdir}}/subdir2/subdir3
- command: test -f {{testdir}}/subdir2/subdir3/test1
- command: test -f {{testdir}}/subdir2/subdir3/test2
- command: test "{{res.changed}}" == "True"
- copy: src={{filesdir}}/subdir/ dest={{testdir}}
register: res
- command: test "{{res.changed}}" == "False"
- name: "src dir wo/trailing slash, dest w/trailing slash"
command: rm -rf {{testdir}}
- file: state=directory dest={{testdir}}
- copy: src={{filesdir}}/subdir dest={{testdir}}/
register: res
- command: test -d {{testdir}}/subdir/subdir2
- command: test -d {{testdir}}/subdir/subdir2/subdir3
- command: test -d {{testdir}}/subdir/subdir2/subdir3
- command: test -f {{testdir}}/subdir/subdir2/subdir3/test1
- command: test -f {{testdir}}/subdir/subdir2/subdir3/test2
- command: test "{{res.changed}}" == "True"
- copy: src={{filesdir}}/subdir dest={{testdir}}/
register: res
- command: test "{{res.changed}}" == "False"
# Expecting the same behavior
- name: "src dir wo/trailing slash, dest wo/trailing slash"
command: rm -rf {{testdir}}
- file: state=directory dest={{testdir}}
- copy: src={{filesdir}}/subdir dest={{testdir}}
register: res
- command: test -d {{testdir}}/subdir/subdir2
- command: test -d {{testdir}}/subdir/subdir2/subdir3
- command: test -d {{testdir}}/subdir/subdir2/subdir3
- command: test -f {{testdir}}/subdir/subdir2/subdir3/test1
- command: test -f {{testdir}}/subdir/subdir2/subdir3/test2
- command: test "{{res.changed}}" == "True"
- copy: src={{filesdir}}/subdir dest={{testdir}}
register: res
- command: test "{{res.changed}}" == "False"
- name: "Verifying notify handling for recursive files"
command: rm -rf {{testdir}}
- file: state=directory dest={{testdir}}
- copy: src={{filesdir}}/subdir dest={{testdir}}
notify:
- files changed
- meta: flush_handlers
- command: test -f {{testdir}}/notify_fired
- command: rm {{testdir}}/notify_fired
- copy: src={{filesdir}}/subdir dest={{testdir}}
notify:
- files changed
- meta: flush_handlers
- command: test ! -f {{testdir}}/notify_fired
handlers:
- name: files changed
command: touch {{testdir}}/notify_fired
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment