Update ansible/ping_check.yml

This commit is contained in:
2026-08-11 22:15:26 +02:00
parent 1fccaad781
commit 028609a0d3

View File

@@ -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 hosts: all
gather_facts: false gather_facts: false # Wyłączamy automatyczny zbieranie faktów na start
ignore_unreachable: true ignore_unreachable: true
ignore_errors: true
tasks: tasks:
# 1. SPRAWDZENIE PING (ICMP) # ==========================================
- name: Sprawdź odpowiadanie na ping (ICMP) # SPRAWDZENIE CZY HOST JEST WŁĄCZONY (SSH)
ansible.builtin.command: "ping -c 2 -W 2 {{ inventory_hostname }}" # ==========================================
delegate_to: localhost - name: Sprawdzenie dostępności portu SSH
register: icmp_result
ignore_errors: true
changed_when: false
# 2. SPRAWDZENIE PORTU SSH (TCP 22)
- name: Sprawdź czy port SSH (22) jest otwarty
ansible.builtin.wait_for: ansible.builtin.wait_for:
host: "{{ inventory_hostname }}" host: "{{ ansible_host | default(inventory_hostname) }}"
port: 22 port: "{{ ansible_port | default(22) }}"
state: started state: started
delay: 0 delay: 0
timeout: 3 timeout: 3
delegate_to: localhost delegate_to: localhost
register: ssh_port_result register: ssh_check
ignore_errors: true ignore_errors: true
# 3. SPRAWDZENIE LOGOWANIA SSH (Ansible Ping) - name: Oznaczenie hosta jako niedostępnego
- name: Próba zalogowania się przez SSH ansible.builtin.set_fact:
ansible.builtin.ping: host_is_online: false
register: ssh_login_result when: ssh_check.failed is defined and ssh_check.failed
ignore_errors: true
# 4. PODSUMOWANIE DLA DANEGO HOSTA - name: Oznaczenie hosta jako dostępnego
- name: Podsumowanie stanu hosta 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: ansible.builtin.debug:
msg: msg:
- "Host: {{ inventory_hostname }}" - "Host: {{ inventory_hostname }} ({{ ansible_distribution | default('Brak połączenia') }})"
- "PING (ICMP): {{ 'OK' if icmp_result.rc == 0 else 'FAIL' }}" - "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') }}"
- "Port SSH (22): {{ 'OTWARTY' if not ssh_port_result.failed else 'ZAMKNIĘTY/TIMEOUT' }}" - "Status restartu: {{ '⚠️ WYMAGANY RESTART!' if (reboot_needed | default(false)) else '✅ Brak potrzeby restartu' }}"
- "Logowanie SSH: {{ 'SUKCES' if not ssh_login_result.failed else 'BŁĄD LOGOWANIA' }}"