Backup With Robocopy on Windows - Ansible module win_robocopy
How to automate the backup synchronization of an “examples” folder to “examples-backup” on Windows using RoboCopy with Ansible module win_robocopy.


How to Backup With Robocopy on Windows with Ansible?
I’m going to show you a live demo 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
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
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
---
- name: win_robocopy module demo
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
ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory copy\ files\ to\ remote\ hosts/win_robocopy.yml
PLAY [win_robocopy module demo] *******************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [WindowsServer]
TASK [data synchronization] ***********************************************************************
changed: [WindowsServer]
PLAY RECAP ****************************************************************************************
WindowsServer : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
idempotency
ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory copy\ files\ to\ remote\ hosts/win_robocopy.yml
PLAY [win_robocopy module demo] *******************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [WindowsServer]
TASK [data synchronization] ***********************************************************************
ok: [WindowsServer]
PLAY RECAP ****************************************************************************************
WindowsServer : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
before execution
after execution
Recap
Now you know how to back up With Robocopy on Windows 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