From cf0eabd109dda744bef897012fad31e51b79a65f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20=C5=BBbikowski?= Date: Tue, 11 Aug 2026 22:20:35 +0200 Subject: [PATCH] Update ansible/ping_check.yml --- ansible/ping_check.yml | 150 ++++++++++++++++------------------------- 1 file changed, 58 insertions(+), 92 deletions(-) diff --git a/ansible/ping_check.yml b/ansible/ping_check.yml index 4f86708..e59d680 100644 --- a/ansible/ping_check.yml +++ b/ansible/ping_check.yml @@ -1,106 +1,72 @@ --- -- name: Aktualizacja systemów i sprawdzenie wymaganych restartów +- name: Test dostępności hostów oraz logowania SSH hosts: all - gather_facts: false # Wyłączamy automatyczny zbieranie faktów na start - ignore_unreachable: true - ignore_errors: true + gather_facts: false tasks: # ========================================== - # SPRAWDZENIE CZY HOST JEST WŁĄCZONY (SSH) + # 1. SPRAWDZENIE PING (ICMP) # ========================================== - - name: Sprawdzenie dostępności 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 - register: ssh_check - 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 - - - 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)) + - name: Test PING (ICMP) 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 + - name: Wykonaj ping do hosta + ansible.builtin.command: "ping -c 2 -W 2 {{ inventory_hostname }}" + delegate_to: localhost changed_when: false - ignore_errors: true - - - name: Zapisanie informacji o restarcie (Alpine) + - name: Ustaw wynik PING jako OK ansible.builtin.set_fact: - reboot_needed: "{{ 'reboot_required' in alpine_kernel_check.stdout }}" + icmp_status: "OK" + rescue: + - name: Ustaw wynik PING jako FAIL + ansible.builtin.set_fact: + icmp_status: "FAIL" # ========================================== - # PODSUMOWANIE + # 2. SPRAWDZENIE PORTU SSH (TCP 22) # ========================================== - - name: Informacja o stanie po aktualizacji + - name: Test portu SSH (TCP 22) + block: + - name: Sprawdź otwarcie portu 22 + ansible.builtin.wait_for: + host: "{{ inventory_hostname }}" + port: 22 + state: started + delay: 0 + timeout: 3 + delegate_to: localhost + - name: Ustaw wynik portu SSH jako OTWARTY + ansible.builtin.set_fact: + ssh_port_status: "OTWARTY" + ssh_port_open: true + rescue: + - name: Ustaw wynik portu SSH jako ZAMKNIĘTY + ansible.builtin.set_fact: + ssh_port_status: "ZAMKNIĘTY/TIMEOUT" + ssh_port_open: false + + # ========================================== + # 3. SPRAWDZENIE LOGOWANIA SSH + # ========================================== + - name: Test logowania po SSH (Ansible Ping) + when: ssh_port_open + block: + - name: Próba zalogowania i wykonania modułu ping + ansible.builtin.ping: + - name: Ustaw wynik logowania jako SUKCES + ansible.builtin.set_fact: + ssh_login_status: "SUKCES" + rescue: + - name: Ustaw wynik logowania jako BŁĄD LOGOWANIA + ansible.builtin.set_fact: + ssh_login_status: "BŁĄD LOGOWANIA / BRAK UPRAWNIEŃ" + + # ========================================== + # 4. PODSUMOWANIE DLA DANEGO HOSTA + # ========================================== + - name: Podsumowanie stanu hosta ansible.builtin.debug: msg: - - "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 + - "Host: {{ inventory_hostname }}" + - "PING (ICMP): {{ icmp_status }}" + - "Port SSH (22): {{ ssh_port_status }}" + - "Logowanie SSH: {{ ssh_login_status | default('POMINIĘTO (Port zamknięty)') }}" \ No newline at end of file