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.
This commit is contained in:
parent
35068527db
commit
10f336a97c
1 changed files with 9 additions and 24 deletions
|
@ -40,11 +40,6 @@ options:
|
||||||
required: false
|
required: false
|
||||||
choices: [ 'push', 'pull' ]
|
choices: [ 'push', 'pull' ]
|
||||||
default: 'push'
|
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:
|
delete:
|
||||||
description:
|
description:
|
||||||
- Delete files that don't exist (after transfer, not before) in the C(src) path.
|
- 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 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: 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 using an alternate rsync command
|
||||||
synchronize: src=some/relative/path dest=/some/absolute/path rsync_path="sudo rsync"
|
synchronize: src=some/relative/path dest=/some/absolute/path rsync_path="sudo rsync"
|
||||||
'''
|
'''
|
||||||
|
@ -90,7 +82,6 @@ def main():
|
||||||
argument_spec = dict(
|
argument_spec = dict(
|
||||||
src = dict(required=True),
|
src = dict(required=True),
|
||||||
dest = dict(required=True),
|
dest = dict(required=True),
|
||||||
verbosity = dict(default=0),
|
|
||||||
delete = dict(default='no', type='bool'),
|
delete = dict(default='no', type='bool'),
|
||||||
private_key = dict(default=None),
|
private_key = dict(default=None),
|
||||||
rsync_path = dict(default=None),
|
rsync_path = dict(default=None),
|
||||||
|
@ -100,7 +91,6 @@ def main():
|
||||||
|
|
||||||
source = module.params['src']
|
source = module.params['src']
|
||||||
dest = module.params['dest']
|
dest = module.params['dest']
|
||||||
verbosity = module.params['verbosity']
|
|
||||||
delete = module.params['delete']
|
delete = module.params['delete']
|
||||||
private_key = module.params['private_key']
|
private_key = module.params['private_key']
|
||||||
rsync_path = module.params['rsync_path']
|
rsync_path = module.params['rsync_path']
|
||||||
|
@ -109,10 +99,6 @@ def main():
|
||||||
cmd = '%s --archive --delay-updates --compress' % rsync
|
cmd = '%s --archive --delay-updates --compress' % rsync
|
||||||
if module.check_mode:
|
if module.check_mode:
|
||||||
cmd = cmd + ' --dry-run'
|
cmd = cmd + ' --dry-run'
|
||||||
if verbosity:
|
|
||||||
cmd = '%s -%s' % (cmd, 'v' * int(verbosity))
|
|
||||||
else:
|
|
||||||
cmd = cmd + ' --quiet'
|
|
||||||
if delete:
|
if delete:
|
||||||
cmd = cmd + ' --delete-after'
|
cmd = cmd + ' --delete-after'
|
||||||
if private_key is None:
|
if private_key is None:
|
||||||
|
@ -122,19 +108,18 @@ def main():
|
||||||
cmd = cmd + " --rsh '%s %s -o %s'" % ('ssh', private_key,
|
cmd = cmd + " --rsh '%s %s -o %s'" % ('ssh', private_key,
|
||||||
'StrictHostKeyChecking=no') # need ssh param
|
'StrictHostKeyChecking=no') # need ssh param
|
||||||
if rsync_path:
|
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])
|
cmd = ' '.join([cmd, source, dest])
|
||||||
cmdstr = cmd
|
cmdstr = cmd
|
||||||
cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
|
(rc, out, err) = module.run_command(cmd)
|
||||||
stderr=subprocess.PIPE)
|
if rc:
|
||||||
(out, err) = cmd.communicate()
|
return module.fail_json(msg=err, rc=rc, cmd=cmdstr)
|
||||||
if cmd.returncode:
|
|
||||||
return module.fail_json(msg=err, rc=cmd.returncode, cmd=cmdstr)
|
|
||||||
else:
|
else:
|
||||||
return module.exit_json(changed=True, msg=out,
|
changed = changed_marker in out
|
||||||
rc=cmd.returncode, cmd=cmdstr, check=module.check_mode)
|
return module.exit_json(changed=changed, msg=out.replace(changed_marker,''),
|
||||||
|
rc=rc, cmd=cmdstr)
|
||||||
|
|
||||||
# include magic from lib/ansible/module_common.py
|
# include magic from lib/ansible/module_common.py
|
||||||
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
||||||
|
|
Loading…
Reference in a new issue