| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright: (c) 2017, Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
# This is a virtual module that is entirely implemented as an action plugin and runs on the controller |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = r""" |
| 12 |
--- |
| 13 |
module: fetch |
| 14 |
short_description: Fetch files from remote nodes |
| 15 |
description: |
| 16 |
- This module works like M(ansible.builtin.copy), but in reverse. |
| 17 |
- It is used for fetching files from remote machines and storing them locally in a file tree, organized by hostname. |
| 18 |
- Files that already exist at O(dest) will be overwritten if they are different than the O(src). |
| 19 |
- This module is also supported for Windows targets. |
| 20 |
version_added: '0.2' |
| 21 |
options: |
| 22 |
src: |
| 23 |
description: |
| 24 |
- The file on the remote system to fetch. |
| 25 |
- This I(must) be a file, not a directory. |
| 26 |
- Recursive fetching may be supported in a later release. |
| 27 |
required: yes |
| 28 |
dest: |
| 29 |
description: |
| 30 |
- A directory to save the file into. |
| 31 |
- For example, if O(dest=/backup), then O(src=/etc/profile) on host |
| 32 |
C(host.example.com), would save the file into C(/backup/host.example.com/etc/profile). |
| 33 |
The host name is based on the inventory name. |
| 34 |
required: yes |
| 35 |
fail_on_missing: |
| 36 |
version_added: '1.1' |
| 37 |
description: |
| 38 |
- When set to V(true), the task will fail if the remote file cannot be read for any reason. |
| 39 |
- Prior to Ansible 2.5, setting this would only fail if the source file was missing. |
| 40 |
- The default was changed to V(true) in Ansible 2.5. |
| 41 |
type: bool |
| 42 |
default: yes |
| 43 |
validate_checksum: |
| 44 |
version_added: '1.4' |
| 45 |
description: |
| 46 |
- Verify that the source and destination checksums match after the files are fetched. |
| 47 |
type: bool |
| 48 |
default: yes |
| 49 |
flat: |
| 50 |
version_added: '1.2' |
| 51 |
description: |
| 52 |
- Allows you to override the default behavior of appending hostname/path/to/file to the destination. |
| 53 |
- If O(dest) ends with '/', it will use the basename of the source file, similar to the copy module. |
| 54 |
- This can be useful if working with a single host, or if retrieving files that are uniquely named per host. |
| 55 |
- If using multiple hosts with the same filename, the file will be overwritten for each host. |
| 56 |
type: bool |
| 57 |
default: no |
| 58 |
extends_documentation_fragment: |
| 59 |
- action_common_attributes |
| 60 |
- action_common_attributes.files |
| 61 |
- action_common_attributes.flow |
| 62 |
attributes: |
| 63 |
action: |
| 64 |
support: full |
| 65 |
async: |
| 66 |
support: none |
| 67 |
bypass_host_loop: |
| 68 |
support: none |
| 69 |
check_mode: |
| 70 |
support: full |
| 71 |
diff_mode: |
| 72 |
support: full |
| 73 |
platform: |
| 74 |
platforms: posix, windows |
| 75 |
safe_file_operations: |
| 76 |
support: none |
| 77 |
vault: |
| 78 |
support: none |
| 79 |
notes: |
| 80 |
- When running fetch with C(become), the M(ansible.builtin.slurp) module will also be |
| 81 |
used to fetch the contents of the file for determining the remote |
| 82 |
checksum. This effectively doubles the transfer size, and |
| 83 |
depending on the file size can consume all available memory on the |
| 84 |
remote or local hosts causing a C(MemoryError). Due to this it is |
| 85 |
advisable to run this module without C(become) whenever possible. |
| 86 |
- Prior to Ansible 2.5 this module would not fail if reading the remote |
| 87 |
file was impossible unless O(fail_on_missing) was set. |
| 88 |
- In Ansible 2.5 or later, playbook authors are encouraged to use |
| 89 |
C(failed_when) or C(ignore_errors) to get this ability. They may |
| 90 |
also explicitly set O(fail_on_missing) to V(false) to get the |
| 91 |
non-failing behaviour. |
| 92 |
seealso: |
| 93 |
- module: ansible.builtin.copy |
| 94 |
- module: ansible.builtin.slurp |
| 95 |
author: |
| 96 |
- Ansible Core Team |
| 97 |
- Michael DeHaan |
| 98 |
""" |
| 99 |
|
| 100 |
EXAMPLES = r""" |
| 101 |
- name: Store file into /tmp/fetched/host.example.com/tmp/somefile |
| 102 |
ansible.builtin.fetch: |
| 103 |
src: /tmp/somefile |
| 104 |
dest: /tmp/fetched |
| 105 |
|
| 106 |
- name: Specifying a path directly |
| 107 |
ansible.builtin.fetch: |
| 108 |
src: /tmp/somefile |
| 109 |
dest: /tmp/prefix-{{ inventory_hostname }} |
| 110 |
flat: yes |
| 111 |
|
| 112 |
- name: Specifying a destination path |
| 113 |
ansible.builtin.fetch: |
| 114 |
src: /tmp/uniquefile |
| 115 |
dest: /tmp/special/ |
| 116 |
flat: yes |
| 117 |
|
| 118 |
- name: Storing in a path relative to the playbook |
| 119 |
ansible.builtin.fetch: |
| 120 |
src: /tmp/uniquefile |
| 121 |
dest: special/prefix-{{ inventory_hostname }} |
| 122 |
flat: yes |
| 123 |
""" |