模块参数

$ ansible-doc -s fetch
- name: Fetch files from remote nodes
  fetch:
      dest:                  # (required) A directory to save the file into. For example, if the 'dest'
                               directory is '/backup' a 'src' file named
                               '/etc/profile' on host 'host.example.com', would
                               be saved into
                               '/backup/host.example.com/etc/profile'. The host
                               name is based on the inventory name.
      fail_on_missing:       # When set to 'yes', 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 'yes' in Ansible 2.5.
      flat:                  # Allows you to override the default behavior of appending hostname/path/to/file
                               to the destination. If '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.
      src:                   # (required) The file on the remote system to fetch. This 'must' be a file, not a
                               directory. Recursive fetching may be supported
                               in a later release.
      validate_checksum:     # Verify that the source and destination checksums match after the files are
                               fetched.

示例代码

- name: test
  hosts: 127.0.0.1  # 修改为远程主机IP
  remote_user: root
  gather_facts: False
  tasks:
  - name: "获取远程文件路径"
    find:
      paths: "/tmp/"
      patterns: "test*.py"
    register: remote_paths

  - debug:
      msg: "{{ item.path }}"
    with_items: "{{ remote_paths.files }}"
    loop_control:
      label: "{{ item.path }}"

  - name: "获取文件到本机"
    fetch:
      src: "{{ item.path }}"
      dest: /data/tmp_file/
      # flat: true
    with_items: "{{ remote_paths.files }}"
    loop_control:
      label: "{{ item.path }}"

运行结果

PLAY [test] ***************************************************************************************************

TASK [获取远程文件路径] ***********************************************************************************************
ok: [127.0.0.1]

TASK [debug] **************************************************************************************************
ok: [127.0.0.1] => (item=/tmp/test_21.py) => {
    "msg": "/tmp/test_21.py"
}
ok: [127.0.0.1] => (item=/tmp/test_crypto.py) => {
    "msg": "/tmp/test_crypto.py"
}
ok: [127.0.0.1] => (item=/tmp/test_subprocess.py) => {
    "msg": "/tmp/test_subprocess.py"
}

TASK [获取文件到本机] ************************************************************************************************
ok: [127.0.0.1] => (item=/tmp/test_21.py)
ok: [127.0.0.1] => (item=/tmp/test_crypto.py)
ok: [127.0.0.1] => (item=/tmp/test_subprocess.py)

PLAY RECAP ****************************************************************************************************
127.0.0.1                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0