63 lines
2.1 KiB
YAML
63 lines
2.1 KiB
YAML
---
|
|
- name: Backup Protocol
|
|
hosts: all
|
|
become: true
|
|
become_method: sudo
|
|
collections:
|
|
- community.docker
|
|
- community.general
|
|
vars:
|
|
env_backups_to_keep: 10
|
|
tasks:
|
|
- name: Run container mounts backup
|
|
ansible.builtin.import_role:
|
|
name: docker_backup
|
|
vars:
|
|
backup_rules: "{{ item }}"
|
|
when: docker_backup is defined and docker_backup | length != 0
|
|
loop: "{{ docker_backup }}"
|
|
|
|
- name: Stat the /root/infrastructure-compose folder
|
|
ansible.builtin.stat:
|
|
path: "/root/infrastructure-compose"
|
|
register: infra_compose_stat
|
|
|
|
- name: Find all .env files
|
|
ansible.builtin.find:
|
|
paths: "/root/infrastructure-compose"
|
|
patterns: ".*.env"
|
|
when: infra_compose_stat.stat.exists
|
|
register: all_env_files
|
|
|
|
- name: .env Backup block
|
|
when: infra_compose_stat.stat.exists and all_env_files.files is defined and all_env_files.files | length != 0
|
|
block:
|
|
- name: Archive .env files
|
|
community.general.archive:
|
|
path: "{{ all_env_files.files }}"
|
|
dest: >-
|
|
/backup/env/{{ inventory_hostname }}_{{ now().strftime("%Y%m%d%H%M%S") }}.tar.gz
|
|
format: gz
|
|
force_archive: true
|
|
|
|
- name: Find all .env backup files for the current host
|
|
ansible.builtin.find:
|
|
paths: "/backup/env"
|
|
patterns: "{{ inventory_hostname }}*"
|
|
register: backup_env_files
|
|
|
|
- name: If too many backups kept
|
|
when: backup_env_files.files | length > env_backups_to_keep
|
|
block:
|
|
- name: Get the oldest file paths
|
|
ansible.builtin.set_fact:
|
|
oldest_file_paths: >-
|
|
{{ (backup_env_files.files | sort(attribute='mtime'))[:backup_env_files.files | length - env_backups_to_keep] |
|
|
map(attribute=path) | list }}
|
|
|
|
- name: Remove the files
|
|
ansible.builtin.file:
|
|
path: "{{ item }}"
|
|
state: absent
|
|
loop: "{{ oldest_file_paths }}"
|