diff --git a/host_vars/pve2-test2.grote.lan.yml b/host_vars/pve2-test2.grote.lan.yml index f5c8b0a2..3d98b58c 100644 --- a/host_vars/pve2-test2.grote.lan.yml +++ b/host_vars/pve2-test2.grote.lan.yml @@ -45,3 +45,10 @@ systemd_timer_schedule: "*-01,04,07,10-01 23:00" - name: "hdd_vm_mirror" 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" diff --git a/host_vars/pve2.grote.lan.yml b/host_vars/pve2.grote.lan.yml index dfaaafbb..ed3618fe 100644 --- a/host_vars/pve2.grote.lan.yml +++ b/host_vars/pve2.grote.lan.yml @@ -284,7 +284,7 @@ ### mgrote.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_keep_snapshots: 5 cv4pve_dl_link: "https://github.com/Corsinvest/cv4pve-autosnap/releases/download/v1.10.0/cv4pve-autosnap-linux-x64.zip" diff --git a/keepass_db.kdbx b/keepass_db.kdbx index ab6d069b..7571f410 100644 Binary files a/keepass_db.kdbx and b/keepass_db.kdbx differ diff --git a/playbooks/3_service/proxmox.yml b/playbooks/3_service/proxmox.yml index b2bacec3..937e8fca 100644 --- a/playbooks/3_service/proxmox.yml +++ b/playbooks/3_service/proxmox.yml @@ -13,8 +13,7 @@ - { role: mgrote.smart, tags: "smart" } - { role: mgrote.postfix, tags: "postfix" } - { role: mgrote.cv4pve-autosnap, - tags: "cv4pve", - when: "'pve2.grote.lan' in inventory_hostname" } + tags: "cv4pve" } - { role: mgrote.r8152_kernel_module, tags: "r8152" } - { role: mgrote.proxmox_bind_mounts, diff --git a/roles/mgrote.cv4pve-autosnap/README.md b/roles/mgrote.cv4pve-autosnap/README.md index 3c389643..2238a007 100644 --- a/roles/mgrote.cv4pve-autosnap/README.md +++ b/roles/mgrote.cv4pve-autosnap/README.md @@ -2,10 +2,9 @@ ### Beschreibung Installiert [cv4pve-autosnap](https://github.com/Corsinvest/cv4pve-autosnap). -Legt einen cronjob und das Logfile an. +Legt einen systemd-timer. ### getestet auf -- [x] ProxMox 6* - [x] ProxMox 7* ### Variablen + Defaults diff --git a/roles/mgrote.cv4pve-autosnap/defaults/main.yml b/roles/mgrote.cv4pve-autosnap/defaults/main.yml index 4012dcc2..4935e342 100644 --- a/roles/mgrote.cv4pve-autosnap/defaults/main.yml +++ b/roles/mgrote.cv4pve-autosnap/defaults/main.yml @@ -12,5 +12,3 @@ # under which user the script is run cv4pve_user_group: cv4pve cv4pve_user: cv4pve - # logfile path - cv4pve_logfile: /var/log/cv4pve-autosnap.log diff --git a/roles/mgrote.cv4pve-autosnap/handlers/main.yml b/roles/mgrote.cv4pve-autosnap/handlers/main.yml new file mode 100644 index 00000000..510b17fe --- /dev/null +++ b/roles/mgrote.cv4pve-autosnap/handlers/main.yml @@ -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 diff --git a/roles/mgrote.cv4pve-autosnap/tasks/main.yml b/roles/mgrote.cv4pve-autosnap/tasks/main.yml index 946c1722..84aab1d2 100644 --- a/roles/mgrote.cv4pve-autosnap/tasks/main.yml +++ b/roles/mgrote.cv4pve-autosnap/tasks/main.yml @@ -2,6 +2,7 @@ - name: include user tasks include_tasks: user.yml + - name: create directories become: yes ansible.builtin.file: @@ -30,41 +31,43 @@ owner: "{{ cv4pve_user }}" group: "{{ cv4pve_user_group }}" - - name: copy bash-script + - name: template cv4pve.service become: yes ansible.builtin.template: - src: "cv4pve-script.sh" - dest: "/usr/local/bin/cv4pve/cv4pve-script.sh" - 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 + src: cv4pve.service.j2 + dest: /etc/systemd/system/cv4pve.service owner: root - group: "{{ cv4pve_user_group }}" + group: root 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 diff --git a/roles/mgrote.cv4pve-autosnap/templates/cv4pve-script.sh b/roles/mgrote.cv4pve-autosnap/templates/cv4pve-script.sh deleted file mode 100644 index 48bd4199..00000000 --- a/roles/mgrote.cv4pve-autosnap/templates/cv4pve-script.sh +++ /dev/null @@ -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 diff --git a/roles/mgrote.cv4pve-autosnap/templates/cv4pve.service.j2 b/roles/mgrote.cv4pve-autosnap/templates/cv4pve.service.j2 new file mode 100644 index 00000000..d6eee5ab --- /dev/null +++ b/roles/mgrote.cv4pve-autosnap/templates/cv4pve.service.j2 @@ -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 diff --git a/roles/mgrote.cv4pve-autosnap/templates/cv4pve.timer.j2 b/roles/mgrote.cv4pve-autosnap/templates/cv4pve.timer.j2 new file mode 100644 index 00000000..ed002a77 --- /dev/null +++ b/roles/mgrote.cv4pve-autosnap/templates/cv4pve.timer.j2 @@ -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 diff --git a/roles/mgrote.cv4pve-autosnap/templates/cv4pve_mail.service.j2 b/roles/mgrote.cv4pve-autosnap/templates/cv4pve_mail.service.j2 new file mode 100644 index 00000000..af2a5c24 --- /dev/null +++ b/roles/mgrote.cv4pve-autosnap/templates/cv4pve_mail.service.j2 @@ -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 }}' diff --git a/roles/mgrote.cv4pve-autosnap/templates/logrotate_cv4pve b/roles/mgrote.cv4pve-autosnap/templates/logrotate_cv4pve deleted file mode 100644 index a20ad8e8..00000000 --- a/roles/mgrote.cv4pve-autosnap/templates/logrotate_cv4pve +++ /dev/null @@ -1,12 +0,0 @@ -{{ file_header | default () }} -{{ cv4pve_logfile }} { - su root root - create 0640 root root - rotate 4 - weekly - compress - missingok - notifempty - dateext - dateyesterday -}