ansible.posix.synchronize (1.5.4) — module

A wrapper around rsync to make common tasks in your playbooks quick and easy

| "added in version" 1.0.0 of ansible.posix"

Authors: Timothy Appnel (@tima)

This plugin has a corresponding action plugin.

Install collection

Install with ansible-galaxy collection install ansible.posix:==1.5.4


Add to requirements.yml

  collections:
    - name: ansible.posix
      version: 1.5.4

Description

C(synchronize) is a wrapper around 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 C(command) action to call 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 rsync, but does make the most common invocations easier to implement. You `still` may need to call rsync directly via C(command) or C(shell) depending on your use case.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Synchronization using rsync protocol (push)
  ansible.posix.synchronize:
    src: some/relative/path/
    dest: rsync://somehost.com/path/
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Synchronization using rsync protocol (pull)
  ansible.posix.synchronize:
    mode: pull
    src: rsync://somehost.com/path/
    dest: /some/absolute/path/
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Synchronization without any --archive options enabled
  ansible.posix.synchronize:
    src: some/relative/path
    dest: /some/absolute/path
    archive: false
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Synchronization with --archive options enabled except for --recursive
  ansible.posix.synchronize:
    src: some/relative/path
    dest: /some/absolute/path
    recursive: false
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Synchronization without --archive options enabled except use --links
  ansible.posix.synchronize:
    src: some/relative/path
    dest: /some/absolute/path
    archive: false
    links: true
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Synchronization of two paths both on the control machine
  ansible.posix.synchronize:
    src: some/relative/path
    dest: /some/absolute/path
  delegate_to: localhost
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Synchronize two directories on one remote host.
  ansible.posix.synchronize:
    src: /first/absolute/path
    dest: /second/absolute/path
  delegate_to: "{{ inventory_hostname }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# 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
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# 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"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# 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/
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# 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

Inputs

    
src:
    description:
    - Path on the source host that will be synchronized to the destination.
    - The path can be absolute or relative.
    required: true
    type: str

dest:
    description:
    - Path on the destination host that will be synchronized from the source.
    - The path can be absolute or relative.
    required: true
    type: str

dirs:
    default: false
    description:
    - Transfer directories without recursing.
    type: bool

mode:
    choices:
    - pull
    - push
    default: push
    description:
    - 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.
    type: str

group:
    description:
    - Preserve group.
    - This parameter defaults to the value of the archive option.
    type: bool

links:
    description:
    - Copy symlinks as symlinks.
    - This parameter defaults to the value of the archive option.
    type: bool

owner:
    description:
    - Preserve owner (super user only).
    - This parameter defaults to the value of the archive option.
    type: bool

perms:
    description:
    - Preserve permissions.
    - This parameter defaults to the value of the archive option.
    type: bool

times:
    description:
    - Preserve modification times.
    - This parameter defaults to the value of the archive option.
    type: bool

delete:
    default: false
    description:
    - Delete files in I(dest) that do not exist (after transfer, not before) in the I(src)
      path.
    - This option requires I(recursive=true).
    - This option ignores excluded files and behaves like the rsync opt C(--delete-after).
    type: bool

archive:
    default: true
    description:
    - Mirrors the rsync archive flag, enables recursive, links, perms, times, owner, group
      flags and -D.
    type: bool

partial:
    default: false
    description:
    - Tells rsync to keep the partial file which should make a subsequent transfer of
      the rest of the file much faster.
    type: bool

checksum:
    default: false
    description:
    - 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.
    type: bool

compress:
    default: true
    description:
    - Compress file data during the transfer.
    - In most cases, leave this enabled unless it causes problems.
    type: bool

dest_port:
    description:
    - 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 C(ansible_port), the C(remote_port) config
      setting or the value from ssh client configuration if none of the former have been
      set.
    type: int

link_dest:
    default: null
    description:
    - Add a destination to hard link against during the rsync.
    elements: str
    type: list

recursive:
    description:
    - Recurse into directories.
    - This parameter defaults to the value of the archive option.
    type: bool

copy_links:
    default: false
    description:
    - Copy symlinks as the item that they point to (the referent) is copied, rather than
      the symlink.
    type: bool

rsync_opts:
    default: null
    description:
    - 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.
    elements: str
    type: list

rsync_path:
    description:
    - 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).
    type: str

private_key:
    description:
    - Specify the private key to use for SSH-based rsync connections (e.g. C(~/.ssh/id_rsa)).
    type: path

verify_host:
    default: false
    description:
    - Verify destination host key.
    type: bool

use_ssh_args:
    default: false
    description:
    - 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 C(true), it uses all SSH connection configurations
      like C(ansible_ssh_args), C(ansible_ssh_common_args), and C(ansible_ssh_extra_args).
    type: bool

delay_updates:
    default: true
    description:
    - 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.
    type: bool
    version_added: 1.3.0
    version_added_collection: ansible.posix

existing_only:
    default: false
    description:
    - Skip creating new files on receiver.
    type: bool

rsync_timeout:
    default: 0
    description:
    - Specify a C(--timeout) for the rsync command in seconds.
    type: int

set_remote_user:
    default: true
    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 C(false).
    type: bool

ssh_connection_multiplexing:
    default: false
    description:
    - 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 C(true) to allow multiplexing and reduce SSH connection overhead.
    - Note that simply setting this option to C(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).
    type: bool

See also