What you'll learn
- How to Backup Windows 10/11/2019/2022 with Ansible Playbook: win_robocopy vs win_copy
- Ansible backing up with RoboCopy
- Parameters
- Links
- code
- execution
- idempotency
- before execution
- after execution
- Conclusion
How to Backup Windows 10/11/2019/2022 with Ansible Playbook: win_robocopy vs win_copy
Ansible backing up with RoboCopy
> `community.windows.win_robocopy`: Synchronizes the contents of two directories using Robocopy
For a Windows backup playbook on Windows 10, Windows 11, Windows Server 2019, or Windows Server 2022, use `community.windows.win_robocopy` when you need to synchronize a whole directory tree. Use `ansible.windows.win_copy` for smaller one-off file copies from the Ansible controller to a Windows host; use `win_robocopy` for Robocopy-style backups between paths available on the Windows target.
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.
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] **********************************