From 7f5275d2407fd2c1475395bf033ec1cad6655719 Mon Sep 17 00:00:00 2001 From: Andrew Williams Date: Sat, 13 Jul 2024 14:33:47 +0100 Subject: [PATCH] Support Debian --- defaults/main.yaml | 1 + meta/main.yaml | 5 ++++- tasks/config.yaml | 5 ++++- tasks/init.yaml | 25 +++++++++++++++++++++---- tasks/install.yaml | 26 ++++++++++++++++---------- tasks/main.yaml | 11 ++++++++--- vars/os/Debian.yaml | 2 ++ vars/os/RedHat.yaml | 2 ++ 8 files changed, 58 insertions(+), 19 deletions(-) create mode 100644 vars/os/Debian.yaml create mode 100644 vars/os/RedHat.yaml diff --git a/defaults/main.yaml b/defaults/main.yaml index 1b89547..2183125 100644 --- a/defaults/main.yaml +++ b/defaults/main.yaml @@ -1,5 +1,6 @@ --- borgmatic_ssh_keys: [] + borgmatic_config: # Where to look for files to backup, and where to store those backups. # See https://borgbackup.readthedocs.io/en/stable/quickstart.html and diff --git a/meta/main.yaml b/meta/main.yaml index b1bc661..31786e0 100644 --- a/meta/main.yaml +++ b/meta/main.yaml @@ -3,7 +3,7 @@ allow_duplicates: false galaxy_info: role_name: borgmatic - author: Andrew Williams + author: nikdoof description: Installs and configures a Borgmatic license: MIT @@ -13,3 +13,6 @@ galaxy_info: - name: EL versions: - 8 + - name: Debian + versions: + - bookworm diff --git a/tasks/config.yaml b/tasks/config.yaml index a72e57c..50598a6 100644 --- a/tasks/config.yaml +++ b/tasks/config.yaml @@ -6,13 +6,15 @@ owner: root group: root mode: "0700" + - name: Write borgmatic config copy: dest: /etc/borgmatic/config.yaml owner: root group: root mode: "0600" - content: "{{ borgmatic_config | to_nice_yaml( width=50, explicit_start=True, explicit_end=True) }}" + content: "{{ borgmatic_config | to_nice_yaml(width=50, explicit_start=True, explicit_end=True) }}" + - name: Create /root/.borgmatic folder ansible.builtin.file: path: /root/.borgmatic @@ -20,6 +22,7 @@ owner: root group: root mode: "0700" + - name: "Copy SSH key {{ item }} to borgmatic config folder" ansible.builtin.copy: src: "ssh_keys/{{ item }}" diff --git a/tasks/init.yaml b/tasks/init.yaml index 47f4a4c..3d2be62 100644 --- a/tasks/init.yaml +++ b/tasks/init.yaml @@ -1,9 +1,26 @@ --- +- name: Get borg target host SSH host key + ansible.builtin.command: "ssh-keyscan -H {{ item.split('@')[1].split(':')[0] }}" + loop: "{{ borgmatic_config['location']['repositories'] }}" + when: + - "'location' in borgmatic_config" + - "'repositories' in borgmatic_config['location']" + register: _borgmatic_ssh_host_keys + +- name: Add SSH key to known hosts + ansible.builtin.known_hosts: + path: /etc/ssh/ssh_known_hosts + name: "{{ item.item.split('@')[1].split(':')[0] }}" + key: "{{ item.stdout }}" + loop: "{{ _borgmatic_ssh_host_keys.results }}" + loop_control: + label: "{{ item.item }}" + - name: Initialize borg check_repositories - ansible.builtin.command: "/usr/local/bin/borgmatic init --encryption repokey" # noqa 301 - no_log: true - async: 300 - poll: 0 + ansible.builtin.shell: "borgmatic init --encryption repokey" + environment: + PATH: "{{ ansible_env.PATH}}:/usr/local/bin" + - name: Enable borgmatic.timer ansible.builtin.systemd: name: borgmatic.timer diff --git a/tasks/install.yaml b/tasks/install.yaml index 4affffe..a42bd16 100644 --- a/tasks/install.yaml +++ b/tasks/install.yaml @@ -1,17 +1,21 @@ --- - name: Install BorgBackup ansible.builtin.package: - name: "{{ packages }}" - state: installed - vars: - packages: - - borgbackup -- name: Install Borgmatic + name: borgbackup + state: present + +- name: Install Borgmatic (Pip) ansible.builtin.pip: - name: "{{ packages }}" - vars: - packages: - - borgmatic + name: borgmatic + when: + - borgmatic_install_from_pip | default(false) + +- name: Install Borgmatic (Package) + ansible.builtin.package: + name: borgmatic + when: + - borgmatic_install_from_package | default(false) + - name: Add borgmatic systemd units ansible.builtin.copy: src: "systemd/{{ item }}" @@ -23,3 +27,5 @@ loop: - borgmatic.service - borgmatic.timer + when: + - borgmatic_install_from_pip | default(false) \ No newline at end of file diff --git a/tasks/main.yaml b/tasks/main.yaml index 776bb90..5688812 100644 --- a/tasks/main.yaml +++ b/tasks/main.yaml @@ -1,4 +1,9 @@ --- -- include: install.yaml -- include: config.yaml -- include: init.yaml +- name: Borgmatic - Get OS specific vars + include_vars: "{{ item }}" + with_first_found: + - "os/{{ ansible_os_family }}/{{ ansible_distribution_major_version }}.yaml" + - "os/{{ ansible_os_family }}.yaml" +- include_tasks: install.yaml +- include_tasks: config.yaml +- include_tasks: init.yaml diff --git a/vars/os/Debian.yaml b/vars/os/Debian.yaml new file mode 100644 index 0000000..3cced6e --- /dev/null +++ b/vars/os/Debian.yaml @@ -0,0 +1,2 @@ +--- +borgmatic_install_from_package: true \ No newline at end of file diff --git a/vars/os/RedHat.yaml b/vars/os/RedHat.yaml new file mode 100644 index 0000000..fd66c38 --- /dev/null +++ b/vars/os/RedHat.yaml @@ -0,0 +1,2 @@ +--- +borgmatic_install_from_pip: true \ No newline at end of file