Compare commits

..

No commits in common. "b6a7375b7e24f6fad3046645c185c329337abf77" and "3bffa17b70cd293d9efde8a12aa0f1f09ddd5c12" have entirely different histories.

View File

@ -1,178 +1,104 @@
--- ---
# ============================================================================= - name: Check if Docker is installed
# SYSTEM CLEANUP ROLE command: docker --version
# ============================================================================= register: docker_check
ignore_errors: yes
# ========== Docker Pre-check ========== changed_when: false
- name: Check if Docker is installed
command: docker --version - name: Remove unwanted system packages
register: docker_check apt:
ignore_errors: yes name: "{{ cleanup_packages }}"
changed_when: false state: absent
tags: [cleanup, docker] autoremove: "{{ cleanup_autoremove }}"
purge: true
# ========== APT Cleanup ========== when: cleanup_packages | length > 0
- name: Remove unwanted system packages
apt: - name: Remove specific SSH authorized keys (if any defined)
name: "{{ cleanup_packages }}" authorized_key:
state: absent user: root
autoremove: "{{ cleanup_autoremove }}" key: "{{ item }}"
purge: true state: absent
when: cleanup_packages | default([]) | length > 0 loop: "{{ cleanup_ssh_keys | default([]) }}"
become: yes when: cleanup_ssh_keys | length > 0
tags: [cleanup, apt]
- name: Prune unused Docker containers
- name: Clean APT cache (remove downloaded .deb files) command: docker container prune -f
command: apt-get clean when:
changed_when: false - cleanup_docker and cleanup_docker_containers
become: yes - docker_check.rc == 0
tags: [cleanup, apt] changed_when: true
- name: Remove old configuration files (dpkg --purge rc packages) - name: Prune unused Docker images
shell: dpkg --purge $(dpkg --list | grep '^rc' | awk '{print $2}') 2>/dev/null || true command: docker image prune -af
changed_when: true when:
become: yes - cleanup_docker and cleanup_docker_images
tags: [cleanup, apt] - docker_check.rc == 0
changed_when: true
- name: Remove old kernels (keep current + N last)
shell: | - name: Prune unused Docker volumes
CURRENT_KERNEL=$(uname -r) command: docker volume prune -f
dpkg -l 'linux-image-*' 2>/dev/null | awk '/^ii/ {print $2}' | grep -v "$CURRENT_KERNEL" | sort -V | head -n -{{ cleanup_keep_kernels | default(2) }} | xargs -r apt-get purge -y || true when:
changed_when: true - cleanup_docker and cleanup_docker_volumes
become: yes - docker_check.rc == 0
tags: [cleanup, apt, kernels] changed_when: true
# ========== SSH Keys Cleanup ========== - name: Prune unused Docker networks
- name: Remove specific SSH authorized keys (if any defined) command: docker network prune -f
authorized_key: when:
user: root - cleanup_docker and cleanup_docker_networks
key: "{{ item }}" - docker_check.rc == 0
state: absent changed_when: true
loop: "{{ cleanup_ssh_keys | default([]) }}"
when: cleanup_ssh_keys | default([]) | length > 0 - name: Remove custom directories (use with caution!)
become: yes file:
tags: [cleanup, ssh] path: "{{ item }}"
state: absent
# ========== Docker Cleanup ========== loop: "{{ cleanup_directories | default([]) }}"
- name: Prune unused Docker containers when: cleanup_directories | length > 0
command: docker container prune -f
when: - name: Remove cAdvisor (if cleanup_cadvisor is true)
- cleanup_docker | default(false) block:
- cleanup_docker_containers | default(false) - name: Check if cAdvisor container exists
- docker_check.rc == 0 shell: docker ps -a --filter name=cadvisor --format "{{ '{{.Names}}' }}"
changed_when: true register: cadvisor_check
become: yes changed_when: false
tags: [cleanup, docker] when: docker_check.rc == 0
- name: Prune unused Docker images - name: Stop and remove cAdvisor container using docker-compose
command: docker image prune -af community.docker.docker_compose_v2:
when: project_src: "{{ cadvisor_base_dir | default('/opt/cadvisor') }}"
- cleanup_docker | default(false) state: absent
- cleanup_docker_images | default(false) become: yes
- docker_check.rc == 0 when:
changed_when: true - docker_check.rc == 0
become: yes - "'cadvisor' in cadvisor_check.stdout"
tags: [cleanup, docker]
- name: Remove cAdvisor Docker image
- name: Prune unused Docker volumes command: docker rmi gcr.io/cadvisor/cadvisor || true
command: docker volume prune -f when: docker_check.rc == 0
when: ignore_errors: yes
- cleanup_docker | default(false)
- cleanup_docker_volumes | default(false) - name: Remove cAdvisor directories
- docker_check.rc == 0 file:
changed_when: true path: "{{ item }}"
become: yes state: absent
tags: [cleanup, docker] loop:
- "{{ cadvisor_base_dir | default('/opt/cadvisor') }}"
- name: Prune unused Docker networks - "{{ cadvisor_config_dir | default('/opt/cadvisor/config') }}"
command: docker network prune -f ignore_errors: yes
when:
- cleanup_docker | default(false) - name: Verify cAdvisor removal
- cleanup_docker_networks | default(false) shell: |
- docker_check.rc == 0 echo "Containers: $(docker ps -a | grep cadvisor | wc -l 2>/dev/null || echo 0)"
changed_when: true echo "Images: $(docker images | grep cadvisor | wc -l 2>/dev/null || echo 0)"
become: yes echo "Directories: $(ls -d {{ cadvisor_base_dir | default('/opt/cadvisor') }}* 2>/dev/null | wc -l || echo 0)"
tags: [cleanup, docker] register: cadvisor_removal_check
changed_when: false
- name: Prune Docker build cache when: docker_check.rc == 0
command: docker builder prune -f
when: - name: Show cAdvisor removal status
- cleanup_docker | default(false) debug:
- docker_check.rc == 0 msg: "cAdvisor removal status: {{ cadvisor_removal_check.stdout }}"
changed_when: true when: cadvisor_removal_check is defined
become: yes
tags: [cleanup, docker] when: cleanup_cadvisor | default(false) and docker_check.rc == 0
# ========== Journal Logs Vacuum ==========
- name: Vacuum systemd journal logs
command: journalctl --vacuum-size={{ cleanup_max_journal_size | default('100M') }}
changed_when: true
become: yes
tags: [cleanup, logs]
# ========== Old Log Files Cleanup ==========
- name: Find old rotated logs (*.1, *.gz, *.old)
find:
paths: /var/log
patterns: "*.1,*.gz,*.old"
age: "{{ cleanup_logs_age_days | default(7) }}d"
recurse: yes
register: old_logs
become: yes
tags: [cleanup, logs]
- name: Delete found old log files
file:
path: "{{ item.path }}"
state: absent
loop: "{{ old_logs.files }}"
become: yes
tags: [cleanup, logs]
# ========== Temporary Files Cleanup ==========
- name: Clean systemd tmpfiles
command: systemd-tmpfiles --clean
changed_when: true
become: yes
tags: [cleanup, tmp]
- name: Find old files in /tmp (exclude system dirs)
find:
paths: /tmp
age: "{{ cleanup_tmp_age_days | default(3) }}d"
exclude: "systemd-*,ssh-*,tmux-*,.X*,.ICE-unix*"
register: tmp_old_files
become: yes
tags: [cleanup, tmp]
- name: Delete old /tmp files
file:
path: "{{ item.path }}"
state: absent
loop: "{{ tmp_old_files.files }}"
become: yes
tags: [cleanup, tmp]
# ========== Custom Directories Removal ==========
- name: Remove custom directories (if defined)
file:
path: "{{ item }}"
state: absent
loop: "{{ cleanup_directories | default([]) }}"
when: cleanup_directories | default([]) | length > 0
become: yes
tags: [cleanup]
# ========== Summary: Show Disk Usage After Cleanup ==========
- name: Show disk usage after cleanup
command: df -h /
register: disk_after
changed_when: false
tags: [cleanup, summary]
- name: Display cleanup summary
debug:
msg: |
=== 🧹 Cleanup Summary ===
{{ disk_after.stdout_lines | join('\n') }}
tags: [cleanup, summary]