start making this role compatible with ansible 5

This commit is contained in:
L3D 2022-01-07 20:13:43 +01:00
parent e614bdc661
commit 25ee60fb5a
No known key found for this signature in database
GPG key ID: CD08445BFF4313D1
6 changed files with 206 additions and 137 deletions

View file

@ -22,6 +22,7 @@ restic_systemd_timer_default_OnCalendar: '*-*-* 02:00:00'
# perform simple version check for this role? (true is recomended)
submodules_versioncheck: false
restic_do_not_cleanup_cron: false
# outdated variables because of irritating names, but kept for compatibility
restic_create_cron: false

View file

@ -0,0 +1,48 @@
---
- name: "check if ansible version is under 2.12.0"
ansible.builtin.assert:
that:
- "ansible_version.full is version_compare('2.12.0', '<')"
fail_msg: "[ERROR] Youre ansible version is above 2.12.0"
success_msg: "Congratulations. You are using ansible version {{ ansible_version.full }}"
delegate_to: localhost
- name: try to remove entries from /etc/crontab
ansible.builtin.cron:
name: "do1jlr.restic backup {{ item.name }}"
job: "CRON=true {{ restic_script_dir }}/backup-{{ item.name | replace(' ', '') }}.sh"
minute: '{{ item.schedule_minute | default("*") }}'
hour: '{{ item.schedule_hour | default("2") }}'
weekday: '{{ item.schedule_weekday | default("*") }}'
month: '{{ item.schedule_month | default("*") }}'
state: absent
cron_file: '/etc/crontab'
user: 'root'
become: true
no_log: "{{ restic_no_log }}"
with_items: '{{ restic_backups }}'
when:
- restic_create_schedule | bool
- item.name is defined
- item.scheduled | default(false)
- ansible_service_mgr != 'systemd' or restic_force_cron | default(false) or restic_schedule_type == "cronjob"
ignore_error: true
register: cron_delete
- name: "make sure 'do1jlr.restic backup {{ item.name }}' is not in /etc/crontab"
become: true
ansible.builtin.lineinfile:
path: '/etc/crontab'
state: absent
search_string: "do1jlr.restic backup {{ item.name }}"
when: cron_delete.failed
with_items: '{{ restic_backups }}'
- name: "make sure '{{ restic_script_dir }}/backup-{{ item.name | replace(' ', '') }}.sh' is not in /etc/crontab"
become: true
ansible.builtin.lineinfile:
path: '/etc/crontab'
state: absent
search_string: "{{ restic_script_dir }}/backup-{{ item.name | replace(' ', '') }}.sh"
when: cron_delete.failed
with_items: '{{ restic_backups }}'

View file

@ -0,0 +1,18 @@
---
- name: install restic backup cronjob
ansible.builtin.cron:
name: "do1jlr.restic backup {{ item.name }}"
job: "CRON=true {{ restic_script_dir }}/backup-{{ item.name | replace(' ', '') }}.sh"
minute: '{{ item.schedule_minute | default("*") }}'
hour: '{{ item.schedule_hour | default("2") }}'
weekday: '{{ item.schedule_weekday | default("*") }}'
month: '{{ item.schedule_month | default("*") }}'
state: present
user: 'root'
cron_file: restic_backup
become: true
no_log: "{{ restic_no_log }}"
with_items: '{{ restic_backups }}'
when:
- item.name is defined
- item.scheduled | default(false)

View file

@ -0,0 +1,66 @@
---
- name: create systemd timer
block:
- name: copy systemd timer
become: true
ansible.builtin.template:
src: templates/restic.timer.j2
dest: "/lib/systemd/system/restic-{{ item.name | replace(' ', '') }}.timer"
owner: 'root'
group: 'root'
mode: '0644'
no_log: "{{ restic_no_log }}"
with_items: '{{ restic_backups }}'
notify: systemctl restart restic.timer
when:
- item.name is defined
- item.scheduled | default(false)
- name: copy systemd service
become: true
ansible.builtin.template:
src: templates/restic.service.j2
dest: "/lib/systemd/system/restic-{{ item.name | replace(' ', '') }}.service"
owner: 'root'
group: 'root'
mode: '0644'
no_log: "{{ restic_no_log }}"
with_items: '{{ restic_backups }}'
when:
- item.name is defined
- item.scheduled | default(false)
- name: Enable restic service
become: true
ansible.builtin.systemd:
name: "restic-{{ item.name | replace(' ', '') | string }}.service"
enabled: true
daemon_reload: true
masked: false
with_items: '{{ restic_backups }}'
notify: systemctl restart restic.timer
when:
- item.name is defined
- item.scheduled | default(false)
- name: Enable and start restic timer
become: true
ansible.builtin.systemd:
name: "restic-{{ item.name | replace(' ', '') | string }}.timer"
enabled: true
state: started
daemon_reload: true
masked: false
with_items: '{{ restic_backups }}'
notify: systemctl restart restic.timer
when:
- item.name is defined
- item.scheduled | default(false)
when:
- ansible_service_mgr == 'systemd'
- restic_schedule_type == "systemd"
- restic_create_schedule | bool
rescue:
- name: set cronjob intead of systemd
set_fact:
restic_force_cron: true

View file

@ -0,0 +1,58 @@
---
- name: remove systemd timer
block:
- name: mask restic timer
become: true
ansible.builtin.systemd:
name: "restic-{{ item.name | replace(' ', '') | string }}.timer"
enabled: false
state: stopped
daemon_reload: true
masked: false
with_items: '{{ restic_backups }}'
ignore_errors: true
failed_when: false
changed_when: false
when:
- item.name is defined
- item.scheduled | default(false)
- restic_schedule_type == "cronjob" or restic_force_cron | default(false)
- name: mask restic service
become: true
ansible.builtin.systemd:
name: "restic-{{ item.name | replace(' ', '') | string }}.service"
enabled: false
state: stopped
daemon_reload: true
masked: true
with_items: '{{ restic_backups }}'
when:
- item.name is defined
- item.scheduled | default(false)
- restic_schedule_type == "cronjob" or restic_force_cron | default(false)
ignore_errors: true
failed_when: false
changed_when: false
- name: delete systemd .timer file
become: true
ansible.builtin.file:
path: "/lib/systemd/system/restic-{{ item.name | replace(' ', '') }}.timer"
state: absent
with_items: '{{ restic_backups }}'
when:
- item.name is defined
- item.scheduled | default(false)
- restic_schedule_type == "cronjob" or restic_force_cron | default(false)
- name: delete systemd .service file
become: true
ansible.builtin.file:
path: "/lib/systemd/system/restic-{{ item.name | replace(' ', '') }}.service"
state: absent
with_items: '{{ restic_backups }}'
when:
- item.name is defined
- item.scheduled | default(false)
- restic_schedule_type == "cronjob" or restic_force_cron | default(false)

View file

@ -1,146 +1,24 @@
---
- name: create systemd timer
block:
- name: copy systemd timer
become: true
ansible.builtin.template:
src: templates/restic.timer.j2
dest: "/lib/systemd/system/restic-{{ item.name | replace(' ', '') }}.timer"
owner: 'root'
group: 'root'
mode: '0644'
no_log: "{{ restic_no_log }}"
with_items: '{{ restic_backups }}'
notify: systemctl restart restic.timer
when:
- item.name is defined
- item.scheduled | default(false)
- name: copy systemd service
become: true
ansible.builtin.template:
src: templates/restic.service.j2
dest: "/lib/systemd/system/restic-{{ item.name | replace(' ', '') }}.service"
owner: 'root'
group: 'root'
mode: '0644'
no_log: "{{ restic_no_log }}"
with_items: '{{ restic_backups }}'
when:
- item.name is defined
- item.scheduled | default(false)
- name: copy systemd service
become: true
ansible.builtin.template:
src: templates/restic.service.j2
dest: "/lib/systemd/system/restic-{{ item.name | replace(' ', '') }}.service"
owner: 'root'
group: 'root'
mode: '0644'
no_log: "{{ restic_no_log }}"
with_items: '{{ restic_backups }}'
when:
- item.name is defined
- item.scheduled | default(false)
- name: Enable restic service
become: true
ansible.builtin.systemd:
name: "restic-{{ item.name | replace(' ', '') | string }}.service"
enabled: true
daemon_reload: true
masked: false
with_items: '{{ restic_backups }}'
notify: systemctl restart restic.timer
when:
- item.name is defined
- item.scheduled | default(false)
- name: Enable and start restic timer
become: true
ansible.builtin.systemd:
name: "restic-{{ item.name | replace(' ', '') | string }}.timer"
enabled: true
state: started
daemon_reload: true
masked: false
with_items: '{{ restic_backups }}'
notify: systemctl restart restic.timer
when:
- item.name is defined
- item.scheduled | default(false)
- name: delete old cronjob entry if available
ansible.builtin.cron:
name: "do1jlr.restic backup {{ item.name }}"
job: "CRON=true {{ restic_script_dir }}/backup-{{ item.name | replace(' ', '') }}.sh"
minute: '{{ item.schedule_minute | default("*") }}'
hour: '{{ item.schedule_hour | default("2") }}'
weekday: '{{ item.schedule_weekday | default("*") }}'
month: '{{ item.schedule_month | default("*") }}'
state: absent
cron_file: '/etc/crontab'
user: 'root'
become: true
no_log: "{{ restic_no_log }}"
with_items: '{{ restic_backups }}'
when:
- item.name is defined
- item.scheduled | default(false)
- name: create restic systemd timer
ansible.builtin.include_tasks: restic_create_systemd.yml
when:
- ansible_service_mgr == 'systemd'
- restic_schedule_type == "systemd"
- restic_create_schedule | bool
rescue:
- name: set cronjob intead of systemd
set_fact:
restic_force_cron: true
- name: install cronjob instead of systemd
ansible.builtin.cron:
name: "do1jlr.restic backup {{ item.name }}"
job: "CRON=true {{ restic_script_dir }}/backup-{{ item.name | replace(' ', '') }}.sh"
minute: '{{ item.schedule_minute | default("*") }}'
hour: '{{ item.schedule_hour | default("2") }}'
weekday: '{{ item.schedule_weekday | default("*") }}'
month: '{{ item.schedule_month | default("*") }}'
state: present
cron_file: '/etc/crontab'
user: 'root'
become: true
no_log: "{{ restic_no_log }}"
with_items: '{{ restic_backups }}'
- name: delete systemd timers if available
ansible.builtin.include_tasks: restic_delete_systemd.yml
when:
- ansible_service_mgr == 'systemd'
- restic_schedule_type == "cronjob" or restic_force_cron | default(false)
- restic_create_schedule | bool
- name: delete old cron entry from previous versions of this role
ansible.builtin.include_tasks: delete_legacy_cron_entry.yml
when: restic_do_not_cleanup_cron | bool
- name: install restic via cronjob
ansible.builtin.include_tasks: restic_create_cron.yml
when:
- restic_create_schedule | bool
- item.name is defined
- item.scheduled | default(false)
- ansible_service_mgr != 'systemd' or restic_force_cron | default(false) or restic_schedule_type == "cronjob"
- name: make sure no unwanted systemd timer is available
ansible.builtin.systemd:
name: "restic-{{ item.name | replace(' ', '') | string }}.timer"
state: 'stopped'
enabled: false
masked: true
with_items: '{{ restic_backups }}'
ignore_errors: true
when:
- restic_create_schedule | bool
- item.name is defined
- item.scheduled | default(false)
- ansible_service_mgr != 'systemd' or restic_force_cron | default(false) or restic_schedule_type == "cronjob"
- name: mask systemd service
ansible.builtin.systemd:
name: "restic-{{ item.name | replace(' ', '') | string }}.service"
state: 'stopped'
enabled: false
masked: true
with_items: '{{ restic_backups }}'
ignore_errors: true
when:
- restic_create_schedule | bool
- item.name is defined
- item.scheduled | default(false)
- ansible_service_mgr != 'systemd' or restic_force_cron | default(false) or restic_schedule_type == "cronjob"