4 Commits
Author SHA1 Message Date
alessandrovitali 617883a9a7 style: rewrite comments 2026-07-12 23:56:06 +02:00
alessandrovitali 71a02e0f86 refactor: harden restic configuration
move secrets to .env file so they work on their own, initialize restic repo on fresh deployments
2026-07-12 23:46:29 +02:00
alessandrovitali c4c8d6de7f feat: make backup role universal by passing paths as variables 2026-07-09 16:42:10 +02:00
alessandrovitali 8d96529d8f feat: add backup role using restic 2026-07-09 15:51:45 +02:00
14 changed files with 228 additions and 146 deletions
+1 -3
View File
@@ -1,5 +1,3 @@
# Ansible Collection
Opinionated set of roles to configure reasonable defaults for newly provisioned virtual machines. The repository provides a ready-to-use Ansible Collection (`studio.ansible`) containing reusable roles.
Currently only supports Debian-based systems.
Opinionated set of roles to configure reasonable defaults for newly provisioned Debian / Ubuntu VMs running Docker. The repository provides a ready-to-use Ansible Collection (`studio.ansible`) containing reusable roles.
+2 -3
View File
@@ -3,9 +3,8 @@
namespace: studio
name: ansible
readme: README.md
version: 0.0.1
version: 1.0.0
authors:
- Studio Vitali
description: Studio Ansible collection containing common roles
license:
- MIT
license: MIT
-2
View File
@@ -1,2 +0,0 @@
---
requires_ansible: ">=2.15.0"
+14
View File
@@ -0,0 +1,14 @@
# ansible/roles/backup/defaults/main.yml
---
## Directories to include in the backup
backup_include_paths:
- /etc
- /opt/docker
## Directories to exclude from the backup
backup_exclude_paths: [] ## Nothing to exclude
## Restic retention policy
backup_retention_daily: 7
backup_retention_weekly: 4
backup_retention_monthly: 6
+61
View File
@@ -0,0 +1,61 @@
---
- name: Install restic
ansible.builtin.apt:
name: restic
state: present
- name: Initialize restic repository
environment:
RESTIC_REPOSITORY: "{{ vault_restic_repo_url }}"
RESTIC_PASSWORD: "{{ vault_restic_password }}"
AWS_ACCESS_KEY_ID: "{{ vault_aws_access_key }}"
AWS_SECRET_ACCESS_KEY: "{{ vault_aws_secret_key }}"
block:
- name: Check if already initialized
ansible.builtin.command: restic snapshots
register: restic_check
failed_when: false
changed_when: false
- name: Initialize repository
ansible.builtin.command: restic init
when: restic_check.rc != 0
- name: Deploy restic environment file
ansible.builtin.template:
src: restic-backup.env.j2
dest: /etc/restic-backup.env
owner: root
group: root
mode: "0600" ## only root can read this file
- name: Deploy restic backup script
ansible.builtin.template:
src: restic-backup.sh.j2
dest: /usr/local/bin/restic-backup.sh
owner: root
group: root
mode: "0700" ## only root can execute the script
- name: Deploy restic systemd service
ansible.builtin.template:
src: restic-backup.service.j2
dest: /etc/systemd/system/restic-backup.service
owner: root
group: root
mode: "0644"
- name: Deploy restic systemd timer
ansible.builtin.template:
src: restic-backup.timer.j2
dest: /etc/systemd/system/restic-backup.timer
owner: root
group: root
mode: "0644"
- name: Enable and start backup timer
ansible.builtin.systemd:
name: restic-backup.timer
enabled: yes
state: started
daemon_reload: yes
@@ -0,0 +1,5 @@
# Ansible managed
RESTIC_REPOSITORY="{{ vault_restic_repo_url }}"
RESTIC_PASSWORD="{{ vault_restic_password }}"
AWS_ACCESS_KEY_ID="{{ vault_aws_access_key }}"
AWS_SECRET_ACCESS_KEY="{{ vault_aws_secret_key }}"
@@ -0,0 +1,8 @@
[Unit]
Description=Run Restic Backup
After=network-online.target
[Service]
Type=oneshot
EnvironmentFile=/etc/restic-backup.env
ExecStart=/usr/local/bin/restic-backup.sh
@@ -0,0 +1,30 @@
#!/bin/bash
## Ansible managed
set -e
# Source environment variables if running manually/outside Systemd
if [ -f /etc/restic-backup.env ]; then
# shellcheck source=/dev/null
. /etc/restic-backup.env
fi
echo "Starting Restic Backup at $(date)"
## Run the backup using paths and exclusions defined as group_vars
restic backup \
{% for path in backup_include_paths %}
"{{ path }}" \
{% endfor %}
{% for path in backup_exclude_paths %}
--exclude "{{ path }}" \
{% endfor %}
--cleanup-cache
restic forget \
--prune \
--keep-daily {{ backup_retention_daily }} \
--keep-weekly {{ backup_retention_weekly }} \
--keep-monthly {{ backup_retention_monthly }}
echo "Restic backup completed at $(date)"
@@ -0,0 +1,10 @@
[Unit]
Description=Run Restic Backup
[Timer]
OnCalendar=*-*-* 04:00:00
Persistent=true
RandomizedDelaySec=1800
[Install]
WantedBy=timers.target
+4
View File
@@ -10,6 +10,7 @@
update_cache: true
upgrade: dist
cache_valid_time: 3600
when: ansible_facts['os_family'] == "Debian" ## only on Debian / Ubuntu systems
become: true
- name: Install essential system utilities
@@ -25,6 +26,7 @@
- unattended-upgrades
state: present
update_cache: true
when: ansible_facts['os_family'] == "Debian"
become: true
#
# - name: Enable unattended-upgrades
@@ -35,3 +37,5 @@
# APT::Periodic::Unattended-Upgrade "1";
# mode: "0644"
# when: ansible_facts['os_family'] == "Debian"
## SCHEDULE REBOOTS 4 kernel upgrades: with cron, unattended-upgrades or action workflows?
+15 -56
View File
@@ -1,52 +1,29 @@
---
- name: Install Docker
- name: Install necessary Docker packages
ansible.builtin.apt:
name:
- docker.io
- docker-compose
state: present
when: ansible_facts['os_family'] == "Debian" ## Debian / Ubuntu
become: true
block:
- name: Install Docker prerequisites
ansible.builtin.apt:
name:
- ca-certificates
- curl
- python3-debian ## needed for the deb822 module
state: present
update_cache: true
- name: Add Docker official APT repository (DEB822 format)
ansible.builtin.deb822_repository:
name: docker
types: deb
uris: "https://download.docker.com/linux/{{ ansible_facts['distribution'] | lower }}"
suites: "{{ ansible_facts['distribution_release'] }}"
components: stable
signed_by: "https://download.docker.com/linux/{{ ansible_facts['distribution'] | lower }}/gpg"
state: present
- name: Install Docker CE packages
ansible.builtin.apt:
name:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-buildx-plugin
- docker-compose-plugin
state: present
update_cache: true
- name: Ensure Docker service is running and enabled
- name: Ensure Docker service is running
ansible.builtin.service:
name: docker
state: started
enabled: true
- name: Configure deployment user for Docker
when: deployment_public_ssh_key is defined
become: true
block:
- name: Add deployment user to docker group
- name: Create docker user for container execution
ansible.builtin.user:
name: deployment
name: docker
state: present
system: true
shell: /usr/sbin/nologin
groups: docker
append: true
create_home: false
become: true
- name: Create target for docker compose
ansible.builtin.file:
@@ -55,22 +32,4 @@
owner: deployment
group: deployment
mode: "0755"
- name: Create app user for secure container execution
become: true
block:
- name: Create appuser group
ansible.builtin.group:
name: appuser
state: present
gid: 9999
- name: Create appuser
ansible.builtin.user:
name: appuser
state: present
uid: 9999
group: appuser
system: true
shell: /usr/sbin/nologin
create_home: false
+1 -1
View File
@@ -1,5 +1,5 @@
---
- name: Restart sshd
ansible.builtin.service:
name: ssh
name: sshd
state: restarted
+6 -6
View File
@@ -1,7 +1,4 @@
---
- name: Harden SSH configuration
become: true
block:
- name: Create SSH drop-in directory
ansible.builtin.file:
path: /etc/ssh/sshd_config.d
@@ -9,6 +6,7 @@
owner: root
group: root
mode: "0755"
become: true
- name: Harden SSH configuration with drop-in template
ansible.builtin.template:
@@ -17,19 +15,21 @@
owner: root
group: root
mode: "0644"
validate: "sshd -t -f %s"
notify: Restart sshd
- name: Enable fail2ban
become: true
block:
- name: Install fail2ban
ansible.builtin.apt:
name:
- fail2ban
state: present
when: ansible_facts['os_family'] == "Debian"
become: true
- name: Start fail2ban
ansible.builtin.service:
name: fail2ban
state: started
enabled: yes
when: ansible_facts['os_family'] == "Debian"
+25 -29
View File
@@ -1,42 +1,38 @@
---
- name: Set up primary sysadmin user
when: sysadmin_public_ssh_key is defined
become: true
block:
- name: Create sysadmin user
ansible.builtin.user:
name: "{{ sysadmin_user }}"
groups: sudo
append: yes
shell: /bin/bash
create_home: yes
# - name: Create primary admin user
# ansible.builtin.user:
# name: sysadmin
# groups: sudo
# append: yes
# shell: /bin/bash
# create_home: yes
# become: true
- name: Add SSH public key
ansible.posix.authorized_key:
user: "{{ sysadmin_user }}"
state: present
key: "{{ sysadmin_public_ssh_key }}"
# - name: Add SSH public key for sysadmin
# ansible.posix.authorized_key:
# user: sysadmin
# state: present
# key: "{{ sysadmin_public_ssh_key }}"
# become: true
- name: Allow passwordless sudo
ansible.builtin.copy:
dest: /etc/sudoers.d/sudo-nopasswd
content: "%sudo ALL=(ALL) NOPASSWD: ALL"
mode: "0440"
validate: /usr/sbin/visudo -cf %s
# - name: Allow passwordless sudo to sysadmin user
# ansible.builtin.copy:
# dest: /etc/sudoers.d/sysadmin
# content: "sysadmin ALL=(ALL) NOPASSWD: ALL"
# mode: "0440"
# validate: /usr/sbin/visudo -cf %s
# become: true
- name: Set up deployment user for CI/CD
when: deployment_public_ssh_key is defined
become: true
block:
- name: Create deployment user
- name: Create deployment user for CI/CD
ansible.builtin.user:
name: deployment
shell: /bin/bash
create_home: yes
become: true
- name: Add SSH public key
- name: Add SSH public key for deployment user
ansible.posix.authorized_key:
user: deployment
state: present
key: "{{ deployment_public_ssh_key }}"
become: true