clean maintenance tasks

This commit is contained in:
Moritz 2024-09-25 17:53:35 +02:00
parent b49ab2a2a4
commit 75035e7961
5 changed files with 79 additions and 9 deletions

View file

@ -1,3 +1,38 @@
- name: lookup debian version
shell: cat /etc/debian_version
register: debian_version
- name: outputs debian version
debug:
var: debian_version.stdout_lines
- name: update apt cache
apt:
update_cache: yes
- name: show updateable packages
shell: apt-get -s dist-upgrade | grep Inst
register: apt_upgrade_result
changed_when: false
failed_when: apt_upgrade_result.rc > 1
- name: outputs updatable packages
debug:
var: apt_upgrade_result.stdout_lines
when: apt_upgrade_result.stdout_lines | length > 0
- name: mark server as "restart required" if necessary
shell: "needrestart -b | grep -E 'NEEDRESTART-SESS|NEEDRESTART-SVC|NEEDRESTART-KSTA: 3'"
register: restart_needed
ignore_errors: yes
changed_when: false
failed_when: restart_needed.rc > 1
tags:
- needrestart
- restart
- name: output "restart required"
debug:
var: restart_needed.stdout_lines
when: restart_needed.stdout_lines | length > 0
tags: needrestart

7
tasks/docker.yml Normal file
View file

@ -0,0 +1,7 @@
- name: Perform Docker system prune
shell: docker system prune -f
register: docker_prune
- name: outputs docker prune
debug:
var: docker_prune.stdout_lines

View file

@ -1,8 +1,15 @@
- name: dist upgrade software
tags: [apt]
apt:
autoremove: yes
update_cache: yes
cache_valid_time: 3600
state: latest
upgrade: dist
- name: check server status
import_tasks: check.yml
tags: check
- name: update packages
import_tasks: update.yml
tags: update
- name: restart server if required
import_tasks: restart.yml
tags: restart
- name: docker maintenance
import_tasks: docker.yml
tags: docker

13
tasks/restart.yml Normal file
View file

@ -0,0 +1,13 @@
- name: schedule restart randomly between 2-5 AM if necessary
shell: |
hour=$(shuf -i 2-5 -n 1)
minute=$(shuf -i 0-59 -n 1)
shutdown -r "$hour:$minute"
echo "restart scheduled for $hour:$minute"
when: restart_needed.stdout_lines | length > 0
register: restard_scheduled
- name: output "restart schedule"
debug:
var: restard_scheduled.stdout_lines
when: restart_needed.stdout_lines | length > 0

8
tasks/update.yml Normal file
View file

@ -0,0 +1,8 @@
- name: dist upgrade software
tags: [apt]
apt:
autoremove: yes
update_cache: yes
cache_valid_time: 3600
state: latest
upgrade: dist