Ansible win_copy: Copy Files to Windows Hosts (Complete Guide)
By Luca Berton · Published 2024-01-01 · Category: installation
How to copy files to Windows hosts with Ansible win_copy module. Transfer files, set permissions, copy directories, and manage Windows file operations.
How to copy files to Windows remote hosts? 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 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.
Demo Let's jump in a real-life playbook to copy files to Windows remote hosts with Ansible. • copy.yml • report.txt
Conclusion Now you know how to copy files to remote Windows hosts with Ansible.
Copy File to Windows
Copy Directory
Copy Content (Inline)
Copy with Backup
Remote to Remote Copy
Deployment Pattern
win_copy vs win_template
| Module | Use Case | |--------|----------| | win_copy | Static files (as-is) | | win_template | Files with Jinja2 variables | | win_get_url | Download from URL | | win_robocopy | Large directory sync |
Key Parameters
| Parameter | Description | |-----------|-------------| | src | Source file/directory (controller) | | dest | Destination path (Windows) | | content | Inline content to write | | remote_src | Source is on remote host | | backup | Create backup of existing file | | force | Replace if different (default: true) |
FAQ
How do I copy large files efficiently?
For large directories, community.windows.win_robocopy is faster:
Linux copy vs win_copy?
Use copy for Linux, win_copy for Windows. They have the same interface but different backends.
How do I set file permissions on Windows?
Use win_acl after copying:
Copy File to Windows
Copy with Content
Copy Directory
Remote to Remote
Force and Backup
Copy Multiple Files
Deploy Application Files
win_copy vs win_template
| Module | Use Case | |--------|----------| | win_copy | Static files, binaries, inline content | | win_template | Files with Jinja2 variables/logic | | win_get_url | Download from HTTP/S | | win_robocopy | Large directory sync |
Fetch from Windows
Key Parameters
| Parameter | Description | |-----------|-------------| | src | Source file/dir on controller | | dest | Destination on Windows host | | content | Inline content (instead of src) | | backup | Keep backup of original | | force | Overwrite if different | | remote_src | Source is on remote Windows |
FAQ
Path format — forward or backslash?
Both work in YAML. Use backslash for clarity: C:\MyApp\config. In YAML, single-quote paths to avoid escape issues.
Large files are slow?
WinRM has overhead for large transfers. For large files, use win_get_url to download from a file share or HTTP server.
How do I set Windows file permissions?
Related Articles • Ansible Become Guide • Ansible Roles Guide • Ansible for Windows Guide
Category: installation
Watch the video: Ansible win_copy: Copy Files to Windows Hosts (Complete Guide) — Video Tutorial