synchronize¶
Collection Note
This module is part of the ansible.posix collection. To install the collection, use:
Added in version1.0.0.
Synopsis¶
- M(ansible.posix.synchronize) is a wrapper around C(rsync) to make common tasks in your playbooks quick and easy.
- It is run and originates on the local host where Ansible is being run.
- Of course, you could just use the M(ansible.builtin.command) action to call C(rsync) yourself, but you also have to add a fair number of boilerplate options and host facts.
- This module is not intended to provide access to the full power of C(rsync), but does make the most common invocations easier to implement. You I(still) may need to call C(rsync) directly via M(ansible.builtin.command) or M(ansible.builtin.shell) depending on your use case.
Parameters¶
| Parameter | Defaults / Choices | Comments |
|---|---|---|
| _local_rsync_password str |
Internal use only, never logged. | |
| _local_rsync_path path |
Default: rsync |
Internal use only. |
| _ssh_args str |
Internal use only. See O(use_ssh_args) for ssh arg settings. | |
| _substitute_controller bool |
Internal use only. | |
| archive bool |
Default: True |
Mirrors the rsync archive flag, enables recursive, links, perms, times, owner, group flags, and C(-D). |
| checksum bool |
Skip based on checksum, rather than mod-time & size; Note that that O(archive) option is still enabled by default - the O(checksum) option will not disable it. | |
| compress bool |
Default: True |
Compress file data during the transfer. In most cases, leave this enabled unless it causes problems. |
| copy_links bool |
Copy symlinks as the item that they point to (the referent) is copied, rather than the symlink. | |
| delay_updates bool |
Default: True |
This option puts the temporary file from each updated file into a holding directory until the end of the transfer, at which time all the files are renamed into place in rapid succession. Version Added: 1.3.0 |
| delete bool |
Delete files in O(dest) that do not exist (after transfer, not before) in the O(src) path. This option requires O(recursive=true). This option ignores excluded files and behaves like the rsync opt C(--delete-after). |
|
| dest path required |
Path on the destination host that will be synchronized from the source. The path can be absolute or relative. |
|
| dest_port int |
Port number for ssh on the destination host. Prior to Ansible 2.0, the C(ansible_ssh_port) inventory var took precedence over this value. This parameter defaults to the value of C(ansible_port), the C(remote_port) config setting or the value from ssh client configuration if none of the former have been set. |
|
| dirs bool |
Transfer directories without recursing. | |
| existing_only bool |
Skip creating new files on receiver. | |
| group bool |
Preserve group. This parameter defaults to the value of the archive option. |
|
| link_dest list / elements=path |
Add a destination to hard link against during the rsync. | |
| links bool |
Copy symlinks as symlinks. This parameter defaults to the value of the archive option. |
|
| mode str |
Default: push Choices: pull, push |
Specify the direction of the synchronization. In V(push) mode the localhost or delegate is the source. In V(pull) mode the remote host in context is the source. |
| owner bool |
Preserve owner (super user only). This parameter defaults to the value of the archive option. |
|
| partial bool |
Tells rsync to keep the partial file which should make a subsequent transfer of the rest of the file much faster. | |
| perms bool |
Preserve permissions. This parameter defaults to the value of the archive option. |
|
| private_key path |
Specify the private key to use for SSH-based rsync connections (e.g. C(~/.ssh/id_rsa)). | |
| recursive bool |
Recurse into directories. This parameter defaults to the value of the archive option. |
|
| rsync_opts list / elements=str |
Specify additional rsync options by passing in an array. Note that an empty string in C(rsync_opts) will end up transfer the current working directory. |
|
| rsync_path str |
Specify the rsync command to run on the remote host. See C(--rsync-path) on the rsync man page. To specify the rsync command to run on the local host, you need to set this your task var C(ansible_rsync_path). |
|
| rsync_timeout int |
Specify a C(--timeout) for the rsync command in seconds. | |
| set_remote_user bool |
Default: True |
Put C(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 V(false). |
| src path required |
Path on the source host that will be synchronized to the destination. The path can be absolute or relative. |
|
| ssh_connection_multiplexing bool |
SSH connection multiplexing for rsync is disabled by default to prevent misconfigured ControlSockets from resulting in failed SSH connections. This is accomplished by setting the SSH C(ControlSocket) to C(none). Set this option to V(true) to allow multiplexing and reduce SSH connection overhead. Note that simply setting this option to V(true) is not enough; You must also configure SSH connection multiplexing in your SSH client config by setting values for C(ControlMaster), C(ControlPersist) and C(ControlPath). |
|
| times bool |
Preserve modification times. This parameter defaults to the value of the archive option. |
|
| use_ssh_args bool |
In Ansible 2.10 and lower, it uses the ssh_args specified in C(ansible.cfg). In Ansible 2.11 and onwards, when set to V(true), it uses all SSH connection configurations like C(ansible_ssh_args), C(ansible_ssh_common_args), and C(ansible_ssh_extra_args). |
|
| verify_host bool |
Verify destination host key. |
Notes¶
Note
- C(rsync) must be installed on both the local and remote host.
- For the M(ansible.posix.synchronize) module, the "local host" is the host I(the synchronize task originates on), and the "destination host" is the host I(synchronize is connecting to).
- The "local host" can be changed to a different host by using C(delegate_to). This enables copying between two remote hosts or entirely on one remote machine.
-
The user and permissions for the synchronize O(src) are those of the user running the Ansible task on the local host (or the remote_user for a C(delegate_to) host when C(delegate_to) is used).
-
The user and permissions for the synchronize
destare those of theremote_useron the destination host or thebecome_userifbecome=yesis active. - In Ansible 2.0 a bug in the synchronize module made become occur on the "local host". This was fixed in Ansible 2.0.1.
- Currently, M(ansible.posix.synchronize) is limited to elevating permissions via passwordless sudo. This is because rsync itself is connecting to the remote machine and rsync doesn't give us a way to pass sudo credentials in.
- Currently there are only a few connection types which support synchronize (ssh, paramiko, local, and docker) because a sync strategy has been determined for those connection types. Note that the connection for these must not need a password as rsync itself is making the connection and rsync does not provide us a way to pass a password to the connection.
- Expect that O(dest=~/x) will be V(~
/x) even if using sudo. - Inspect the verbose output to validate the destination user/host/path are what was expected.
- To exclude files and directories from being synchronized, you may add C(.rsync-filter) files to the source directory.
- rsync daemon must be up and running with correct permission when using rsync protocol in source or destination path.
- The C(synchronize) module enables C(--delay-updates) by default to avoid leaving a destination in a broken in-between state if the underlying rsync process encounters an error. Those synchronizing large numbers of files that are willing to trade safety for performance should disable this option.
- link_destination is subject to the same limitations as the underlying rsync daemon. Hard links are only preserved if the relative subtrees of the source and destination are the same. Attempts to hardlink into a directory that is a subdirectory of the source will be prevented.
Examples¶
- name: Synchronization of src on the control machine to dest on the remote hosts
ansible.posix.synchronize:
src: some/relative/path
dest: /some/absolute/path
- name: Synchronization using rsync protocol (push)
ansible.posix.synchronize:
src: some/relative/path/
dest: rsync://somehost.com/path/
- name: Synchronization using rsync protocol (pull)
ansible.posix.synchronize:
mode: pull
src: rsync://somehost.com/path/
dest: /some/absolute/path/
- name: Synchronization using rsync protocol on delegate host (push)
ansible.posix.synchronize:
src: /some/absolute/path/
dest: rsync://somehost.com/path/
delegate_to: delegate.host
- name: Synchronization using rsync protocol on delegate host (pull)
ansible.posix.synchronize:
mode: pull
src: rsync://somehost.com/path/
dest: /some/absolute/path/
delegate_to: delegate.host
- name: Synchronization without any --archive options enabled
ansible.posix.synchronize:
src: some/relative/path
dest: /some/absolute/path
archive: false
- name: Synchronization with --archive options enabled except for --recursive
ansible.posix.synchronize:
src: some/relative/path
dest: /some/absolute/path
recursive: false
- name: Synchronization with --archive options enabled except for --times, with --checksum option enabled
ansible.posix.synchronize:
src: some/relative/path
dest: /some/absolute/path
checksum: true
times: false
- name: Synchronization without --archive options enabled except use --links
ansible.posix.synchronize:
src: some/relative/path
dest: /some/absolute/path
archive: false
links: true
- name: Synchronization of two paths both on the control machine
ansible.posix.synchronize:
src: some/relative/path
dest: /some/absolute/path
delegate_to: localhost
- name: Synchronization of src on the inventory host to the dest on the localhost in pull mode
ansible.posix.synchronize:
mode: pull
src: some/relative/path
dest: /some/absolute/path
- name: Synchronization of src on delegate host to dest on the current inventory host.
ansible.posix.synchronize:
src: /first/absolute/path
dest: /second/absolute/path
delegate_to: delegate.host
- name: Synchronize two directories on one remote host.
ansible.posix.synchronize:
src: /first/absolute/path
dest: /second/absolute/path
delegate_to: "{{ inventory_hostname }}"
- name: Synchronize and delete files in dest on the remote host that are not found in src of localhost.
ansible.posix.synchronize:
src: some/relative/path
dest: /some/absolute/path
delete: true
recursive: true
# This specific command is granted su privileges on the destination
- name: Synchronize using an alternate rsync command
ansible.posix.synchronize:
src: some/relative/path
dest: /some/absolute/path
rsync_path: su -c rsync
# 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
- name: Synchronize passing in extra rsync options
ansible.posix.synchronize:
src: /tmp/helloworld
dest: /var/www/helloworld
rsync_opts:
- "--no-motd"
- "--exclude=.git"
# Hardlink files if they didn't change
- name: Use hardlinks when synchronizing filesystems
ansible.posix.synchronize:
src: /tmp/path_a/foo.txt
dest: /tmp/path_b/foo.txt
link_dest: /tmp/path_a/
# Specify the rsync binary to use on remote host and on local host
- hosts: groupofhosts
vars:
ansible_rsync_path: /usr/gnu/bin/rsync
tasks:
- name: copy /tmp/localpath/ to remote location /tmp/remotepath
ansible.posix.synchronize:
src: /tmp/localpath/
dest: /tmp/remotepath
rsync_path: /usr/gnu/bin/rsync
Authors¶
- Timothy Appnel (@tima)