From 7e2999ed2d7e2e86d19ed618e3697e995cf624fb Mon Sep 17 00:00:00 2001 From: Daniel Hokka Zakrisson Date: Tue, 12 Feb 2013 22:39:05 +0100 Subject: [PATCH] Add directory detection to _remote_md5 and use this in copy If it is a directory, change the destination path by appending the basename of the source file, like is done if the destination ends with a /, and try to get the MD5 of the new path. --- lib/ansible/runner/__init__.py | 2 +- lib/ansible/runner/action_plugins/copy.py | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py index 268371dea16..45d0ec6ae76 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -511,7 +511,7 @@ class Runner(object): def _remote_md5(self, conn, tmp, path): ''' takes a remote md5sum without requiring python, and returns 0 if no file ''' - test = "rc=0; [ -r \"%s\" ] || rc=2; [ -f \"%s\" ] || rc=1" % (path,path) + test = "rc=0; [ -r \"%s\" ] || rc=2; [ -f \"%s\" ] || rc=1; [ -d \"%s\" ] && rc=3" % (path, path, path) md5s = [ "(/usr/bin/md5sum %s 2>/dev/null)" % path, # Linux "(/sbin/md5sum -q %s 2>/dev/null)" % path, # ? diff --git a/lib/ansible/runner/action_plugins/copy.py b/lib/ansible/runner/action_plugins/copy.py index f3625724fc6..4eacd42e2de 100644 --- a/lib/ansible/runner/action_plugins/copy.py +++ b/lib/ansible/runner/action_plugins/copy.py @@ -34,10 +34,6 @@ class ActionModule(object): source = options.get('src', None) dest = options.get('dest', None) - if dest.endswith("/"): - base = os.path.basename(source) - dest = os.path.join(dest, base) - if (source is None and not 'first_available_file' in inject) or dest is None: result=dict(failed=True, msg="src and dest are required") return ReturnData(conn=conn, result=result) @@ -65,7 +61,15 @@ class ActionModule(object): result=dict(failed=True, msg="could not find src=%s" % source) return ReturnData(conn=conn, result=result) + if dest.endswith("/"): + base = os.path.basename(source) + dest = os.path.join(dest, base) + remote_md5 = self.runner._remote_md5(conn, tmp, dest) + if remote_md5 == '3': + # Destination is a directory + dest = os.path.join(dest, os.path.basename(source)) + remote_md5 = self.runner._remote_md5(conn, tmp, dest) exec_rc = None if local_md5 != remote_md5: