Copy files from Windows remote hosts - Ansible module fetch
How to automate the copy and validate the checksum of example.txt file in the Desktop to a Linux Ansible Controller using Ansible Playbook and fetch module.


How to Copy files from Windows remote hosts with Ansible?
I’m going to show you a live demo with some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.
Ansible Copy files from Windows remote hosts
- ansible.builtin.fetch
- Copy files from remote nodes
Today we’re talking about the Ansible module 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
- dest path - the local path
- src string - Remote file path
- fail_on_missing boolean - yes / no
- validate_checksum boolean - yes / no
- flat boolean - no / yes
The parameter list is pretty wide but I’ll summarize the most useful. The only required parameters are “dest” which specifies a directory to save the file into and the “src” specifies the source files in the remote hosts. It must be a file, not a directory. The “fail_on_missing” boolean is set to true so the task is going to fail if the file doesn’t exist. The file is going to be transferred and validated in the source and the destination with a checksum. If we don’t want this behavior we could override with the “validate_checksum” option. The “flat” option allows you to override the default behavior of appending hostname/path/to/file to the destination.
The Best Resources For Ansible
Certifications
Video Course
Printed Book
eBooks
Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
demo
Copy files from Windows remote hosts with Ansible Playbook.
code
---
- name: fetch module demo
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 demo] **************************************************************************
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 demo] **************************************************************************
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 $
Recap
Now you know how to Copy files from Windows remote hosts with Ansible. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate