Skip to content
This repository was archived by the owner on Oct 7, 2025. It is now read-only.

Commit 6610923

Browse files
authored
r73510-apt-reboot-alerts (#2730)
* r73510-apt-reboot-alerts * fix linting error
1 parent a391435 commit 6610923

File tree

6 files changed

+111
-0
lines changed

6 files changed

+111
-0
lines changed

roles/debian/apt_unattended_upgrades/defaults/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ apt_unattended_upgrades:
1616
automatic_reboot_with_users: "false" # reboot even if users are logged in
1717
automatic_reboot_time: "02:00"
1818
enable_syslog: "false" # make apt log upgrades to syslog as well as apt history
19+
rebootnotifyallow: false # Send email when reboot is required
20+
rebootnotifymailfrom: "[email protected]" # Send reboot notify email from address
21+
rebootnotifymailto: "[email protected]" # Send reboot notify email to address

roles/debian/apt_unattended_upgrades/tasks/main.yml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,74 @@
5555
state: restarted
5656
enabled: true
5757
when: apt_unattended_upgrades.enable
58+
59+
- name: Deploy apt reboot hook
60+
ansible.builtin.copy:
61+
dest: /etc/apt/apt.conf.d/99notify-reboot
62+
owner: root
63+
group: root
64+
mode: "0644"
65+
content: 'DPkg::Post-Invoke { "/usr/local/sbin/apt-reboot-notify-hook"; };'
66+
backup: true
67+
force: true
68+
when:
69+
- apt_unattended_upgrades.enable
70+
- apt_unattended_upgrades.rebootnotifyallow
71+
72+
- name: Deploy apt-reboot-notify-hook script
73+
ansible.builtin.template:
74+
src: apt-reboot-notify-hook.j2
75+
dest: /usr/local/sbin/apt-reboot-notify-hook
76+
owner: root
77+
group: root
78+
mode: "0755"
79+
force: true
80+
when:
81+
- apt_unattended_upgrades.enable
82+
- apt_unattended_upgrades.rebootnotifyallow
83+
84+
- name: Install reboot-required daily reminder script
85+
ansible.builtin.template:
86+
src: reboot-required-notify.j2
87+
dest: /usr/local/sbin/reboot-required-notify
88+
owner: root
89+
group: root
90+
mode: "0755"
91+
force: true
92+
when:
93+
- apt_unattended_upgrades.enable
94+
- apt_unattended_upgrades.rebootnotifyallow
95+
96+
- name: Install reboot-required systemd service
97+
ansible.builtin.template:
98+
src: reboot-required-notify.service.j2
99+
dest: /etc/systemd/system/reboot-required-notify.service
100+
owner: root
101+
group: root
102+
mode: "0644"
103+
force: true
104+
when:
105+
- apt_unattended_upgrades.enable
106+
- apt_unattended_upgrades.rebootnotifyallow
107+
108+
- name: Install reboot-required systemd timer
109+
ansible.builtin.template:
110+
src: reboot-required-notify.timer.j2
111+
dest: /etc/systemd/system/reboot-required-notify.timer
112+
owner: root
113+
group: root
114+
mode: "0644"
115+
force: true
116+
when:
117+
- apt_unattended_upgrades.enable
118+
- apt_unattended_upgrades.rebootnotifyallow
119+
120+
- name: Reload systemd and enable reboot-required timer
121+
ansible.builtin.systemd_service:
122+
state: restarted
123+
enabled: true
124+
daemon_reload: true
125+
name: reboot-required-notify.timer
126+
when:
127+
- apt_unattended_upgrades.enable
128+
- apt_unattended_upgrades.rebootnotifyallow
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
set -eu
3+
if [ -f /run/reboot-required ] && [ ! -f /run/reboot-mail-sent ]; then
4+
HOST=$(/bin/hostname -f)
5+
printf 'A reboot is required on %s\n\nFlag: /run/reboot-required\n' "$HOST" \
6+
| /usr/bin/mail -s "Reboot required on $HOST" -r "{{ apt_unattended_upgrades.rebootnotifymailfrom }}" "{{ apt_unattended_upgrades.rebootnotifymailto }}"
7+
/usr/bin/touch /run/reboot-mail-sent
8+
fi
9+
exit 0
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
set -eu
3+
HOST="$(/bin/hostname -f)"
4+
STAMP_DIR="/var/lib/reboot-notify"
5+
STAMP_FILE="${STAMP_DIR}/last-notified"
6+
TODAY="$(/bin/date +%F)"
7+
if [ ! -f /run/reboot-required ]; then exit 0; fi
8+
/bin/mkdir -p "$STAMP_DIR"
9+
if [ -f "$STAMP_FILE" ] && [ "$(cat "$STAMP_FILE")" = "$TODAY" ]; then exit 0; fi
10+
(
11+
echo "A reboot is still required on ${HOST}."
12+
echo
13+
echo "Flag present: /run/reboot-required"
14+
) | /usr/bin/mail -s "Reminder: reboot required on ${HOST}" -r "{{ apt_unattended_upgrades.rebootnotifymailfrom }}" "{{ apt_unattended_upgrades.rebootnotifymailto }}"
15+
echo "$TODAY" > "$STAMP_FILE"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[Unit]
2+
Description=Send daily email reminder if /run/reboot-required exists
3+
[Service]
4+
Type=oneshot
5+
ExecStart=/usr/local/sbin/reboot-required-notify
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[Unit]
2+
Description=Daily reminder for reboot-required
3+
[Timer]
4+
OnCalendar=08:30
5+
Persistent=true
6+
Unit=reboot-required-notify.service
7+
[Install]
8+
WantedBy=timers.target

0 commit comments

Comments
 (0)