ansible-role-borg-backup/tasks/noauto_create_timer_systemd.yml
Manu bdee5114a6 fix: correct systemd timer reference and enable timer by default
Fixes #143 and #164.

- Fix incorrect timer reference in borgmatic.service.j2
  (backup_normal_repo.timer -> borgmatic.timer)
- Add borgmatic_timer_enabled variable (default: true) to control
  whether the systemd timer is enabled after installation
- Simplify timer management logic in noauto_create_timer_systemd.yml

Previously, the timer was always created but never enabled, causing
users to think backups were running when they weren't. Now the timer
is enabled by default. Users who need manual control can set
borgmatic_timer_enabled: false.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-06 14:46:21 +00:00

59 lines
1.9 KiB
YAML

---
- name: Register existence of Borgmatic cron file.
cron:
name: "{{ borgmatic_timer_cron_name }}"
cron_file: "{{ borgmatic_timer_cron_name }}"
state: absent
check_mode: true
register: cron_file_exists
ignore_errors: true
- name: Ensure no Borgmatic Cron file exists.
ansible.builtin.assert:
that:
- not cron_file_exists.changed
- not cron_file_exists.failed or "Failed to find" in cron_file_exists.msg
fail_msg: Found an existing Borgmatic Cron job. Please remove before using Systemd timer.
- name: Create borgbackup timer
block:
- name: Copy systemd files
ansible.builtin.template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
owner: root
group: root
backup: true
mode: "{{ item.mode }}"
with_items:
- { src: "borgmatic.timer.j2", dest: "/usr/lib/systemd/system/borgmatic.timer", mode: "0644" }
- { src: "borgmatic.service.j2", dest: "/usr/lib/systemd/system/borgmatic.service", mode: "0644" }
- name: Reload systemd daemon
ansible.builtin.systemd:
daemon_reload: true
- name: Enable and start borgmatic timer
when: borgmatic_timer_enabled | default(true) | bool
ansible.builtin.systemd:
name: borgmatic.timer
state: started
enabled: true
- name: Disable borgmatic timer
when: not (borgmatic_timer_enabled | default(true) | bool)
block:
- name: Stop and disable borgmatic.timer
ansible.builtin.systemd:
name: borgmatic.timer
state: stopped
enabled: false
- name: Show hints
ansible.builtin.debug:
msg: >-
Attention: borgmatic_timer_enabled is set to false.
The systemd timer (borgmatic.timer) is not activated.
Enable it manually with: systemctl enable --now borgmatic.timer
...