From 028609a0d3f716340cd3a6093ebc5b1eee7ef5bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20=C5=BBbikowski?= Date: Tue, 11 Aug 2026 22:15:26 +0200 Subject: [PATCH] Update ansible/ping_check.yml --- ansible/ping_check.yml | 117 ++++++++++++++++++++++++++++++++--------- 1 file changed, 91 insertions(+), 26 deletions(-) diff --git a/ansible/ping_check.yml b/ansible/ping_check.yml index a24f6aa..4f86708 100644 --- a/ansible/ping_check.yml +++ b/ansible/ping_check.yml @@ -1,41 +1,106 @@ --- -- name: Test dostępności hostów oraz logowania SSH +- name: Aktualizacja systemów i sprawdzenie wymaganych restartów hosts: all - gather_facts: false + gather_facts: false # Wyłączamy automatyczny zbieranie faktów na start ignore_unreachable: true + ignore_errors: true tasks: - # 1. SPRAWDZENIE PING (ICMP) - - name: Sprawdź odpowiadanie na ping (ICMP) - ansible.builtin.command: "ping -c 2 -W 2 {{ inventory_hostname }}" - delegate_to: localhost - register: icmp_result - ignore_errors: true - changed_when: false - - # 2. SPRAWDZENIE PORTU SSH (TCP 22) - - name: Sprawdź czy port SSH (22) jest otwarty + # ========================================== + # SPRAWDZENIE CZY HOST JEST WŁĄCZONY (SSH) + # ========================================== + - name: Sprawdzenie dostępności portu SSH ansible.builtin.wait_for: - host: "{{ inventory_hostname }}" - port: 22 + host: "{{ ansible_host | default(inventory_hostname) }}" + port: "{{ ansible_port | default(22) }}" state: started delay: 0 timeout: 3 delegate_to: localhost - register: ssh_port_result + register: ssh_check ignore_errors: true - # 3. SPRAWDZENIE LOGOWANIA SSH (Ansible Ping) - - name: Próba zalogowania się przez SSH - ansible.builtin.ping: - register: ssh_login_result - ignore_errors: true + - name: Oznaczenie hosta jako niedostępnego + ansible.builtin.set_fact: + host_is_online: false + when: ssh_check.failed is defined and ssh_check.failed - # 4. PODSUMOWANIE DLA DANEGO HOSTA - - name: Podsumowanie stanu hosta + - name: Oznaczenie hosta jako dostępnego + ansible.builtin.set_fact: + host_is_online: true + when: ssh_check.failed is not defined or not ssh_check.failed + + # ========================================== + # POBRANIE FAKTÓW DLA AKTYWNYCH HOSTÓW + # ========================================== + - name: Zbieranie faktów systemowych + ansible.builtin.setup: + when: host_is_online | default(false) + ignore_errors: true + ignore_unreachable: true + + # ========================================== + # DEBIAN / UBUNTU + # ========================================== + - name: Aktualizacja pakietów (Debian/Ubuntu) + when: + - host_is_online | default(false) + - 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 | default(false) + - 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 }}" - - "PING (ICMP): {{ 'OK' if icmp_result.rc == 0 else 'FAIL' }}" - - "Port SSH (22): {{ 'OTWARTY' if not ssh_port_result.failed else 'ZAMKNIĘTY/TIMEOUT' }}" - - "Logowanie SSH: {{ 'SUKCES' if not ssh_login_result.failed else 'BŁĄD LOGOWANIA' }}" \ No newline at end of file + - "Host: {{ inventory_hostname }} ({{ ansible_distribution | default('Brak połączenia') }})" + - "Status: {{ '🔴 NIEOSIĄGALNY / WYŁĄCZONY' if not (host_is_online | default(false)) 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' }}" \ No newline at end of file