copy action plug-in check mode respects force=no

The copy action accepts force=no, which tells it not to replace an
existing file even if it differs from the source.  The copy action
plug-in wasn't respecting this option when operated in check mode, so it
would report that changes are necessary in check mode even though copy
would make no changes when run normally.

Runner._remote_md5 was changed to make the logic for setting rc perhaps
a little more clear, and to make sure that rc=0 when the file does not
exist.
This commit is contained in:
Dale Sedivec 2013-04-06 21:20:10 -05:00 committed by Michael DeHaan
commit 515fd9e915
3 changed files with 18 additions and 2 deletions

View file

@ -584,7 +584,11 @@ class Runner(object):
''' takes a remote md5sum without requiring python, and returns 0 if no file '''
path = pipes.quote(path)
test = "rc=0; [ -r \"%s\" ] || rc=2; [ -f \"%s\" ] || rc=1; [ -d \"%s\" ] && rc=3" % (path, path, path)
test = ('if [ ! -e \"%s\" ]; then rc=0;'
'elif [ -d \"%s\" ]; then rc=3;'
'elif [ ! -f \"%s\" ]; then rc=1;'
'elif [ ! -r \"%s\" ]; then rc=2;'
'else rc=0; fi') % ((path,) * 4)
md5s = [
"(/usr/bin/md5sum %s 2>/dev/null)" % path, # Linux
"(/sbin/md5sum -q %s 2>/dev/null)" % path, # ?

View file

@ -42,6 +42,7 @@ class ActionModule(object):
source = options.get('src', None)
content = options.get('content', None)
dest = options.get('dest', None)
force = utils.boolean(options.get('force', 'yes'))
if (source is None and content is None and not 'first_available_file' in inject) or dest is None:
result=dict(failed=True, msg="src (or content) and dest are required")
@ -105,6 +106,10 @@ class ActionModule(object):
dest = os.path.join(dest, os.path.basename(source))
remote_md5 = self.runner._remote_md5(conn, tmp, dest)
# remote_md5 == '0' would mean that the file does not exist.
if remote_md5 != '0' and not force:
return ReturnData(conn=conn, result=dict(changed=False))
exec_rc = None
if local_md5 != remote_md5: