AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 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 "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, 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 Backup Windows 10/11/2019/2022: win_robocopy Playbook (Guide) — Video Tutorial
How to automate the backup synchronization of an “examples” folder to “examples-backup” on Windows using RoboCopy with Ansible module win_robocopy.
What You'll Learn
- How to Backup With Robocopy on Windows with Ansible?
- Ansible backing up with RoboCopy
- Parameters
- Links
- code
- execution
- idempotency
- before execution
- after execution
- Conclusion
Full Tutorial Content
How to Backup With Robocopy on Windows with Ansible?
I'm going to show you a live Playbook and some simple Ansible code.
I'm Luca Berton and welcome to today's episode of Ansible Pilot.
Ansible backing up with RoboCopy
> `community.windows.win_robocopy`: Synchronizes the contents of two directories using Robocopy
Today we're talking about the Ansible module `win_robocopy`.
The full name is `community.windows.win_robocopy`, which means that is part of the collection targeting Windows platforms.
Synchronizes the contents of two directories using Robocopy.
Under the hood, it uses the RoboCopy utility, since that should be available on most modern Windows systems.
Parameters
- `src` _string_ - source path - absolute or relative
- `dest` _string_ - destination path - absolute or relative
- `recurse` _string_ - no/yes - Includes all subdirectories
- `purge` _string_ - no/yes - Deletes any files/directories found in the destination that do not exist in the source.
- `flags` _string_ - Additional flags
Let's see the parameter of the `win_robocopy` module.
The only mandatory parameters are "src" and "dest" parameters.
The "src" parameter is mandatory and specifies the path on the source host that will be synchronized to the destination. The path can be absolute or relative.
Same story for the `dest` parameter that specifies the path on the destination host that will be synchronized from the source.
Paths could be Local or Remote according to your needs.
The "recurse" parameter is default as disabled but you should consider enabling it when you want to synchronize all the subdirectories.
If you need to delete all the files and directories found in the destination that do not exist in the source you must enable the purge parameter.
It's possible to specify additionals Robocopy parameters via the "flags" parameter, but not all are supported.
Links
- [community.windows.win_robocopy](https://docs.ansible.com/ansible/latest/collections/community/windows/win_robocopy_module.html)
- [RoboCopy](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy)
## Playbook
How to Backup With Robocopy on Windows with Ansible Playbook.
I'm going to show you how to replicate some "examples" directory tree in "examples-backup" a Windows machine using the RoboCopy utility via the `win_robocopy` module.
code
```yaml
---
- name: win_robocopy module Playbook
hosts: all
become: false
vars:
source: "Desktop/examples"
destination: "Desktop/examples-backup"
tasks:
- name: data syncronization
community.windows.win_robocopy:
src: '{{ source }}'
dest: '{{ destination }}'
```
execution
```bash
ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory copy\ files\ to\ remote\ hosts/win_robocopy.yml
PLAY [win_robocopy module Playbook] *******************************************************************
TASK [Gathering Facts] ************************************************************************
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 3 min
- Category: troubleshooting
Read the full written article: Ansible Backup Windows 10/11/2019/2022: win_robocopy Playbook (Guide)
Related Video Tutorials
- Ansible synchronize Module: Rsync Files & Directories Between Hosts — Discover how to use Ansible synchronize module for efficient file backups with rsync. Learn practical steps and see real-world examples for Linux systems.
- Ansible win_file Module: Create Directory on Windows Hosts (Guide) — How to create directories on Windows with Ansible win_file module (ansible.windows.win_file). Set paths, manage folders, handle permissions. Practical YAML playbook examples for Windows automation.
- Ansible Delete Files in Directory: find & file Modules (Examples) — How to delete only files inside a directory in Ansible using find and file modules. Clean up logs, temp files, and old backups by age, pattern, and size.
- Ansible win_stat: Check if File or Directory Exists on Windows (Examples) — How to check if a file or directory exists on Windows using Ansible win_stat module. Conditional tasks, file properties, checksum verification with practical examples.
- Ansible Create Directory: file Module with state=directory (Guide) — How to create directories in Ansible with the file module state=directory. Set permissions, owner, group, recursive creation. Create multiple directories with loops. Practical YAML examples.
- Ansible win_file Module: Create & Manage Files on Windows (Guide) — How to create and manage files on Windows with Ansible win_file module (ansible.windows.win_file). Create files, directories, symlinks on Windows. Practical YAML playbook examples.