Copy files to Windows remote hosts - Local to Remote - Ansible module win_copy
How to copy report.txt file to Windows remote hosts using Ansible module win_copy.


How to copy files to Windows remote hosts?
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 to Windows remote hosts
Today we’re talking about Ansible module win_copy.
The full name is ansible.windows.win_copy
which means is part of the collection of modules “ansible.windows” targeted windows remote hosts.
This module is pretty stable and out for years.
The purpose is to copy files from the local machine to remote locations.
Because win_copy runs over WinRM, it is not a very efficient transfer mechanism. If you plan to send large files consider hosting them on a web service and using ansible.windows.win_get_url
instead.
Please note that the opposite is done by module win_fetch.
On Linux target use the module ansible.builtin.copy
.
Please note that the opposite is done by module win_fetch
.
On Linux target use the Ansible copy module..
Parameters
- dest path - remote path
- src string - local path
- backup boolean - no / yes
- force string - yes / no
I’m going to summarize the most useful parameters. The only required parameter is “dest” which specifies the remote absolute path destination. It’s better with “", the Windows way, instead of “/”, the Unix way. Please also verify that the remote user has the right to write in this path. The “src” specifies the source file in the controller host. It could be a relative or absolute path. I recommend absolutely. If the source is a directory all the content is copied. Please be more careful with the “/” at the end. If present only the content of the directory is copied, if you want the directory and the content you need to omit the “/” character. The “backup” boolean parameter allows you to create a backup if the utility overwrites any file. The default behavior is to transfer the file only if not present or differ in the target host, the file is also verified by Ansible with a checksum on the source and target host automatically. If you want to transfer the all the time the file you need to toggle the “force” parameter to “no”. Please note that setting to “no” also disables the checksum that could speed up the process but also corrupt the file.
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- 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 Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- 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
Let’s jump in a real-life playbook to copy files to Windows remote hosts with Ansible.
- copy.yml
---
- name: win_copy module demo
hosts: all
become: false
gather_facts: false
vars:
source: "report.txt"
destination: "Desktop/report.txt"
tasks:
- name: copy report.txt
ansible.windows.win_copy:
src: "{{ source }}"
dest: "{{ destination }}"
- report.txt
test report.txt
Recap
Now you know how to copy files to remote Windows 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