Ansible is one of the most popular open-source automation and configuration management tools, allowing infrastructure teams to efficiently manage systems at scale through simple yet powerful playbooks. A standout capability of Ansible is its support for not only Linux/Unix systems but also Windows environments.
One module that demonstrates Ansible‘s cross-platform prowess is the win_copy module. As the name suggests, win_copy facilitates seamless file transfers between Ansible controller and Windows hosts. In this comprehensive guide, we‘ll explore common and advanced usage of win_copy for streamlining file operations across heterogeneous infrastructure.
Overview of win_copy Module
The win_copy module is part of the ansible.windows collection. As mentioned earlier, it allows copying files from Ansible control node to remote Windows machines.
Key highlights of the module:
- Supports copying single files, multiple files or entire directories recursively.
- Can create backup copies before overwriting existing files.
- Has check mode to preview file changes.
- Handles large files efficiently without taxing controller resources.
Now let‘s see this powerful module in action through some practical examples.
Installing the Ansible Windows Collection
The win_copy module resides in the Ansible-developed windows collection, so we need to install it first:
$ ansible-galaxy collection install ansible.windows
This fetches the latest version of the collection containing 100+ Windows-centric modules.
Copying a Single File
Let‘s start with a simple playbook to copy a file from Ansible control node to Windows host:
- hosts: windows
tasks:
- name: Copy config.txt to C:\temp
ansible.windows.win_copy:
src: /ansible/files/config.txt
dest: C:\temp
Here src points to file location on the Ansible controller while dest defines the target path on Windows. By default, win_copy overwrites existing destination files.
We can also inline file content instead of external file through content parameter:
- name: Create foo.txt with inline content
ansible.windows.win_copy:
content: |
Hello world!
This is foo file content
dest: C:\temp\foo.txt
This creates foo.txt at given location with provided content.
Copying Multiple Files
To copy multiple files in one go, set src to a folder path containing those files:
- name: Copy scripts folder recursively
ansible.windows.win_copy:
src: /ansible/files/scripts/
dest: C:\temp\scripts
The trailing slash indicates that scripts folder should be copied recurisvely. All files within scripts directory on controller will be copied maintaining same structure.
Preserving Filenames While Copying
If you just want to dump a file inside destination folder without explicitly specifying filename, provide folder path instead of full file path in dest:
- name: Copy file preserving filename
ansible.windows.win_copy:
src: /ansible/files/config.txt
dest: C:\temp
So config.txt will be copied as C:\temp\config.txt. This comes handy when iterating over a folder containing multiple files.
Creating Backup Copies
To safely copy over existing destination files, we can enable backup creation through backup parameter:
- name: Copy foo.txt creating backup
ansible.windows.win_copy:
src: /ansible/files/foo.txt
dest: C:\temp\foo.txt
backup: true
If foo.txt already exists at target location, it will be renamed to foo.txt.~before_ansible_backup~ before copying updated file.
The backup name contains timestamp, so running the task multiple times will maintain separate versions.
Copying Files Conditionally
We can control file copy operation based on certain conditions using when clause:
- name: Copy if source file exists
ansible.windows.win_copy:
src: /ansible/files/foo.txt
dest: C:\temp\foo.txt
when: ansible_local.file_foo_txt.stat.exists
Here foo.txt will be copied only if it‘s present in controller‘s file system.
We can use any arbitrary condition here leveraging facts like – file timestamps, sizes, etc.
Using Check Mode for Dry Run
It‘s considered a best practice to test playbook changes through Ansible‘s check mode before actual execution.
Check mode shows what changes would occur without applying them:
$ ansible-playbook copy-playbook.yml --check
This test run allows spotting any potential issues upfront without impacting existing systems.
Copying Directories Recursively
We already saw how win_copy can recursively copy folders maintaining full hierarchy. One special case is selectively copying subdirectories and files rather than entire tree.
This can be achieved through remote_src parameter like below:
- name: Selective copy from Windows
ansible.windows.win_copy:
remote_src: yes
src: C:\source\scripts\utils
dest: /ansible/backups/scripts
Setting remote_src: true denotes that source is on remote Windows machine instead of controller. So only utils subfolder from the given path will be copied.
Handling Large File Transfers Efficiently
Copying large files through Ansible controller is inefficient as it consumes controller disk space and network bandwidth between controller and managed node.
win_get_url provides a better approach – it directly downloads file on Windows host from a remote URL without routing it via controller:
- name: Download tensorflow binaries
ansible.windows.win_get_url:
url: https://xyz.com/tensorflow.zip
dest: C:\temp
For ultra large binaries, this method is highly recommended.
Understanding Win_copy Limitations
While win_copy makes Windows file transfers easier, it comes with a few limitations:
- Maximum file size limited to available controller disk space
- No native support for advanced protocols like SFTP/SCP
- Limited error handling capabilities
So for advanced use cases, native Windows PowerShell resources may provide better flexibility.
Conclusion
Ansible‘s win_copy module takes away complexity of managing Windows file transfers through automated, idempotent playbooks. We explored it‘s versatile options for copying files/folders, creating backups, conditional tasks, check mode and more.
For large binaries, win_get_url is the way to go as it directly downloads onto Windows host.
Overall, win_copy dramatically cuts down tedious manual file operations – helping engineers focus on writing business logic rather than spend time on filesystem grunt work!


