Note
This plugin is part of the ansible.posix collection.
To install it use: ansible-galaxy collection install ansible.posix
.
To use it in a playbook, specify: ansible.posix.synchronize
.
New in version 1.0.0: of ansible.posix
synchronize
is a wrapper around rsync to make common tasks in your playbooks quick and easy.command
action to call rsync yourself, but you also have to add a fair number of boilerplate options and host facts.still
may need to call rsync directly via command
or shell
depending on your use case.Note
This module has a corresponding action plugin.
Parameter | Choices/Defaults | Comments |
---|---|---|
archive boolean |
| Mirrors the rsync archive flag, enables recursive, links, perms, times, owner, group flags and -D. |
checksum boolean |
| 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. |
compress boolean |
| Compress file data during the transfer. In most cases, leave this enabled unless it causes problems. |
copy_links boolean |
| Copy symlinks as the item that they point to (the referent) is copied, rather than the symlink. |
delete boolean |
| Delete files in dest that don't exist (after transfer, not before) in the src path.This option requires recursive=yes .This option ignores excluded files and behaves like the rsync opt --delete-excluded. |
dest string / required | Path on the destination host that will be synchronized from the source. The path can be absolute or relative. | |
dest_port integer | Port number for ssh on the destination host. Prior to Ansible 2.0, the ansible_ssh_port inventory var took precedence over this value. This parameter defaults to the value of ansible_ssh_port or ansible_port , the remote_port config setting or the value from ssh client configuration if none of the former have been set. | |
dirs boolean |
| Transfer directories without recursing. |
existing_only boolean |
| Skip creating new files on receiver. |
group boolean |
| Preserve group. This parameter defaults to the value of the archive option. |
link_dest list / elements=string | Add a destination to hard link against during the rsync. | |
links boolean |
| Copy symlinks as symlinks. This parameter defaults to the value of the archive option. |
mode string |
| Specify the direction of the synchronization. In push mode the localhost or delegate is the source. In pull mode the remote host in context is the source. |
owner boolean |
| Preserve owner (super user only). This parameter defaults to the value of the archive option. |
partial boolean |
| Tells rsync to keep the partial file which should make a subsequent transfer of the rest of the file much faster. |
perms boolean |
| 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. ~/.ssh/id_rsa ). | |
recursive boolean |
| Recurse into directories. This parameter defaults to the value of the archive option. |
rsync_opts list / elements=string | Specify additional rsync options by passing in an array. Note that an empty string in rsync_opts will end up transfer the current working directory. | |
rsync_path string | Specify the rsync command to run on the remote host. See --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 ansible_rsync_path . | |
rsync_timeout integer | Default: 0 | Specify a --timeout for the rsync command in seconds. |
set_remote_user boolean |
| 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 . |
src string / required | Path on the source host that will be synchronized to the destination. The path can be absolute or relative. | |
times boolean |
| Preserve modification times. This parameter defaults to the value of the archive option. |
use_ssh_args boolean |
| Use the ssh_args specified in ansible.cfg. |
verify_host boolean |
| Verify destination host key. |
Note
synchronize
module, the “local host” is the host the synchronize task originates on
, and the “destination host” is the host synchronize is connecting to
.delegate_to
. This enables copying between two remote hosts or entirely on one remote machine.src
are those of the user running the Ansible task on the local host (or the remote_user for a delegate_to host when delegate_to is used).dest
are those of the remote_user
on the destination host or the become_user
if become=yes
is active..rsync-filter
files to the source directory.synchronize
module forces –delay-updates
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 call rsync directly.See also
The official documentation on the copy module.
The official documentation on the community.windows.win_robocopy module.
- 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: no - name: Synchronization with --archive options enabled except for --recursive ansible.posix.synchronize: src: some/relative/path dest: /some/absolute/path recursive: no - 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: yes times: no - name: Synchronization without --archive options enabled except use --links ansible.posix.synchronize: src: some/relative/path dest: /some/absolute/path archive: no links: yes - 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: yes recursive: yes # 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
© 2012–2018 Michael DeHaan
© 2018–2019 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/2.10/collections/ansible/posix/synchronize_module.html