Update ansible/ping_check.yml
This commit is contained in:
@@ -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
|
hosts: all
|
||||||
gather_facts: false # Wyłączamy automatyczny zbieranie faktów na start
|
gather_facts: false
|
||||||
ignore_unreachable: true
|
|
||||||
ignore_errors: true
|
|
||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
# ==========================================
|
# ==========================================
|
||||||
# SPRAWDZENIE CZY HOST JEST WŁĄCZONY (SSH)
|
# 1. SPRAWDZENIE PING (ICMP)
|
||||||
# ==========================================
|
# ==========================================
|
||||||
- name: Sprawdzenie dostępności portu SSH
|
- name: Test PING (ICMP)
|
||||||
|
block:
|
||||||
|
- name: Wykonaj ping do hosta
|
||||||
|
ansible.builtin.command: "ping -c 2 -W 2 {{ inventory_hostname }}"
|
||||||
|
delegate_to: localhost
|
||||||
|
changed_when: false
|
||||||
|
- name: Ustaw wynik PING jako OK
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
icmp_status: "OK"
|
||||||
|
rescue:
|
||||||
|
- name: Ustaw wynik PING jako FAIL
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
icmp_status: "FAIL"
|
||||||
|
|
||||||
|
# ==========================================
|
||||||
|
# 2. SPRAWDZENIE PORTU SSH (TCP 22)
|
||||||
|
# ==========================================
|
||||||
|
- name: Test portu SSH (TCP 22)
|
||||||
|
block:
|
||||||
|
- name: Sprawdź otwarcie portu 22
|
||||||
ansible.builtin.wait_for:
|
ansible.builtin.wait_for:
|
||||||
host: "{{ ansible_host | default(inventory_hostname) }}"
|
host: "{{ inventory_hostname }}"
|
||||||
port: "{{ ansible_port | default(22) }}"
|
port: 22
|
||||||
state: started
|
state: started
|
||||||
delay: 0
|
delay: 0
|
||||||
timeout: 3
|
timeout: 3
|
||||||
delegate_to: localhost
|
delegate_to: localhost
|
||||||
register: ssh_check
|
- name: Ustaw wynik portu SSH jako OTWARTY
|
||||||
ignore_errors: true
|
|
||||||
|
|
||||||
- name: Oznaczenie hosta jako niedostępnego
|
|
||||||
ansible.builtin.set_fact:
|
ansible.builtin.set_fact:
|
||||||
host_is_online: false
|
ssh_port_status: "OTWARTY"
|
||||||
when: ssh_check.failed is defined and ssh_check.failed
|
ssh_port_open: true
|
||||||
|
rescue:
|
||||||
- name: Oznaczenie hosta jako dostępnego
|
- name: Ustaw wynik portu SSH jako ZAMKNIĘTY
|
||||||
ansible.builtin.set_fact:
|
ansible.builtin.set_fact:
|
||||||
host_is_online: true
|
ssh_port_status: "ZAMKNIĘTY/TIMEOUT"
|
||||||
when: ssh_check.failed is not defined or not ssh_check.failed
|
ssh_port_open: false
|
||||||
|
|
||||||
# ==========================================
|
# ==========================================
|
||||||
# POBRANIE FAKTÓW DLA AKTYWNYCH HOSTÓW
|
# 3. SPRAWDZENIE LOGOWANIA SSH
|
||||||
# ==========================================
|
# ==========================================
|
||||||
- name: Zbieranie faktów systemowych
|
- name: Test logowania po SSH (Ansible Ping)
|
||||||
ansible.builtin.setup:
|
when: ssh_port_open
|
||||||
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:
|
block:
|
||||||
- name: Aktualizacja pamięci podręcznej i podniesienie pakietów
|
- name: Próba zalogowania i wykonania modułu ping
|
||||||
ansible.builtin.apt:
|
ansible.builtin.ping:
|
||||||
update_cache: true
|
- name: Ustaw wynik logowania jako SUKCES
|
||||||
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:
|
ansible.builtin.set_fact:
|
||||||
reboot_needed: "{{ debian_reboot_file.stat.exists }}"
|
ssh_login_status: "SUKCES"
|
||||||
|
rescue:
|
||||||
# ==========================================
|
- name: Ustaw wynik logowania jako BŁĄD LOGOWANIA
|
||||||
# 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:
|
ansible.builtin.set_fact:
|
||||||
reboot_needed: "{{ 'reboot_required' in alpine_kernel_check.stdout }}"
|
ssh_login_status: "BŁĄD LOGOWANIA / BRAK UPRAWNIEŃ"
|
||||||
|
|
||||||
# ==========================================
|
# ==========================================
|
||||||
# PODSUMOWANIE
|
# 4. PODSUMOWANIE DLA DANEGO HOSTA
|
||||||
# ==========================================
|
# ==========================================
|
||||||
- name: Informacja o stanie po aktualizacji
|
- name: Podsumowanie stanu hosta
|
||||||
ansible.builtin.debug:
|
ansible.builtin.debug:
|
||||||
msg:
|
msg:
|
||||||
- "Host: {{ inventory_hostname }} ({{ ansible_distribution | default('Brak połączenia') }})"
|
- "Host: {{ inventory_hostname }}"
|
||||||
- "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') }}"
|
- "PING (ICMP): {{ icmp_status }}"
|
||||||
- "Status restartu: {{ '⚠️ WYMAGANY RESTART!' if (reboot_needed | default(false)) else '✅ Brak potrzeby restartu' }}"
|
- "Port SSH (22): {{ ssh_port_status }}"
|
||||||
|
- "Logowanie SSH: {{ ssh_login_status | default('POMINIĘTO (Port zamknięty)') }}"
|
||||||
Reference in New Issue
Block a user