Ansible fetch Module: Copy Files from Remote Hosts to Control Node
By Luca Berton · Published 2024-01-01 · Category: troubleshooting
How to copy files from remote hosts with Ansible fetch module (ansible.builtin.fetch). Download logs, configs, backups from Linux and Windows.

How to Copy files from Windows remote hosts with Ansible?
See also: Ansible win_copy Module: Copy Files to Windows Hosts (ansible.windows.win_copy)
Ansible Copy files from Windows remote hosts
- ansible.builtin.fetch
- Copy files from remote nodes
fetch.
The full name is ansible.builtin.fetch which means is part of the collection of modules "builtin" with ansible and shipped with it.
This module is pretty stable and out for years.
The purpose is to copy files from remote locations. Please note that the opposite is done by Ansible copy module for Linux and Ansible win_copy module for Windows.
Parameters
destpath - the local pathsrcstring - Remote file pathfail_on_missingboolean -yes/novalidate_checksumboolean -yes/noflatboolean -no/yes
## Playbook
Copy files from Windows remote hosts with Ansible Playbook.
code
---
- name: fetch module Playbook
hosts: all
become: false
vars:
myfile: 'C:\Users\vagrant\Desktop\example.txt'
dump_dir: "logs"
tasks:
- name: fetch file
ansible.builtin.fetch:
src: "{{ myfile }}"
dest: "{{ dump_dir }}"
execution
ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory copy\ files\ from\ remote\ hosts/fetch-windows.yml
PLAY [fetch module Playbook] **************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [WindowsServer]
TASK [fetch file] *********************************************************************************
changed: [WindowsServer]
PLAY RECAP ****************************************************************************************
WindowsServer : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
idempotency
ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory copy\ files\ from\ remote\ hosts/fetch-windows.yml
PLAY [fetch module Playbook] **************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [WindowsServer]
TASK [fetch file] *********************************************************************************
ok: [WindowsServer]
PLAY RECAP ****************************************************************************************
WindowsServer : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $before execution
after execution
ansible-pilot $ ls -al copy\ files\ from\ remote\ hosts/logs
total 0
drwxr-xr-x 3 lberton staff 96 Jan 31 12:34 .
drwxr-xr-x 5 lberton staff 160 Jan 31 12:34 ..
drwxr-xr-x 3 lberton staff 96 Jan 31 12:34 WindowsServer
ansible-pilot $ ls -al copy\ files\ from\ remote\ hosts/logs/WindowsServer
total 0
drwxr-xr-x 3 lberton staff 96 Jan 31 12:34 .
drwxr-xr-x 3 lberton staff 96 Jan 31 12:34 ..
drwxr-xr-x 3 lberton staff 96 Jan 31 12:34 C:
ansible-pilot $ ls -al copy\ files\ from\ remote\ hosts/logs/WindowsServer/C:/Users/vagrant/Desktop
total 8
drwxr-xr-x 3 lberton staff 96 Jan 31 12:34 .
drwxr-xr-x 3 lberton staff 96 Jan 31 12:34 ..
-rw-r--r-- 1 lberton staff 15 Jan 31 12:34 example.txt
ansible-pilot $ cat copy\ files\ from\ remote\ hosts/logs/WindowsServer/C:/Users/vagrant/Desktop/example.txt
example content%
ansible-pilot $See also: Ansible fetch Module: Download Files from Remote Hosts (Complete Guide)
Conclusion
Now you know how to Copy files from Windows remote hosts with Ansible.Related Articles
Category: troubleshooting
Watch the video: Ansible fetch Module: Copy Files from Remote Hosts to Control Node — Video Tutorial