ansible.builtin.fetch (v2.16.5) — module

Fetch files from remote nodes

| "added in version" 0.2 of ansible.builtin"

Authors: Ansible Core Team, Michael DeHaan

This plugin has a corresponding action plugin.

Install Ansible via pip

Install with pip install ansible-core==2.16.5

Description

This module works like M(ansible.builtin.copy), but in reverse.

It is used for fetching files from remote machines and storing them locally in a file tree, organized by hostname.

Files that already exist at O(dest) will be overwritten if they are different than the O(src).

This module is also supported for Windows targets.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Store file into /tmp/fetched/host.example.com/tmp/somefile
  ansible.builtin.fetch:
    src: /tmp/somefile
    dest: /tmp/fetched
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Specifying a path directly
  ansible.builtin.fetch:
    src: /tmp/somefile
    dest: /tmp/prefix-{{ inventory_hostname }}
    flat: yes
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Specifying a destination path
  ansible.builtin.fetch:
    src: /tmp/uniquefile
    dest: /tmp/special/
    flat: yes
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Storing in a path relative to the playbook
  ansible.builtin.fetch:
    src: /tmp/uniquefile
    dest: special/prefix-{{ inventory_hostname }}
    flat: yes

Inputs

    
src:
    description:
    - The file on the remote system to fetch.
    - This I(must) be a file, not a directory.
    - Recursive fetching may be supported in a later release.
    required: true

dest:
    description:
    - A directory to save the file into.
    - For example, if the O(dest) directory is C(/backup) a O(src) file named C(/etc/profile)
      on host C(host.example.com), would be saved into C(/backup/host.example.com/etc/profile).
      The host name is based on the inventory name.
    required: true

flat:
    default: false
    description:
    - Allows you to override the default behavior of appending hostname/path/to/file to
      the destination.
    - If O(dest) ends with '/', it will use the basename of the source file, similar to
      the copy module.
    - This can be useful if working with a single host, or if retrieving files that are
      uniquely named per host.
    - If using multiple hosts with the same filename, the file will be overwritten for
      each host.
    type: bool
    version_added: '1.2'
    version_added_collection: ansible.builtin

fail_on_missing:
    default: true
    description:
    - When set to V(true), the task will fail if the remote file cannot be read for any
      reason.
    - Prior to Ansible 2.5, setting this would only fail if the source file was missing.
    - The default was changed to V(true) in Ansible 2.5.
    type: bool
    version_added: '1.1'
    version_added_collection: ansible.builtin

validate_checksum:
    default: true
    description:
    - Verify that the source and destination checksums match after the files are fetched.
    type: bool
    version_added: '1.4'
    version_added_collection: ansible.builtin

See also