Added ingenious change detection trick from @smoothify though at the cost of verbosity controls. (Quiet or verbosity > 1 breaks change detection. Also added better use of module_common methods.

reviewable/pr18780/r1
Timothy Appnel 11 years ago
parent 35068527db
commit 10f336a97c

@ -40,11 +40,6 @@ options:
required: false
choices: [ 'push', 'pull' ]
default: 'push'
verbosity:
description:
- An integer controlling the amount of information returned during processing. See the C(-v, --verbose) option of the rsync man page for details. If verbosity is not defined or a value of 0 is assumed, the C(--quiet) option is passed and information is supressed.
required: false
default: 0
delete:
description:
- Delete files that don't exist (after transfer, not before) in the C(src) path.
@ -77,9 +72,6 @@ synchronize: >
# Synchronize and delete files in dest on the remote host that are not found in src of localhost.
synchronize: src=some/relative/path dest=/some/absolute/path delete=yes
# Synchronize and return verbose information from the rsync transfer.
synchronize: src=some/relative/path dest=/some/absolute/path verbosity=1
# Synchronize using an alternate rsync command
synchronize: src=some/relative/path dest=/some/absolute/path rsync_path="sudo rsync"
'''
@ -90,7 +82,6 @@ def main():
argument_spec = dict(
src = dict(required=True),
dest = dict(required=True),
verbosity = dict(default=0),
delete = dict(default='no', type='bool'),
private_key = dict(default=None),
rsync_path = dict(default=None),
@ -100,7 +91,6 @@ def main():
source = module.params['src']
dest = module.params['dest']
verbosity = module.params['verbosity']
delete = module.params['delete']
private_key = module.params['private_key']
rsync_path = module.params['rsync_path']
@ -109,10 +99,6 @@ def main():
cmd = '%s --archive --delay-updates --compress' % rsync
if module.check_mode:
cmd = cmd + ' --dry-run'
if verbosity:
cmd = '%s -%s' % (cmd, 'v' * int(verbosity))
else:
cmd = cmd + ' --quiet'
if delete:
cmd = cmd + ' --delete-after'
if private_key is None:
@ -122,19 +108,18 @@ def main():
cmd = cmd + " --rsh '%s %s -o %s'" % ('ssh', private_key,
'StrictHostKeyChecking=no') # need ssh param
if rsync_path:
cmd = cmd + ' --rsync-path ' + rsync_path
cmd = cmd + " --rsync-path '%s'" %(rsync_path)
changed_marker = '<<changed>>'
cmd = cmd + " --out-format='" + changed_marker + "%i %n%L'"
cmd = ' '.join([cmd, source, dest])
cmdstr = cmd
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(out, err) = cmd.communicate()
if cmd.returncode:
return module.fail_json(msg=err, rc=cmd.returncode, cmd=cmdstr)
(rc, out, err) = module.run_command(cmd)
if rc:
return module.fail_json(msg=err, rc=rc, cmd=cmdstr)
else:
return module.exit_json(changed=True, msg=out,
rc=cmd.returncode, cmd=cmdstr, check=module.check_mode)
changed = changed_marker in out
return module.exit_json(changed=changed, msg=out.replace(changed_marker,''),
rc=rc, cmd=cmdstr)
# include magic from lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>

Loading…
Cancel
Save