--- - name: Konserwacja systemu i czyszczenie Docker hosts: all become: true gather_facts: false tasks: # ========================================== # SPRAWDZENIE DOSTĘPNOŚCI (SKIP + PING + SSH) # ========================================== - name: Weryfikacja dostępności hosta when: not (skip | default(false)) 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 # ========================================== # ZBIERANIE FAKTÓW (tylko dla aktywnych hostów) # ========================================== - name: Zbieranie faktów systemowych ansible.builtin.setup: when: - not (skip | default(false)) - host_is_online | default(false) ignore_unreachable: true ignore_errors: true # ========================================== # CZYSZCZENIE SYSTEMOWE # ========================================== - name: Czyszczenie Apt (Debian/Ubuntu) when: - not (skip | default(false)) - host_is_online | default(false) - ansible_os_family | default('') == 'Debian' ignore_unreachable: true block: - name: Usuwanie niepotrzebnych pakietów (autoremove) ansible.builtin.apt: autoremove: true - name: Czyszczenie cache apt (autoclean) ansible.builtin.apt: autoclean: true - name: Ograniczenie wielkości logów journald do 100MB ansible.builtin.command: journalctl --vacuum-size=100M changed_when: false - name: Czyszczenie Apk (Alpine) when: - not (skip | default(false)) - host_is_online | default(false) - ansible_os_family | default('') == 'Alpine' ignore_unreachable: true block: - name: Czyszczenie cache apk ansible.builtin.command: apk cache clean changed_when: false # ========================================== # CZYSZCZENIE DOCKERA # ========================================== - name: Sprawdzenie czy Docker jest zainstalowany ansible.builtin.command: which docker register: docker_check failed_when: false changed_when: false ignore_unreachable: true when: - not (skip | default(false)) - host_is_online | default(false) - name: Czyszczenie nieużywanych zasobów Dockera when: - not (skip | default(false)) - host_is_online | default(false) - docker_check.rc is defined - docker_check.rc == 0 ignore_unreachable: true block: - name: Docker system prune (obrazy, sieci, kontenery, wolumeny) ansible.builtin.command: docker system prune -af --volumes register: docker_prune changed_when: docker_prune.stdout != "" - name: Wyświetlenie odzyskanego miejsca ansible.builtin.debug: var: docker_prune.stdout_lines # ========================================== # PODSUMOWANIE # ========================================== - name: Informacja o stanie ansible.builtin.debug: msg: - "Host: {{ inventory_hostname }}" - "Status: {{ '⏭️ POMINIĘTO (skip: true)' if (skip | default(false)) else ('🔴 OFFLINE / UNREACHABLE' if not (host_is_online | default(false)) else '✅ WYKONANO KONSERWACJĘ') }}"