From c642012754b1d87e72c0e8014901107ef5adb6f3 Mon Sep 17 00:00:00 2001 From: Moritz Date: Tue, 4 Feb 2025 23:54:18 +0100 Subject: [PATCH] add nightly_update role --- tasks/main.yml | 4 ++++ tasks/nightly_update.yml | 36 ++++++++++++++++++++++++++++++++++++ tasks/restart.yml | 2 +- 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tasks/nightly_update.yml diff --git a/tasks/main.yml b/tasks/main.yml index 70bf691..4b69a98 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -6,6 +6,10 @@ import_tasks: update.yml tags: update +- name: schedule package update and restart for the night + import_tasks: nightly_update.yml + tags: nightly_update + - name: restart server if required import_tasks: restart.yml tags: restart diff --git a/tasks/nightly_update.yml b/tasks/nightly_update.yml new file mode 100644 index 0000000..1b84d59 --- /dev/null +++ b/tasks/nightly_update.yml @@ -0,0 +1,36 @@ +- name: Create update Script + copy: + dest: /usr/local/bin/nightly-update.sh + content: | + #!/bin/bash + { + echo "Starting update at $(date)" + apt-get update -q + apt-get -y -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confold dist-upgrade --auto-remove + echo "Update completed at $(date)" + } >> "/var/log/nightly-update.log" 2>&1 + shutdown -r now + mode: '0755' + +- name: Ensure at is installed + apt: + name: at + state: present + +- name: Ensure atd (at daemon) is running and enabled + service: + name: atd + state: started + enabled: true + +- name: Schedule nightly-update.sh to run between 5:00-6:59 AM once + shell: | + hour=$(shuf -i 5-6 -n 1) + minute=$(shuf -i 0-59 -n 1) + echo "/usr/local/bin/nightly-update.sh" | at "$hour:$minute" + echo "update and restart scheduled for $hour:$minute" + register: restard_scheduled + +- name: output "restart schedule" + debug: + var: restard_scheduled.stdout_lines diff --git a/tasks/restart.yml b/tasks/restart.yml index a79f505..1a70494 100644 --- a/tasks/restart.yml +++ b/tasks/restart.yml @@ -1,4 +1,4 @@ -- name: schedule restart randomly between 2-5 AM if necessary +- name: schedule restart randomly between 5-7 AM if necessary shell: | hour=$(shuf -i 5-6 -n 1) minute=$(shuf -i 0-59 -n 1)