[Skripte]

Ansible - Linux Update Status abfragen

/ansible-linux-update-status-abfragen

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: |
          <tr>
            <td>{{ inventory_hostname }}</td>
            <td>{{ update_lines | length }}</td>
          </tr>
      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\n<table border='1'><tr><th>Hostname</th><th>Ausstehende Updates</th></tr>{{ all_update_status_content.stdout }}</table>" | msmtp --file={{ msmtp_config_path }} "{{ mail_recipient }}"
      register: msmtp_result
      ignore_errors: yes
````

---

## Was das Ansible-Playbook tut

1. Leert und erstellt eine Sammeldatei auf dem Control-Host
2. Aktualisiert den APT-Cache auf allen Zielsystemen
3. Ermittelt die Anzahl verfügbarer Updates
4. Baut daraus HTML-Tabellenzeilen
5. Konsolidiert alle Hosts in einer Tabelle
6. 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

```yaml
---
- 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: |
          <tr>
            <td>{{ inventory_hostname }}</td>
            <td>{{ update_lines | length }}</td>
            <td>
              {% for line in update_lines %}
                {{ line }}<br/>
              {% endfor %}
            </td>
          </tr>
      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\n<table border='1'><tr><th>Hostname</th><th>Anzahl der Updates</th><th>Update-Namen</th></tr>{{ all_update_status_content.stdout }}</table>" | 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

Anzeige

/comments0 Einträge

Kommentare

> NO_COMMENTS_FOUND

> INITIATE_COMMENT_PROTOCOL

MARKDOWN_SUPPORT: ENABLED
CHARS: 0