mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
346 lines
12 KiB
Python
346 lines
12 KiB
Python
11 years ago
|
#!/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/>.
|
||
|
|
||
|
DOCUMENTATION = '''
|
||
|
---
|
||
|
module: synchronize
|
||
11 years ago
|
version_added: "1.4"
|
||
11 years ago
|
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
|
||
11 years ago
|
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"
|
||
11 years ago
|
mode:
|
||
|
description:
|
||
11 years ago
|
- 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.
|
||
11 years ago
|
required: false
|
||
|
choices: [ 'push', 'pull' ]
|
||
|
default: 'push'
|
||
11 years ago
|
archive:
|
||
|
description:
|
||
|
- Mirrors the rsync archive flag, enables recursive, links, perms, times, owner, group flags and -D.
|
||
|
choices: [ 'yes', 'no' ]
|
||
|
default: 'yes'
|
||
|
required: false
|
||
11 years ago
|
checksum:
|
||
|
description:
|
||
11 years ago
|
- Skip based on checksum, rather than mod-time & size; Note that that "archive" option is still enabled by default - the "checksum" option will not disable it.
|
||
11 years ago
|
choices: [ 'yes', 'no' ]
|
||
|
default: 'no'
|
||
|
required: false
|
||
11 years ago
|
version_added: "1.6"
|
||
10 years ago
|
compress:
|
||
|
description:
|
||
10 years ago
|
- Compress file data during the transfer. In most cases, leave this enabled unless it causes problems.
|
||
10 years ago
|
choices: [ 'yes', 'no' ]
|
||
|
default: 'yes'
|
||
|
required: false
|
||
|
version_added: "1.7"
|
||
11 years ago
|
existing_only:
|
||
|
description:
|
||
|
- Skip creating new files on receiver.
|
||
|
choices: [ 'yes', 'no' ]
|
||
|
default: 'no'
|
||
|
required: false
|
||
11 years ago
|
version_added: "1.5"
|
||
11 years ago
|
delete:
|
||
|
description:
|
||
11 years ago
|
- Delete files that don't exist (after transfer, not before) in the C(src) path. This option requires C(recursive=yes).
|
||
11 years ago
|
choices: [ 'yes', 'no' ]
|
||
|
default: 'no'
|
||
|
required: false
|
||
11 years ago
|
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
|
||
11 years ago
|
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
|
||
11 years ago
|
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
|
||
11 years ago
|
rsync_path:
|
||
|
description:
|
||
|
- Specify the rsync command to run on the remote machine. See C(--rsync-path) on the rsync man page.
|
||
|
required: false
|
||
11 years ago
|
rsync_timeout:
|
||
|
description:
|
||
|
- Specify a --timeout for the rsync command in seconds.
|
||
11 years ago
|
default: 0
|
||
11 years ago
|
required: false
|
||
11 years ago
|
set_remote_user:
|
||
|
description:
|
||
|
- put user@ for the remote paths. If you have a custom ssh config to define the remote user for a host
|
||
|
that does not match the inventory user, you should set this parameter to "no".
|
||
|
default: yes
|
||
11 years ago
|
rsync_opts:
|
||
|
description:
|
||
11 years ago
|
- Specify additional rsync options by passing in an array.
|
||
11 years ago
|
default:
|
||
11 years ago
|
required: false
|
||
11 years ago
|
version_added: "1.6"
|
||
11 years ago
|
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.
|
||
11 years ago
|
- To exclude files and directories from being synchronized, you may add
|
||
|
C(.rsync-filter) files to the source directory.
|
||
|
|
||
|
|
||
11 years ago
|
author: Timothy Appnel
|
||
|
'''
|
||
|
|
||
|
EXAMPLES = '''
|
||
11 years ago
|
# Synchronization of src on the control machine to dest on the remote hosts
|
||
11 years ago
|
synchronize: src=some/relative/path dest=/some/absolute/path
|
||
|
|
||
11 years ago
|
# 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
|
||
|
|
||
11 years ago
|
# Synchronization with --archive options enabled except for --times, with --checksum option enabled
|
||
|
synchronize: src=some/relative/path dest=/some/absolute/path checksum=yes times=no
|
||
|
|
||
11 years ago
|
# Synchronization without --archive options enabled except use --links
|
||
|
synchronize: src=some/relative/path dest=/some/absolute/path archive=no links=yes
|
||
|
|
||
11 years ago
|
# 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"
|
||
11 years ago
|
|
||
|
# Example .rsync-filter file in the source directory
|
||
|
- var # exclude any path whose last part is 'var'
|
||
|
- /var # exclude any path starting with 'var' starting at the source directory
|
||
|
+ /var/conf # include /var/conf even though it was previously excluded
|
||
11 years ago
|
|
||
|
# Synchronize passing in extra rsync options
|
||
|
synchronize: src=/tmp/helloworld dest=/var/www/helloword rsync_opts=--no-motd,--exclude=.git
|
||
11 years ago
|
'''
|
||
|
|
||
|
|
||
|
def main():
|
||
|
module = AnsibleModule(
|
||
|
argument_spec = dict(
|
||
|
src = dict(required=True),
|
||
|
dest = dict(required=True),
|
||
11 years ago
|
dest_port = dict(default=22),
|
||
11 years ago
|
delete = dict(default='no', type='bool'),
|
||
|
private_key = dict(default=None),
|
||
|
rsync_path = dict(default=None),
|
||
11 years ago
|
archive = dict(default='yes', type='bool'),
|
||
11 years ago
|
checksum = dict(default='no', type='bool'),
|
||
10 years ago
|
compress = dict(default='yes', type='bool'),
|
||
11 years ago
|
existing_only = dict(default='no', type='bool'),
|
||
11 years ago
|
dirs = dict(default='no', type='bool'),
|
||
11 years ago
|
recursive = dict(type='bool'),
|
||
|
links = dict(type='bool'),
|
||
11 years ago
|
copy_links = dict(type='bool'),
|
||
11 years ago
|
perms = dict(type='bool'),
|
||
|
times = dict(type='bool'),
|
||
|
owner = dict(type='bool'),
|
||
|
group = dict(type='bool'),
|
||
11 years ago
|
set_remote_user = dict(default='yes', type='bool'),
|
||
11 years ago
|
rsync_timeout = dict(type='int', default=0),
|
||
11 years ago
|
rsync_opts = dict(type='list')
|
||
11 years ago
|
),
|
||
|
supports_check_mode = True
|
||
|
)
|
||
|
|
||
11 years ago
|
source = '"' + module.params['src'] + '"'
|
||
|
dest = '"' + module.params['dest'] + '"'
|
||
11 years ago
|
dest_port = module.params['dest_port']
|
||
11 years ago
|
delete = module.params['delete']
|
||
|
private_key = module.params['private_key']
|
||
|
rsync_path = module.params['rsync_path']
|
||
|
rsync = module.params.get('local_rsync_path', 'rsync')
|
||
11 years ago
|
rsync_timeout = module.params.get('rsync_timeout', 'rsync_timeout')
|
||
11 years ago
|
archive = module.params['archive']
|
||
11 years ago
|
checksum = module.params['checksum']
|
||
10 years ago
|
compress = module.params['compress']
|
||
11 years ago
|
existing_only = module.params['existing_only']
|
||
11 years ago
|
dirs = module.params['dirs']
|
||
|
# the default of these params depends on the value of archive
|
||
|
recursive = module.params['recursive']
|
||
|
links = module.params['links']
|
||
11 years ago
|
copy_links = module.params['copy_links']
|
||
11 years ago
|
perms = module.params['perms']
|
||
|
times = module.params['times']
|
||
|
owner = module.params['owner']
|
||
|
group = module.params['group']
|
||
11 years ago
|
rsync_opts = module.params['rsync_opts']
|
||
11 years ago
|
|
||
10 years ago
|
cmd = '%s --delay-updates -FF' % rsync
|
||
|
if compress:
|
||
|
cmd = cmd + ' --compress'
|
||
11 years ago
|
if rsync_timeout:
|
||
|
cmd = cmd + ' --timeout=%s' % rsync_timeout
|
||
11 years ago
|
if module.check_mode:
|
||
|
cmd = cmd + ' --dry-run'
|
||
|
if delete:
|
||
|
cmd = cmd + ' --delete-after'
|
||
11 years ago
|
if existing_only:
|
||
|
cmd = cmd + ' --existing'
|
||
11 years ago
|
if checksum:
|
||
|
cmd = cmd + ' --checksum'
|
||
11 years ago
|
if archive:
|
||
|
cmd = cmd + ' --archive'
|
||
|
if recursive is False:
|
||
|
cmd = cmd + ' --no-recursive'
|
||
|
if links is False:
|
||
|
cmd = cmd + ' --no-links'
|
||
11 years ago
|
if copy_links is True:
|
||
|
cmd = cmd + ' --copy-links'
|
||
11 years ago
|
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'
|
||
11 years ago
|
if copy_links is True:
|
||
|
cmd = cmd + ' --copy-links'
|
||
11 years ago
|
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'
|
||
11 years ago
|
if private_key is None:
|
||
|
private_key = ''
|
||
|
else:
|
||
|
private_key = '-i '+ private_key
|
||
11 years ago
|
|
||
10 years ago
|
ssh_opts = '-S none -o StrictHostKeyChecking=no'
|
||
11 years ago
|
if dest_port != 22:
|
||
10 years ago
|
cmd += " --rsh 'ssh %s %s -o Port=%s'" % (private_key, ssh_opts, dest_port)
|
||
11 years ago
|
else:
|
||
10 years ago
|
cmd += " --rsh 'ssh %s %s'" % (private_key, ssh_opts) # need ssh param
|
||
11 years ago
|
|
||
11 years ago
|
if rsync_path:
|
||
11 years ago
|
cmd = cmd + " --rsync-path=%s" % (rsync_path)
|
||
10 years ago
|
|
||
11 years ago
|
if rsync_opts:
|
||
|
cmd = cmd + " " + " ".join(rsync_opts)
|
||
10 years ago
|
|
||
11 years ago
|
changed_marker = '<<CHANGED>>'
|
||
11 years ago
|
cmd = cmd + " --out-format='" + changed_marker + "%i %n%L'"
|
||
11 years ago
|
|
||
|
# expand the paths
|
||
|
if '@' not in source:
|
||
|
source = os.path.expanduser(source)
|
||
|
if '@' not in dest:
|
||
|
dest = os.path.expanduser(dest)
|
||
|
|
||
11 years ago
|
cmd = ' '.join([cmd, source, dest])
|
||
|
cmdstr = cmd
|
||
11 years ago
|
(rc, out, err) = module.run_command(cmd)
|
||
|
if rc:
|
||
|
return module.fail_json(msg=err, rc=rc, cmd=cmdstr)
|
||
11 years ago
|
else:
|
||
11 years ago
|
changed = changed_marker in out
|
||
11 years ago
|
out_clean=out.replace(changed_marker,'')
|
||
|
out_lines=out_clean.split('\n')
|
||
11 years ago
|
while '' in out_lines:
|
||
|
out_lines.remove('')
|
||
11 years ago
|
return module.exit_json(changed=changed, msg=out_clean,
|
||
|
rc=rc, cmd=cmdstr, stdout_lines=out_lines)
|
||
11 years ago
|
|
||
11 years ago
|
# import module snippets
|
||
|
from ansible.module_utils.basic import *
|
||
11 years ago
|
|
||
|
main()
|
||
|
|