From 01ed60a25b9dd665677edb504cdef3b011ab4c09 Mon Sep 17 00:00:00 2001 From: Kevin Thompson Date: Sat, 10 May 2025 22:57:29 -0500 Subject: [PATCH] Make Debian role e-mail me when run --- .../debian_update/tasks/main.yml | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/roles/infrastructure/debian_update/tasks/main.yml b/roles/infrastructure/debian_update/tasks/main.yml index 0cc1c20..415caa2 100644 --- a/roles/infrastructure/debian_update/tasks/main.yml +++ b/roles/infrastructure/debian_update/tasks/main.yml @@ -4,8 +4,33 @@ update_cache: yes cache_valid_time: 3600 -- name: Upgrade all packages to latest - ansible.builtin.apt: - upgrade: dist - autoremove: yes - autoclean: yes +- name: Upgrade all packages and capture output + ansible.builtin.shell: | + apt-get update + apt-get upgrade -y + register: apt_upgrade + changed_when: false + +- name: Determine upgrade message + ansible.builtin.set_fact: + upgrade_summary: >- + {% if '0 upgraded' in apt_upgrade.stdout %} + No packages were upgraded on {{ inventory_hostname }}. + {% else %} + The following packages were upgraded on {{ inventory_hostname }}: + + {{ apt_upgrade.stdout }} + {% endif %} + +- name: Write summary to file + ansible.builtin.copy: + dest: /tmp/apt-upgrade-summary.txt + content: "{{ upgrade_summary }}" + +- name: Email the upgrade summary + ansible.builtin.mail: + host: localhost + port: 25 + to: you@example.com + subject: "Debian Package Upgrade Report - {{ inventory_hostname }}" + body: "{{ upgrade_summary }}"