Files
semaphore/ansible/update.yml

110 lines
4.2 KiB
YAML

---
- name: Aktualizacja systemów i sprawdzenie wymaganych restartów
hosts: all
gather_facts: false
tasks:
# ==========================================
# SPRAWDZENIE DOSTĘPNOŚCI (PING + SSH)
# ==========================================
- name: Weryfikacja dostępności hosta
block:
- name: Sprawdzenie odpowiedzi ICMP Ping
ansible.builtin.command:
cmd: "ping -c 1 -W 2 {{ ansible_host | default(inventory_hostname) }}"
delegate_to: localhost
changed_when: false
- 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
# ==========================================
# POBRANIE FAKTÓW DLA AKTYWNYCH HOSTÓW
# ==========================================
- name: Zbieranie faktów systemowych
ansible.builtin.setup:
when: host_is_online | default(false)
ignore_unreachable: true
ignore_errors: true
# ==========================================
# DEBIAN / UBUNTU
# ==========================================
- name: Aktualizacja pakietów (Debian/Ubuntu)
when:
- host_is_online | default(false)
- ansible_os_family | default('') == 'Debian'
- not (skip | default(false))
ignore_unreachable: true
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 | default(false))
ignore_unreachable: true
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('Brak połączenia') }})"
- "Status: {{ '🔴 NIEOSIĄGALNY / WYŁĄCZONY' if not (host_is_online | default(false)) else ('⏭️ POMINIĘTO (skip)' if (skip | default(false)) else '✅ ZAKOŃCZONO SUCCESS') }}"
- "Status restartu: {{ '⚠️ WYMAGANY RESTART!' if (reboot_needed | default(false)) else '✅ Brak potrzeby restartu' }}"