Stell dir vor, du könntest mit einem einfachen Skript die Patchstände hunderter Linux-Server abrufen und übersichtlich in einer Tabelle darstellen. Klingt praktisch?
Mit diesem kleinen Ansible-Playbook ist genau das möglich.
Das Playbook überprüft auf Debian-basierten Linux-Systemen die Anzahl der ausstehenden Updates und sendet die Ergebnisse tabellarisch per E-Mail.
So siehst du sofort, ob ein Server vergessen wurde oder ob automatische Updates sauber laufen.
Ausgegeben werden:
- Servername
- Anzahl der ausstehenden Updates (numerisch)
Ansible Playbook: Anzahl ausstehender Updates
---
- name: Check update status and send email
hosts: all
become: yes
vars:
mailserver: dein-mailserver
mail_sender: ansible@example.com
mail_recipient: admin@example.com
pre_tasks:
# Ensure the temporary file for storing update statuses is empty and exists
- name: Ensure /tmp/all_update_status.txt is empty and exists
copy:
content: ""
dest: /tmp/all_update_status.txt
delegate_to: localhost
run_once: true
tasks:
# Update the APT cache
- name: Update the apt cache
apt:
update_cache: yes
# Check for available updates on Debian systems
- name: Check for available updates on Debian
shell: apt list --upgradable 2>/dev/null | grep -v 'Listing...'
register: update_output
# Ensure update_output.stdout_lines is defined to avoid errors
- name: Ensure update_output.stdout_lines is defined
set_fact:
update_lines: "{{ update_output.stdout_lines | default([]) }}"
# Format the update status for the current host
- name: Format update status
set_fact:
host_update_status: |
{{ inventory_hostname }}
{{ update_lines | length }}
delegate_to: localhost
# Add the formatted update status to the consolidated list
- name: Add host update status to list
lineinfile:
path: /tmp/all_update_status.txt
line: "{{ host_update_status }}"
delegate_to: localhost
- name: Send consolidated update status email
hosts: localhost
gather_facts: no
vars:
msmtp_config_path: "/root/.msmtprc"
mail_sender: ansible@example.com
mail_recipient: admin@example.com
tasks:
# Read all update statuses from the temporary file
- name: Read all update statuses
command: cat /tmp/all_update_status.txt
register: all_update_status_content
# Send the update status email using msmtp
- name: Send update status email with msmtp
shell: |
echo -e "From: {{ mail_sender }}\nTo: {{ mail_recipient }}\nSubject: Debian Update Status\nMIME-Version: 1.0\nContent-Type: text/html\n\nHostnameAusstehende Updates{{ all_update_status_content.stdout }}" | msmtp --file={{ msmtp_config_path }} "{{ mail_recipient }}"
register: msmtp_result
ignore_errors: yes
Was das Ansible-Playbook tut
- Leert und erstellt eine Sammeldatei auf dem Control-Host
- Aktualisiert den APT-Cache auf allen Zielsystemen
- Ermittelt die Anzahl verfügbarer Updates
- Baut daraus HTML-Tabellenzeilen
- Konsolidiert alle Hosts in einer Tabelle
- Versendet das Ergebnis per E-Mail (HTML)
Variante: Ausstehende Updates mit Paketnamen
Möchtest du nicht nur die Anzahl, sondern auch die Namen der ausstehenden Updates, kannst du dieses Playbook verwenden.
Ansible Playbook: Updates inkl. Paketnamen
---
- name: Überprüfe Update-Status und sende E-Mail
hosts: all
become: yes
vars:
mailserver: dein_mail_server
mail_sender: sender@example.com
mail_recipient: recipient@example.com
pre_tasks:
- name: Sicherstellen, dass /tmp/all_update_status.txt leer ist und existiert
copy:
content: ""
dest: /tmp/all_update_status.txt
delegate_to: localhost
run_once: true
tasks:
- name: Aktualisiere den apt-Cache
apt:
update_cache: yes
- name: Verfügbarkeit von Updates auf Debian prüfen
shell: apt list --upgradable 2>/dev/null | grep -v 'Listing...'
register: update_output
- name: Sicherstellen, dass update_output.stdout_lines definiert ist
set_fact:
update_lines: "{{ update_output.stdout_lines | default([]) }}"
- name: Update-Status formatieren
set_fact:
host_update_status: |
{{ inventory_hostname }}
{{ update_lines | length }}
{% for line in update_lines %}
{{ line }}
{% endfor %}
delegate_to: localhost
- name: Host-Update-Status zur Liste hinzufügen
lineinfile:
path: /tmp/all_update_status.txt
line: "{{ host_update_status }}"
delegate_to: localhost
- name: Konsolidierten Update-Status per E-Mail senden
hosts: localhost
gather_facts: no
vars:
msmtp_config_path: "/root/.msmtprc"
mail_sender: sender@example.com
mail_recipient: recipient@example.com
tasks:
- name: Alle Update-Status lesen
command: cat /tmp/all_update_status.txt
register: all_update_status_content
- name: Update-Status-E-Mail mit msmtp senden
shell: |
echo -e "From: {{ mail_sender }}\nTo: {{ mail_recipient }}\nSubject: Debian Update Status\nMIME-Version: 1.0\nContent-Type: text/html\n\nHostnameAnzahl der UpdatesUpdate-Namen{{ all_update_status_content.stdout }}" | msmtp --file={{ msmtp_config_path }} "{{ mail_recipient }}"
register: msmtp_result
ignore_errors: yes
Erweiterungsideen
- Sortierung nach Anzahl der Updates
- Markierung kritischer Pakete
- CSV- oder JSON-Export
- Slack / Matrix / Teams statt E-Mail
- Cron-Ausführung auf dem Control-Node