feat(restic): migrate to systemd template units
This commit is contained in:
35
roles/restic/tasks/check.yml
Normal file
35
roles/restic/tasks/check.yml
Normal file
@ -0,0 +1,35 @@
|
||||
---
|
||||
- name: Check if 'restic_state' is valid
|
||||
ansible.builtin.fail:
|
||||
msg: >-2
|
||||
Unknown value '{{ restic_state }}' for 'restic_state'!
|
||||
Supported values are {{ restic_states | join(', ') }}
|
||||
when: restic_state not in restic_states
|
||||
|
||||
- name: Ensure 'restic_job_name' is properly populated
|
||||
ansible.builtin.fail:
|
||||
msg: >-2
|
||||
Unsupported restic_job_name '{{ restic_job_name | string }}'!
|
||||
when:
|
||||
- not (restic_job_name | string | length > 0)
|
||||
|
||||
- name: Ensure either backup_paths or backup_stdin_command is populated
|
||||
ansible.builtin.fail:
|
||||
msg: >-2
|
||||
Setting both `restic_backup_paths` and `restic_backup_stdin_command`
|
||||
is not supported!
|
||||
when: restic_backup_paths|length > 0 and restic_backup_stdin_command and false
|
||||
|
||||
- name: Ensure a filename for stdin_command backup is given
|
||||
ansible.builtin.fail:
|
||||
msg: >-2
|
||||
`restic_backup_stdin_command` was set but no filename for the resulting
|
||||
output was supplied in `restic_backup_stdin_command_filename`.
|
||||
when: restic_backup_stdin_command and not restic_backup_stdin_command_filename
|
||||
|
||||
- name: Ensure backup frequency adheres to systemd's OnCalender syntax
|
||||
command:
|
||||
cmd: "systemd-analyze calendar {{ restic_policy.frequency }}"
|
||||
register: systemd_calender_parse_res
|
||||
failed_when: systemd_calender_parse_res.rc != 0
|
||||
changed_when: false
|
30
roles/restic/tasks/configure.yml
Normal file
30
roles/restic/tasks/configure.yml
Normal file
@ -0,0 +1,30 @@
|
||||
---
|
||||
- name: Ensure systemd timer file for '{{ restic_job_name }}' is {{ restic_state }}'
|
||||
ansible.builtin.template:
|
||||
dest: "/etc/systemd/system/{{ restic_systemd_unit_naming_scheme }}.timer"
|
||||
src: restic.timer.j2
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0640"
|
||||
when: restic_state == 'present'
|
||||
register: restic_systemd_timer_info
|
||||
notify:
|
||||
- reload-systemd
|
||||
|
||||
- name: Ensure restic configuration for '{{ restic_job_name }}' is {{ restic_job_state }}
|
||||
ansible.builtin.template:
|
||||
src: "restic.conf.j2"
|
||||
dest: "{{ restic_job_directory }}/{{ restic_job_name }}.conf"
|
||||
mode: "0640"
|
||||
when: restic_job_state in ['present', 'masked']
|
||||
notify:
|
||||
- trigger-restic
|
||||
|
||||
- name: Ensure restic configuration for '{{ restic_job_name }}' is {{ restic_job_state }}
|
||||
ansible.builtin.file:
|
||||
path: "{{ restic_job_directory }}/{{ restic_job_name }}.conf"
|
||||
state: "{{ restic_job_state }}"
|
||||
when: restic_job_state not in ['present', 'masked']
|
||||
|
||||
- name: Flush handlers to ensure systemd knows about '{{ restic_job_name }}'
|
||||
meta: flush_handlers
|
48
roles/restic/tasks/install.yml
Normal file
48
roles/restic/tasks/install.yml
Normal file
@ -0,0 +1,48 @@
|
||||
---
|
||||
- name: Ensure restic is installed
|
||||
block:
|
||||
- name: Ensure restic is installed via apt
|
||||
apt:
|
||||
package: restic
|
||||
state: latest
|
||||
when: ansible_os_family == 'Debian'
|
||||
- name: Ensure restic is installed via dnf
|
||||
dnf:
|
||||
name: restic
|
||||
state: latest
|
||||
when: ansible_os_family == 'RedHat'
|
||||
- name: Ensure restic is installed using the auto-detected package-manager
|
||||
package:
|
||||
name: "{{ restic_package_name }}"
|
||||
state: present
|
||||
when: ansible_os_family not in ['RedHat', 'Debian']
|
||||
|
||||
- name: Ensure restic backup scripts are {{ restic_state }}
|
||||
ansible.builtin.copy:
|
||||
src: "{{ script.source }}"
|
||||
dest: "{{ script.destination }}"
|
||||
mode: "{{ script.mode }}"
|
||||
loop:
|
||||
- source: restic-backup.sh
|
||||
destination: "{{ restic_systemd_service_exec_start }}"
|
||||
mode: "0510"
|
||||
- source: restic-snapshot-metrics.sh
|
||||
destination: "{{ restic_script_generate_snapshot_metrics }}"
|
||||
mode: "0510"
|
||||
loop_control:
|
||||
loop_var: script
|
||||
label: "{{ script.source }}"
|
||||
|
||||
- name: Ensure systemd service file for restic template unit is {{ restic_state }}
|
||||
ansible.builtin.template:
|
||||
dest: "/etc/systemd/system/restic@.service"
|
||||
src: "restic@.service.j2"
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0640"
|
||||
when: restic_state == 'present'
|
||||
notify:
|
||||
- reload-systemd
|
||||
- trigger-restic
|
||||
|
||||
|
@ -1,77 +1,39 @@
|
||||
---
|
||||
- name: Check if role input is valid
|
||||
ansible.builtin.include_tasks:
|
||||
file: check.yml
|
||||
|
||||
- name: Ensure {{ restic_user }} system user exists
|
||||
user:
|
||||
- name: Ensure restic is {{ restic_state }}
|
||||
ansible.builtin.include_tasks:
|
||||
file: install.yml
|
||||
|
||||
- name: Ensure restic user '{{ restic_user }}' is {{ restic_state }}
|
||||
ansible.builtin.user:
|
||||
name: "{{ restic_user }}"
|
||||
state: present
|
||||
system: true
|
||||
state: "{{ restic_state }}"
|
||||
system: "{{ restic_user_system }}"
|
||||
create_home: "{{ restic_user_create_home }}"
|
||||
when: restic_create_user
|
||||
|
||||
- name: Ensure either backup_paths or backup_stdin_command is populated
|
||||
when: restic_backup_paths|length > 0 and restic_backup_stdin_command and false
|
||||
fail:
|
||||
msg: "Setting both `restic_backup_paths` and `restic_backup_stdin_command` is not supported"
|
||||
|
||||
- name: Ensure a filename for stdin_command backup is given
|
||||
when: restic_backup_stdin_command and not restic_backup_stdin_command_filename
|
||||
fail:
|
||||
msg: "`restic_backup_stdin_command` was set but no filename for the resulting output was supplied in `restic_backup_stdin_command_filename`"
|
||||
|
||||
- name: Ensure backup frequency adheres to systemd's OnCalender syntax
|
||||
command:
|
||||
cmd: "systemd-analyze calendar {{ restic_policy.frequency }}"
|
||||
register: systemd_calender_parse_res
|
||||
failed_when: systemd_calender_parse_res.rc != 0
|
||||
changed_when: false
|
||||
|
||||
- name: Ensure restic is installed
|
||||
block:
|
||||
- name: Ensure restic is installed via apt
|
||||
apt:
|
||||
package: restic
|
||||
state: latest
|
||||
when: ansible_os_family == 'Debian'
|
||||
- name: Ensure restic is installed via dnf
|
||||
dnf:
|
||||
name: restic
|
||||
state: latest
|
||||
when: ansible_os_family == 'RedHat'
|
||||
- name: Ensure restic is installed using the auto-detected package-manager
|
||||
package:
|
||||
name: "{{ restic_package_name }}"
|
||||
state: present
|
||||
when: ansible_os_family not in ['RedHat', 'Debian']
|
||||
|
||||
- name: Ensure systemd service file for '{{ restic_job_name }}' is templated
|
||||
template:
|
||||
dest: "/etc/systemd/system/{{ restic_systemd_unit_naming_scheme }}.service"
|
||||
src: restic.service.j2
|
||||
owner: root
|
||||
group: root
|
||||
mode: 0640
|
||||
notify:
|
||||
- reload-systemd
|
||||
- trigger-restic
|
||||
|
||||
- name: Ensure systemd service file for '{{ restic_job_name }}' is templated
|
||||
template:
|
||||
dest: "/etc/systemd/system/{{ restic_systemd_unit_naming_scheme }}.timer"
|
||||
src: restic.timer.j2
|
||||
owner: root
|
||||
group: root
|
||||
mode: 0640
|
||||
notify:
|
||||
- reload-systemd
|
||||
|
||||
- name: Flush handlers to ensure systemd knows about '{{ restic_job_name }}'
|
||||
meta: flush_handlers
|
||||
- name: Ensure restic configuration for job is {{ restic_job_state }}
|
||||
ansible.builtin.include_tasks:
|
||||
file: "configure.yml"
|
||||
|
||||
- name: Ensure systemd timer for '{{ restic_job_name }}' is activated
|
||||
systemd:
|
||||
name: "{{ restic_systemd_unit_naming_scheme }}.timer"
|
||||
ansible.builtin.systemd:
|
||||
name: "{{ restic_systemd_timer_naming_scheme }}"
|
||||
enabled: true
|
||||
when:
|
||||
- restic_systemd_timer_info.changed
|
||||
- not restic_systemd_timer_info.failed
|
||||
- not ansible_check_mode
|
||||
|
||||
- name: Ensure systemd timer for '{{ restic_job_name }}' is started
|
||||
systemd:
|
||||
name: "{{ restic_systemd_unit_naming_scheme }}.timer"
|
||||
state: started
|
||||
- name: Ensure systemd timer for '{{ restic_job_name }}' is {{ restic_job_state }}
|
||||
ansible.builtin.systemd:
|
||||
name: "{{ restic_systemd_timer_naming_scheme }}"
|
||||
state: "{{ restic_job_state }}"
|
||||
masked: "{{ (restic_job_state == 'masked') | ternary('true', omit) }}"
|
||||
when:
|
||||
- restic_systemd_timer_info.changed
|
||||
- not restic_systemd_timer_info.failed
|
||||
- not ansible_check_mode
|
||||
|
Reference in New Issue
Block a user