Ansible Code Backup
This role backs up the entire Ansible repository (playbooks, roles, configurations) to dual NAS storage for redundancy.
Ansible Code Backup Role
Overview
This role backs up the entire Ansible repository (playbooks, roles, configurations) to dual NAS storage for redundancy. It creates timestamped compressed archives and automatically manages retention by keeping only the last 5 backups per NAS.
Purpose
- Disaster Recovery: Protects your infrastructure-as-code against accidental deletion or corruption
- Version History: Maintains 5 timestamped backups for rollback capability
- Dual Redundancy: Backs up to two separate NAS devices (Synology DS418 and Proxmox OMV)
- Automated Cleanup: Automatically removes old backups to manage storage space
Requirements
- Ansible 2.9 or higher
- Collection:
community.general(for archive module) - NAS mount points must be accessible and writable
- Sufficient storage space on both NAS devices
Role Variables
Required Variables
None - all variables have defaults.
Optional Variables
| Variable | Default | Description |
|---|---|---|
ansible_code_backup_syno_mount_point | /mnt/synology-ds418/gitlab | Mount point for Synology NAS backup destination |
ansible_code_backup_prxmxomv_mount_point | /mnt/prxmx-omv/gitlab | Mount point for Proxmox OMV NAS backup destination |
ansible_code_backup_source_path | /home/bjoffrey/Documents/ansible/homelab/ | Source directory to backup |
ansible_code_backup_nases_path | See defaults/main.yml | List of destination paths with timestamps |
Variable Details
ansible_code_backup_source_path
The root directory of your Ansible repository. Everything in this directory will be archived.
Example:
ansible_code_backup_source_path: /home/user/ansible/
ansible_code_backup_nases_path
Automatically generates timestamped filenames using ansible_facts['date_time'].iso8601_basic_short (format: YYYYMMDDTHHMMSS).
Default behavior:
- Synology:
/mnt/synology-ds418/gitlab/ansible_code_backup_20260107T143022.tar.gz - Proxmox OMV:
/mnt/prxmx-omv/gitlab/ansible_code_backup_20260107T143022.tar.gz
Dependencies
This role has no dependencies on other Ansible roles, but requires:
- NAS mounts to be configured (see
nas_mountornas_mount_systemdroles) - Write permissions to the NAS mount points
Example Playbook
Basic Usage
---
- name: Backup Ansible Code
hosts: localhost
become: true
roles:
- ansible_code_backup
Custom Backup Locations
---
- name: Backup Ansible Code to Custom Locations
hosts: localhost
become: true
vars:
ansible_code_backup_syno_mount_point: /mnt/backup1/ansible
ansible_code_backup_prxmxomv_mount_point: /mnt/backup2/ansible
ansible_code_backup_source_path: /opt/ansible/
roles:
- ansible_code_backup
What This Role Does
- Ensures backup directories exist on both NAS mount points
- Creates compressed tar.gz archive of the entire Ansible repository
- Copies archive to both NAS devices with timestamp in filename
- Finds all existing backups on each NAS
- Removes old backups, keeping only the 5 most recent per NAS
Backup Retention
The role implements automatic retention management:
- Keeps: Last 5 backups per NAS
- Deletes: Backups older than the 5th most recent
- Per-NAS logic: Each NAS independently maintains 5 backups
This ensures you have:
- 5 backups on Synology DS418
- 5 backups on Proxmox OMV
- Total: Up to 10 backup copies across both devices
File Naming Convention
Backup files are named using ISO 8601 basic short format:
ansible_code_backup_YYYYMMDDTHHMMSS.tar.gz
Examples:
ansible_code_backup_20260107T143022.tar.gzansible_code_backup_20260107T190530.tar.gz
This naming scheme:
- Sorts chronologically by default
- Is timezone-agnostic
- Prevents filename conflicts
- Makes it easy to identify backup age
Restoration
To restore from a backup:
# On the backup destination
cd /mnt/synology-ds418/gitlab/
# Extract the desired backup
tar -xzf ansible_code_backup_20260107T143022.tar.gz -C /path/to/restore/
Tags
This role does not define any tags. Use playbook-level tags if needed:
- hosts: localhost
roles:
- ansible_code_backup
tags:
- backup
- ansible
Notes
- The role uses
community.general.archivewhich creates efficient compressed backups - Archive creation is idempotent - will show as changed when a new backup is created
- Cleanup operation uses safe file handling with loop control labels
- Both NAS destinations are processed in parallel where possible
Troubleshooting
”Permission denied” errors
Ensure the user running Ansible has write permissions to both NAS mount points.
”No such file or directory” for mount points
Verify NAS devices are mounted before running this role. Check with mount | grep synology and mount | grep prxmx-omv.
Backups are not being removed
Verify the backup filename pattern matches ansible_code_backup_*.tar.gz. Manual backups with different names won’t be cleaned up.
License
MIT
Author
Created for homelab infrastructure management.