67 lines
2.6 KiB
YAML
67 lines
2.6 KiB
YAML
---
|
|
# Raport stanu floty (tylko odczyt, bez żadnych zmian na hostach):
|
|
# OS/wersja, uptime, zajętość dysku, liczba oczekujących aktualizacji
|
|
# oraz flaga reboot-required (decyzję o restarcie podejmuje operator ręcznie).
|
|
#
|
|
# WAŻNE: druga faza (renderowanie raportu) działa na hosts: localhost — jeśli
|
|
# używasz --limit, dopisz do niego ",localhost", inaczej ta faza zostanie pominięta,
|
|
# np.: --limit "Update-now,localhost"
|
|
- name: Zbierz stan floty
|
|
hosts: all
|
|
gather_facts: true
|
|
|
|
tasks:
|
|
- name: Sprawdź czy wymagany jest restart (reboot-required)
|
|
ansible.builtin.stat:
|
|
path: /var/run/reboot-required
|
|
register: reboot_required_stat
|
|
|
|
- name: Policz pakiety oczekujące na aktualizację
|
|
ansible.builtin.command: apt list --upgradable
|
|
register: upgradable_raw
|
|
changed_when: false
|
|
failed_when: false
|
|
when: ansible_os_family in ["Debian", "Ubuntu"]
|
|
|
|
- name: Sprawdź zajętość dysku na partycji root
|
|
ansible.builtin.set_fact:
|
|
root_disk_percent_used: >-
|
|
{{ (( (mount.size_total - mount.size_available) / mount.size_total * 100) | round(0, 'ceil')) | int }}
|
|
vars:
|
|
mount: "{{ ansible_mounts | selectattr('mount', 'equalto', '/') | first }}"
|
|
when: ansible_mounts | selectattr('mount', 'equalto', '/') | list | length > 0
|
|
|
|
- name: Zbuduj podsumowanie hosta
|
|
ansible.builtin.set_fact:
|
|
host_report:
|
|
os: "{{ ansible_distribution }} {{ ansible_distribution_version }}"
|
|
uptime_days: "{{ (ansible_uptime_seconds / 86400) | round(1) }}"
|
|
disk_root_percent: "{{ root_disk_percent_used | default('n/a') }}"
|
|
upgradable_packages: >-
|
|
{{ (upgradable_raw.stdout_lines | default([]) | length - 1)
|
|
if (upgradable_raw is defined and upgradable_raw.stdout_lines is defined and upgradable_raw.stdout_lines | length > 0)
|
|
else 0 }}
|
|
reboot_required: "{{ reboot_required_stat.stat.exists }}"
|
|
|
|
- name: Wygeneruj raport floty
|
|
hosts: localhost
|
|
connection: local
|
|
gather_facts: true
|
|
|
|
tasks:
|
|
- name: Upewnij się, że katalog reports/ istnieje
|
|
ansible.builtin.file:
|
|
path: "{{ playbook_dir }}/../reports"
|
|
state: directory
|
|
mode: "0755"
|
|
|
|
- name: Renderuj raport floty
|
|
ansible.builtin.template:
|
|
src: "{{ playbook_dir }}/../templates/fleet_report.j2"
|
|
dest: "{{ playbook_dir }}/../reports/fleet_report_{{ ansible_date_time.iso8601_basic_short }}.txt"
|
|
mode: "0644"
|
|
|
|
- name: Ścieżka wygenerowanego raportu
|
|
ansible.builtin.debug:
|
|
msg: "Raport zapisany w reports/fleet_report_{{ ansible_date_time.iso8601_basic_short }}.txt"
|