cv4pve auf systemd umgestellt (#344)

Co-authored-by: Michael Grote <michael.grote@posteo.de>
Reviewed-on: mg/ansible#344
Co-authored-by: mg <michael.grote@posteo.de>
Co-committed-by: mg <michael.grote@posteo.de>
This commit is contained in:
Michael Grote 2022-03-28 14:48:31 +02:00
parent 3fb2820e41
commit 17f0ca874e
13 changed files with 101 additions and 60 deletions

View file

@ -45,3 +45,10 @@
systemd_timer_schedule: "*-01,04,07,10-01 23:00" systemd_timer_schedule: "*-01,04,07,10-01 23:00"
- name: "hdd_vm_mirror" - name: "hdd_vm_mirror"
systemd_timer_schedule: "*-01,04,07,10-01 23:00" systemd_timer_schedule: "*-01,04,07,10-01 23:00"
### mgrote.cv4pve-autosnap
cv4pve_api_user: root@pam!cv4pve-autosnap
cv4pve_api_token: "{{ lookup('keepass', 'cv4pve_api_token_pve2-test2', 'password') }}"
cv4pve_vmid: all,-127,-112,-100,-116,-105
cv4pve_keep_snapshots: 5
cv4pve_dl_link: "https://github.com/Corsinvest/cv4pve-autosnap/releases/download/v1.10.0/cv4pve-autosnap-linux-x64.zip"

View file

@ -284,7 +284,7 @@
### mgrote.cv4pve-autosnap ### mgrote.cv4pve-autosnap
cv4pve_api_user: root@pam!cv4pve-autosnap cv4pve_api_user: root@pam!cv4pve-autosnap
cv4pve_api_token: "{{ lookup('keepass', 'cv4pve_api_token', 'password') }}" cv4pve_api_token: "{{ lookup('keepass', 'cv4pve_api_token_pve2', 'password') }}"
cv4pve_vmid: all,-127,-112,-100,-116,-105 cv4pve_vmid: all,-127,-112,-100,-116,-105
cv4pve_keep_snapshots: 5 cv4pve_keep_snapshots: 5
cv4pve_dl_link: "https://github.com/Corsinvest/cv4pve-autosnap/releases/download/v1.10.0/cv4pve-autosnap-linux-x64.zip" cv4pve_dl_link: "https://github.com/Corsinvest/cv4pve-autosnap/releases/download/v1.10.0/cv4pve-autosnap-linux-x64.zip"

Binary file not shown.

View file

@ -13,8 +13,7 @@
- { role: mgrote.smart, tags: "smart" } - { role: mgrote.smart, tags: "smart" }
- { role: mgrote.postfix, tags: "postfix" } - { role: mgrote.postfix, tags: "postfix" }
- { role: mgrote.cv4pve-autosnap, - { role: mgrote.cv4pve-autosnap,
tags: "cv4pve", tags: "cv4pve" }
when: "'pve2.grote.lan' in inventory_hostname" }
- { role: mgrote.r8152_kernel_module, - { role: mgrote.r8152_kernel_module,
tags: "r8152" } tags: "r8152" }
- { role: mgrote.proxmox_bind_mounts, - { role: mgrote.proxmox_bind_mounts,

View file

@ -2,10 +2,9 @@
### Beschreibung ### Beschreibung
Installiert [cv4pve-autosnap](https://github.com/Corsinvest/cv4pve-autosnap). Installiert [cv4pve-autosnap](https://github.com/Corsinvest/cv4pve-autosnap).
Legt einen cronjob und das Logfile an. Legt einen systemd-timer.
### getestet auf ### getestet auf
- [x] ProxMox 6*
- [x] ProxMox 7* - [x] ProxMox 7*
### Variablen + Defaults ### Variablen + Defaults

View file

@ -12,5 +12,3 @@
# under which user the script is run # under which user the script is run
cv4pve_user_group: cv4pve cv4pve_user_group: cv4pve
cv4pve_user: cv4pve cv4pve_user: cv4pve
# logfile path
cv4pve_logfile: /var/log/cv4pve-autosnap.log

View file

@ -0,0 +1,26 @@
---
- name: systemctl daemon-reload
become: yes
ansible.builtin.systemd:
daemon_reload: yes
- name: systemctl enable units
become: yes
ansible.builtin.systemd:
name: "{{ item }}"
enabled: yes
masked: no
with_items:
- cv4pve.service
- cv4pve_mail.service
- cv4pve.timer
- name: systemctl start units
become: yes
ansible.builtin.systemd:
name: "{{ item }}"
state: restarted
enabled: yes
with_items:
- cv4pve.timer
notify: systemctl daemon-reload

View file

@ -2,6 +2,7 @@
- name: include user tasks - name: include user tasks
include_tasks: user.yml include_tasks: user.yml
- name: create directories - name: create directories
become: yes become: yes
ansible.builtin.file: ansible.builtin.file:
@ -30,41 +31,43 @@
owner: "{{ cv4pve_user }}" owner: "{{ cv4pve_user }}"
group: "{{ cv4pve_user_group }}" group: "{{ cv4pve_user_group }}"
- name: copy bash-script - name: template cv4pve.service
become: yes become: yes
ansible.builtin.template: ansible.builtin.template:
src: "cv4pve-script.sh" src: cv4pve.service.j2
dest: "/usr/local/bin/cv4pve/cv4pve-script.sh" dest: /etc/systemd/system/cv4pve.service
mode: a+x
owner: "{{ cv4pve_user }}"
group: "{{ cv4pve_user_group }}"
- name: create cronjob
become: yes
ansible.builtin.cron:
name: cv4pve-autosnap
state: present
job: "/usr/local/bin/cv4pve/cv4pve-script.sh"
minute: "{{ cv4pve_cron_minute }}"
hour: "{{ cv4pve_cron_hour }}"
user: "{{ cv4pve_user }}"
- name: create log
become: true
ansible.builtin.file:
path: /var/log/cv4pve-autosnap.log
state: touch
owner: "{{ cv4pve_user }}"
group: "{{ cv4pve_user_group }}"
mode: 0640
access_time: preserve
modification_time: preserve
- name: copy logrotate config
become: yes
ansible.builtin.template:
src: logrotate_cv4pve
dest: /etc/logrotate.d/cv4pve-autosnap
owner: root owner: root
group: "{{ cv4pve_user_group }}" group: root
mode: 0644 mode: 0644
notify:
- systemctl daemon-reload
- name: template cv4pve_mail.service
become: yes
ansible.builtin.template:
src: cv4pve_mail.service.j2
dest: /etc/systemd/system/cv4pve_mail.service
owner: root
group: root
mode: 0644
notify:
- systemctl daemon-reload
- name: template cv4pve.timer
become: yes
ansible.builtin.template:
src: cv4pve.timer.j2
dest: /etc/systemd/system/cv4pve.timer
owner: root
group: root
mode: 0644
notify:
- systemctl daemon-reload
- name: systemctl start cv4pve.timer
become: yes
ansible.builtin.systemd:
name: cv4pve.timer
state: started
enabled: yes

View file

@ -1,7 +0,0 @@
#!/bin/bash
{{ file_header | default () }}
(
/usr/local/bin/cv4pve/cv4pve-autosnap --host=127.0.0.1 --api-token {{ cv4pve_api_user }}={{ cv4pve_api_token }} --vmid="{{ cv4pve_vmid }}" snap --label='daily' --keep="{{ cv4pve_keep_snapshots }}" --state | ts '%Y-%m-%d - %H-%M-%S'
/usr/local/bin/cv4pve/cv4pve-autosnap --host=127.0.0.1 --api-token {{ cv4pve_api_user }}={{ cv4pve_api_token }} --vmid="all" status | ts '%Y-%m-%d - %H-%M-%S'
) >> {{ cv4pve_logfile }} 2>&1

View file

@ -0,0 +1,9 @@
{{ file_header | default () }}
[Unit]
Description=Trigger VM-Snapshots in PVE with cv4pve.
After=multi-user.target
OnFailure=cv4pve_mail.service
[Service]
Type=simple
ExecStart=/usr/local/bin/cv4pve/cv4pve-autosnap --host=127.0.0.1 --api-token {{ cv4pve_api_user }}={{ cv4pve_api_token }} --vmid="{{ cv4pve_vmid }}" snap --label='daily' --keep="{{ cv4pve_keep_snapshots }}" --state

View file

@ -0,0 +1,11 @@
{{ file_header | default () }}
[Unit]
Description=Timer: Trigger VM-Snapshots in PVE with cv4pve.
[Timer]
OnCalendar=*-*-* {{ cv4pve_cron_hour }}:{{ cv4pve_cron_minute }}:00
RandomizedDelaySec=10 min
[Install]
WantedBy=timers.target multi-user.target

View file

@ -0,0 +1,8 @@
{{ file_header | default () }}
[Unit]
Description=Send a Mail in case of an error in cv4pve.service.
[Service]
Type=oneshot
ExecStart=/bin/bash -c '/bin/systemctl status cv4pve.service | mail -s "[ERROR] cv4pve - %H" {{ empfaenger_mail }}'

View file

@ -1,12 +0,0 @@
{{ file_header | default () }}
{{ cv4pve_logfile }} {
su root root
create 0640 root root
rotate 4
weekly
compress
missingok
notifempty
dateext
dateyesterday
}