2013-09-14 05:20:00 +02:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2012-2013, Timothy Appnel <tim@appnel.com>
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
import subprocess
DOCUMENTATION = '''
---
module: synchronize
2013-09-19 06:07:05 +02:00
version_added: "1.4"
2013-09-14 05:20:00 +02:00
short_description: Uses rsync to make synchronizing file paths in your playbooks quick and easy.
description:
- This is a wrapper around rsync. Of course you could just use the command action to call rsync yourself, but you also have to add a fair number of boilerplate options and host facts. You still may need to call rsync directly via C(command) or C(shell) depending on your use case. The synchronize action is meant to do common things with C(rsync) easily. It does not provide access to the full power of rsync, but does make most invocations easier to follow.
options:
src:
description:
- Path on the source machine that will be synchronized to the destination; The path can be absolute or relative.
required: true
dest:
description:
- Path on the destination machine that will be synchronized from the source; The path can be absolute or relative.
required: true
2013-12-03 20:46:44 +01:00
dest_port:
description:
- Port number for ssh on the destination host. The ansible_ssh_port inventory var takes precedence over this value.
default: 22
version_added: "1.5"
2013-09-14 05:20:00 +02:00
mode:
description:
2013-12-18 22:06:35 +01:00
- Specify the direction of the synchroniztion. In push mode the localhost or delegate is the source; In pull mode the remote host in context is the source.
2013-09-14 05:20:00 +02:00
required: false
choices: [ 'push', 'pull' ]
default: 'push'
2013-09-19 09:09:17 +02:00
archive:
description:
- Mirrors the rsync archive flag, enables recursive, links, perms, times, owner, group flags and -D.
choices: [ 'yes', 'no' ]
default: 'yes'
required: false
2013-09-14 05:20:00 +02:00
delete:
description:
- Delete files that don't exist (after transfer, not before) in the C(src) path.
choices: [ 'yes', 'no' ]
default: 'no'
required: false
2013-09-19 09:09:17 +02:00
dirs:
description:
- Transfer directories without recursing
choices: [ 'yes', 'no' ]
default: 'no'
required: false
recursive:
description:
- Recurse into directories.
choices: [ 'yes', 'no' ]
default: the value of the archive option
required: false
links:
description:
- Copy symlinks as symlinks.
choices: [ 'yes', 'no' ]
default: the value of the archive option
required: false
2013-12-05 20:20:32 +01:00
copy_links:
description:
- Copy symlinks as the item that they point to (the referent) is copied, rather than the symlink.
choices: [ 'yes', 'no' ]
default: 'no'
required: false
2013-09-19 09:09:17 +02:00
perms:
description:
- Preserve permissions.
choices: [ 'yes', 'no' ]
default: the value of the archive option
required: false
times:
description:
- Preserve modification times
choices: [ 'yes', 'no' ]
default: the value of the archive option
required: false
owner:
description:
- Preserve owner (super user only)
choices: [ 'yes', 'no' ]
default: the value of the archive option
required: false
group:
description:
- Preserve group
choices: [ 'yes', 'no' ]
default: the value of the archive option
required: false
2013-09-14 05:20:00 +02:00
rsync_path:
description:
- Specify the rsync command to run on the remote machine. See C(--rsync-path) on the rsync man page.
required: false
2013-11-04 16:57:08 +01:00
rsync_timeout:
description:
- Specify a --timeout for the rsync command in seconds.
default: 10
required: false
2013-11-04 18:19:31 +01:00
notes:
- Inspect the verbose output to validate the destination user/host/path
are what was expected.
- The remote user for the dest path will always be the remote_user, not
the sudo_user.
- Expect that dest=~/x will be ~<remote_user>/x even if using sudo.
2013-09-14 05:20:00 +02:00
author: Timothy Appnel
'''
EXAMPLES = '''
2013-09-19 09:09:17 +02:00
# Synchronization of src on the control machine to dest on the remote hosts
2013-09-14 05:20:00 +02:00
synchronize: src=some/relative/path dest=/some/absolute/path
2013-09-19 09:09:17 +02:00
# Synchronization without any --archive options enabled
synchronize: src=some/relative/path dest=/some/absolute/path archive=no
# Synchronization with --archive options enabled except for --recursive
synchronize: src=some/relative/path dest=/some/absolute/path recursive=no
# Synchronization without --archive options enabled except use --links
synchronize: src=some/relative/path dest=/some/absolute/path archive=no links=yes
2013-09-14 05:20:00 +02:00
# Synchronization of two paths both on the control machine
local_action: synchronize src=some/relative/path dest=/some/absolute/path
# Synchronization of src on the inventory host to the dest on the localhost in
pull mode
synchronize: mode=pull src=some/relative/path dest=/some/absolute/path
# Synchronization of src on delegate host to dest on the current inventory host
synchronize: >
src=some/relative/path dest=/some/absolute/path
delegate_to: delegate.host
# 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 using an alternate rsync command
synchronize: src=some/relative/path dest=/some/absolute/path rsync_path="sudo rsync"
'''
def main():
module = AnsibleModule(
argument_spec = dict(
src = dict(required=True),
dest = dict(required=True),
2013-12-03 20:46:44 +01:00
dest_port = dict(default=22),
2013-09-14 05:20:00 +02:00
delete = dict(default='no', type='bool'),
private_key = dict(default=None),
rsync_path = dict(default=None),
2013-09-19 03:17:50 +02:00
archive = dict(default='yes', type='bool'),
2013-09-19 09:09:17 +02:00
dirs = dict(default='no', type='bool'),
2013-09-19 03:17:50 +02:00
recursive = dict(type='bool'),
links = dict(type='bool'),
2013-12-05 20:20:32 +01:00
copy_links = dict(type='bool'),
2013-09-19 03:17:50 +02:00
perms = dict(type='bool'),
times = dict(type='bool'),
owner = dict(type='bool'),
group = dict(type='bool'),
2013-11-04 16:52:44 +01:00
rsync_timeout = dict(type='int', default=10)
2013-09-14 05:20:00 +02:00
),
supports_check_mode = True
)
source = module.params['src']
dest = module.params['dest']
2013-12-03 20:46:44 +01:00
dest_port = module.params['dest_port']
2013-09-14 05:20:00 +02:00
delete = module.params['delete']
private_key = module.params['private_key']
rsync_path = module.params['rsync_path']
rsync = module.params.get('local_rsync_path', 'rsync')
2013-11-04 16:52:44 +01:00
rsync_timeout = module.params.get('rsync_timeout', 'rsync_timeout')
2013-09-19 03:17:50 +02:00
archive = module.params['archive']
dirs = module.params['dirs']
# the default of these params depends on the value of archive
recursive = module.params['recursive']
links = module.params['links']
2013-12-05 20:20:32 +01:00
copy_links = module.params['copy_links']
2013-09-19 03:17:50 +02:00
perms = module.params['perms']
times = module.params['times']
owner = module.params['owner']
group = module.params['group']
2013-11-04 16:52:44 +01:00
cmd = '%s --delay-updates --compress --timeout=%s' % (rsync, rsync_timeout)
2013-09-14 05:20:00 +02:00
if module.check_mode:
cmd = cmd + ' --dry-run'
if delete:
cmd = cmd + ' --delete-after'
2013-09-19 03:17:50 +02:00
if archive:
cmd = cmd + ' --archive'
if recursive is False:
cmd = cmd + ' --no-recursive'
if links is False:
cmd = cmd + ' --no-links'
2013-12-05 20:20:32 +01:00
if copy_links is True:
cmd = cmd + ' --copy-links'
2013-09-19 03:17:50 +02:00
if perms is False:
cmd = cmd + ' --no-perms'
if times is False:
cmd = cmd + ' --no-times'
if owner is False:
cmd = cmd + ' --no-owner'
if group is False:
cmd = cmd + ' --no-group'
else:
if recursive is True:
cmd = cmd + ' --recursive'
if links is True:
cmd = cmd + ' --links'
2013-12-05 20:20:32 +01:00
if copy_links is True:
cmd = cmd + ' --copy-links'
2013-09-19 03:17:50 +02:00
if perms is True:
cmd = cmd + ' --perms'
if times is True:
cmd = cmd + ' --times'
if owner is True:
cmd = cmd + ' --owner'
if group is True:
cmd = cmd + ' --group'
if dirs:
cmd = cmd + ' --dirs'
2013-09-14 05:20:00 +02:00
if private_key is None:
private_key = ''
else:
private_key = '-i '+ private_key
2013-12-03 20:46:44 +01:00
if dest_port != 22:
cmd += " --rsh '%s %s -o %s -o Port=%s'" % ('ssh', private_key,
'StrictHostKeyChecking=no', dest_port)
else:
cmd += " --rsh '%s %s -o %s'" % ('ssh', private_key,
2013-09-14 05:20:00 +02:00
'StrictHostKeyChecking=no') # need ssh param
2013-12-03 20:46:44 +01:00
2013-09-14 05:20:00 +02:00
if rsync_path:
2013-09-16 06:08:10 +02:00
cmd = cmd + " --rsync-path '%s'" %(rsync_path)
2013-09-19 08:25:33 +02:00
changed_marker = '<<CHANGED>>'
2013-09-16 06:08:10 +02:00
cmd = cmd + " --out-format='" + changed_marker + "%i %n%L'"
2013-09-14 05:20:00 +02:00
cmd = ' '.join([cmd, source, dest])
cmdstr = cmd
2013-09-16 06:08:10 +02:00
(rc, out, err) = module.run_command(cmd)
if rc:
return module.fail_json(msg=err, rc=rc, cmd=cmdstr)
2013-09-14 05:20:00 +02:00
else:
2013-09-16 06:08:10 +02:00
changed = changed_marker in out
return module.exit_json(changed=changed, msg=out.replace(changed_marker,''),
rc=rc, cmd=cmdstr)
2013-09-14 05:20:00 +02:00
2013-12-02 21:11:23 +01:00
# import module snippets
from ansible.module_utils.basic import *
2013-09-14 05:20:00 +02:00
main()