--- - name: Aktualizacja systemów i sprawdzenie wymaganych restartów hosts: all gather_facts: false tasks: # ========================================== # BEZBŁĘDNE SPRAWDZENIE CZY HOST JEST WŁĄCZONY # ========================================== - name: Próba połączenia SSH (check online status) block: - name: Sprawdzenie portu SSH ansible.builtin.wait_for: host: "{{ ansible_host | default(inventory_hostname) }}" port: "{{ ansible_port | default(22) }}" state: started delay: 0 timeout: 3 delegate_to: localhost - name: Ustawienie statusu ONLINE ansible.builtin.set_fact: host_is_online: true rescue: - name: Ustawienie statusu OFFLINE (bez generowania błędu) ansible.builtin.set_fact: host_is_online: false # ========================================== # ZBIERANIE FAKTÓW (tylko dla aktywnych hostów) # ========================================== - name: Zbieranie faktów systemowych ansible.builtin.setup: when: host_is_online # ========================================== # DEBIAN / UBUNTU # ========================================== - name: Aktualizacja pakietów (Debian/Ubuntu) when: - host_is_online - ansible_os_family | default('') == 'Debian' - not (skip_apt | default(false)) block: - name: Aktualizacja pamięci podręcznej i podniesienie pakietów ansible.builtin.apt: update_cache: true upgrade: dist autoremove: true autoclean: true - name: Sprawdzenie czy wymagany jest restart (Debian/Ubuntu) ansible.builtin.stat: path: /var/run/reboot-required register: debian_reboot_file - name: Zapisanie informacji o restarcie (Debian/Ubuntu) ansible.builtin.set_fact: reboot_needed: "{{ debian_reboot_file.stat.exists }}" # ========================================== # ALPINE LINUX # ========================================== - name: Aktualizacja pakietów (Alpine Linux) when: - host_is_online - ansible_os_family | default('') == 'Alpine' - not (skip_apt | default(false)) block: - name: Aktualizacja pamięci podręcznej i pakietów (apk) community.general.apk: update_cache: true upgrade: true - name: Sprawdzenie czy jądro systemu wymaga restartu (Alpine) ansible.builtin.shell: cmd: | running_kernel=$(uname -r) installed_kernel=$(ls -1 /boot/vmlinuz-* 2>/dev/null | tail -n1 | sed 's/.*vmlinuz-//') if [ -n "$installed_kernel" ] && [ "$running_kernel" != "$installed_kernel" ]; then echo "reboot_required" fi executable: /bin/sh register: alpine_kernel_check changed_when: false ignore_errors: true - name: Zapisanie informacji o restarcie (Alpine) ansible.builtin.set_fact: reboot_needed: "{{ 'reboot_required' in alpine_kernel_check.stdout }}" # ========================================== # PODSUMOWANIE # ========================================== - name: Informacja o stanie po aktualizacji ansible.builtin.debug: msg: - "Host: {{ inventory_hostname }} ({{ ansible_distribution | default('Nieokreślony') }})" - "Status: {{ '🔴 NIEOSIĄGALNY / WYŁĄCZONY' if not host_is_online else ('⏭️ POMINIĘTO (skip_apt)' if (skip_apt | default(false)) else '✅ ZAKOŃCZONO SUCCESS') }}" - "Status restartu: {{ '⚠️ WYMAGANY RESTART!' if (reboot_needed | default(false)) else '✅ Brak potrzeby restartu' }}"