diff --git a/ansible/clear.yml b/ansible/clear.yml new file mode 100644 index 0000000..a43bd14 --- /dev/null +++ b/ansible/clear.yml @@ -0,0 +1,92 @@ +--- +- name: Konserwacja systemu i czyszczenie Docker + hosts: all + become: true + gather_facts: false + ignore_errors: true + + tasks: + # ========================================== + # SPRAWDZENIE CZY HOST JEST WŁĄCZONY (SSH) + # ========================================== + - 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 statusu hosta + ansible.builtin.set_fact: + host_is_online: "{{ not (ssh_check.failed | default(false)) }}" + + # ========================================== + # ZBIERANIE FAKTÓW (tylko dla online) + # ========================================== + - name: Zbieranie faktów systemowych + ansible.builtin.setup: + when: host_is_online + + # ========================================== + # CZYSZCZENIE SYSTEMOWE + # ========================================== + - name: Czyszczenie Apt (Debian/Ubuntu) + when: + - host_is_online + - ansible_os_family | default('') == 'Debian' + 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: + - host_is_online + - ansible_os_family | default('') == 'Alpine' + 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 + when: host_is_online + + - name: Czyszczenie nieużywanych zasobów Dockera + when: + - host_is_online + - docker_check.rc == 0 + 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: {{ '🔴 OFFLINE' if not host_is_online else '✅ WYKONANO KONSERWACJĘ' }}" \ No newline at end of file