mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
174 lines
6.3 KiB
Python
174 lines
6.3 KiB
Python
#!/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
|
|
version_added: "1.4"
|
|
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
|
|
mode:
|
|
description:
|
|
- Specify the direction of the synchroniztion. In push mode the localhost or delgate is the source; In pull mode the remote host in context is the source.
|
|
required: false
|
|
choices: [ 'push', 'pull' ]
|
|
default: 'push'
|
|
delete:
|
|
description:
|
|
- Delete files that don't exist (after transfer, not before) in the C(src) path.
|
|
choices: [ 'yes', 'no' ]
|
|
default: 'no'
|
|
required: false
|
|
rsync_path:
|
|
description:
|
|
- Specify the rsync command to run on the remote machine. See C(--rsync-path) on the rsync man page.
|
|
required: false
|
|
author: Timothy Appnel
|
|
'''
|
|
|
|
EXAMPLES = '''
|
|
# Synchronization of src on the control machien to dest on the remote hosts
|
|
synchronize: src=some/relative/path dest=/some/absolute/path
|
|
|
|
# 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),
|
|
delete = dict(default='no', type='bool'),
|
|
private_key = dict(default=None),
|
|
rsync_path = dict(default=None),
|
|
archive = dict(default='yes', type='bool'),
|
|
dirs = dict(type='bool'),
|
|
recursive = dict(type='bool'),
|
|
links = dict(type='bool'),
|
|
perms = dict(type='bool'),
|
|
times = dict(type='bool'),
|
|
owner = dict(type='bool'),
|
|
group = dict(type='bool'),
|
|
),
|
|
supports_check_mode = True
|
|
)
|
|
|
|
source = module.params['src']
|
|
dest = module.params['dest']
|
|
delete = module.params['delete']
|
|
private_key = module.params['private_key']
|
|
rsync_path = module.params['rsync_path']
|
|
rsync = module.params.get('local_rsync_path', 'rsync')
|
|
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']
|
|
perms = module.params['perms']
|
|
times = module.params['times']
|
|
owner = module.params['owner']
|
|
group = module.params['group']
|
|
|
|
cmd = '%s --delay-updates --compress' % rsync
|
|
if module.check_mode:
|
|
cmd = cmd + ' --dry-run'
|
|
if delete:
|
|
cmd = cmd + ' --delete-after'
|
|
if archive:
|
|
cmd = cmd + ' --archive'
|
|
if recursive is False:
|
|
cmd = cmd + ' --no-recursive'
|
|
if links is False:
|
|
cmd = cmd + ' --no-links'
|
|
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'
|
|
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'
|
|
if private_key is None:
|
|
private_key = ''
|
|
else:
|
|
private_key = '-i '+ private_key
|
|
cmd = cmd + " --rsh '%s %s -o %s'" % ('ssh', private_key,
|
|
'StrictHostKeyChecking=no') # need ssh param
|
|
if 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
|
|
(rc, out, err) = module.run_command(cmd)
|
|
if rc:
|
|
return module.fail_json(msg=err, rc=rc, cmd=cmdstr)
|
|
else:
|
|
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>>
|
|
|
|
main()
|
|
|