ansible-collection-duplicity/roles/duplicity_server/tasks/main.yml

89 lines
3.2 KiB
YAML
Raw Normal View History

---
2024-02-11 16:29:29 +01:00
### BASIC SOFTWARE
- name: "Install required software on servers"
become: true
ansible.builtin.package:
name: "{{ item }}"
state: "present"
with_items:
- "duplicity" # Obviously needed for backup
- "sshfs" # TO be able to mount the remote directory
- "python3-packaging" # ??? For something
- "acl" # To manage the access control on the backup data
2024-02-11 16:29:29 +01:00
### SSH ACCESS (USER/KEYS)
- name: "Create backup user on servers"
become: true
ansible.builtin.user:
name: "{{ duplicity_server_user }}"
2024-02-11 16:29:29 +01:00
generate_ssh_key: true # We want to generate an ssh key to be able to configure the access on the clients later
ssh_key_type: "ed25519"
ssh_key_comment: "{{ duplicity_server_user }}@{{ ansible_hostname }} (generated by ansible)"
register: "duplicity_server_created_user" # Store the return value for the ssh key and home path
2024-02-11 16:29:29 +01:00
- name: "Store server ssh key as fact for later usage"
ansible.builtin.set_fact:
duplicity_server_user_key: "{{ duplicity_server_created_user['ssh_public_key'] }}"
- name: "Fetch sshd fingerprints from clients"
ansible.builtin.slurp:
2024-02-11 16:29:29 +01:00
src: "/etc/ssh/ssh_host_ecdsa_key.pub"
delegate_to: "{{ item }}"
with_items: "{{ groups['duplicityclient'] | flatten(levels=1) }}"
changed_when: false
2024-02-11 16:29:29 +01:00
register: "duplicity_client_host_key"
2024-02-11 16:29:29 +01:00
- name: "Register client host keys in server"
become: true
become_user: "{{ duplicity_server_user }}"
ansible.builtin.known_hosts:
name: "{{ item.item }}"
key: "{{ item.item }} {{ item.content | b64decode }}"
with_items: "{{ duplicity_client_host_key.results }}"
2024-02-11 16:29:29 +01:00
### GNUPG ENCRYPTION
- name: "Ensure gnupg config dir"
become: true
become_user: "{{ duplicity_server_user }}"
ansible.builtin.command:
cmd: "gpg --list-keys"
creates: "{{ duplicity_server_created_user['home'] }}/.gnupg"
2024-02-11 16:29:29 +01:00
- name: "Install encryption key for backups"
become: true
become_user: "{{ duplicity_server_user }}"
de_enbewe.duplicity.gpg_key:
fpr: "{{ duplicity_server_gnupg_fingerprint }}"
keyserver: "hkps://keys.openpgp.org"
trust: 5
homedir: "{{ duplicity_server_created_user['home'] }}/.gnupg"
2024-02-11 16:29:29 +01:00
### BACKUP SCRIPTS
- name: "Create backup script path"
become: true
ansible.builtin.file:
path: "{{ duplicity_server_created_user['home'] }}/scripts"
state: "directory"
owner: "{{ duplicity_server_user }}"
group: "{{ duplicity_server_user }}"
mode: "u=rwx,g=rx,o=rx"
2024-02-11 16:29:29 +01:00
- name: "Create backup scripts for clients"
become: true
become_user: "{{ duplicity_server_user }}"
ansible.builtin.template:
src: "backup-script.j2"
dest: "{{ duplicity_server_created_user['home'] }}/scripts/backup-{{ item }}.sh"
mode: "u=rwx,g=rx,o=rx"
with_items: "{{ groups['duplicityclient'] }}"
2024-02-11 16:29:29 +01:00
- name: "Register cronjob for clients"
become: true
ansible.builtin.cron:
name: "backup-{{ item }}"
user: "{{ duplicity_server_user }}"
job: "{{ duplicity_server_created_user['home'] }}/scripts/backup-{{ item }}.sh"
minute: "{{ hostvars[item].duplicity_client_backup_minute | default(12) }}"
hour: "{{ hostvars[item].duplicity_client_backup_hour | default(1) }}"
with_items: "{{ groups['duplicityclient'] }}"