AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible fetch Module: Copy Files from Remote Hosts to Control Node — Video Tutorial

How to copy files from remote hosts with Ansible fetch module (ansible.builtin.fetch). Download logs, configs, backups from Linux and Windows.

Watch on YouTube · Read the written article

Tutorial summary

What you'll learn

  • How to Copy files from Windows remote hosts with Ansible?
  • Ansible Copy files from Windows remote hosts
  • Parameters
  • code
  • execution
  • idempotency
  • before execution
  • after execution
  • Conclusion
  • Related Articles
How to Copy files from Windows remote hosts with Ansible? I'm going to show you a live Playbook 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](/articles/copy-files-to-remote-hosts-ansible-module-copy) for Linux and [Ansible win_copy module](/articles/copy-files-from-windows-remote-hosts-ansible-module-fetch) 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. ## Playbook Copy files from Windows remote hosts with Ansible Playbook. code ```yaml --- - 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 ```bash 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 ```bash ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory copy\ files\ from\ remote\ hosts/fetch-windows.yml PLAY [fetch module Playbook] **************************************************************

About this tutorial

  • Author: Luca Berton
  • Difficulty: Beginner
  • Read time: 3 min
  • Category: troubleshooting

Topics covered

Related video tutorials