removed some old swap files and added cleanup cache and log files
This commit is contained in:
parent
2577db04be
commit
86d343e75a
Binary file not shown.
Binary file not shown.
|
|
@ -0,0 +1,72 @@
|
|||
- name: Clean Old Log Files and Cache
|
||||
hosts: linux
|
||||
become: true
|
||||
serial: 1
|
||||
|
||||
vars:
|
||||
log_dirs:
|
||||
- /var/log
|
||||
log_age_days: 30
|
||||
|
||||
tasks:
|
||||
- name: Remove compressed/rotated log files older than {{ log_age_days }} days
|
||||
ansible.builtin.find:
|
||||
paths: "{{ log_dirs }}"
|
||||
patterns: "*.gz,*.1,*.2,*.3,*.4,*.5,*.old"
|
||||
age: "{{ log_age_days }}d"
|
||||
recurse: true
|
||||
register: old_logs
|
||||
|
||||
- name: Delete found old log files
|
||||
ansible.builtin.file:
|
||||
path: "{{ item.path }}"
|
||||
state: absent
|
||||
loop: "{{ old_logs.files }}"
|
||||
|
||||
- name: Truncate large active log files (> 100MB) without deleting them
|
||||
ansible.builtin.shell: |
|
||||
find /var/log -type f -name "*.log" -size +100M | while read f; do
|
||||
truncate -s 0 "$f"
|
||||
done
|
||||
changed_when: false
|
||||
|
||||
- name: Clean journal logs older than 30 days
|
||||
ansible.builtin.command: journalctl --vacuum-time=30d
|
||||
changed_when: false
|
||||
|
||||
- name: Limit journal size to 500MB
|
||||
ansible.builtin.command: journalctl --vacuum-size=500M
|
||||
changed_when: false
|
||||
|
||||
- name: Remove unused packages and dependencies
|
||||
ansible.builtin.apt:
|
||||
autoremove: yes
|
||||
|
||||
- name: Clean APT package cache
|
||||
ansible.builtin.apt:
|
||||
clean: yes
|
||||
|
||||
- name: Remove obsolete package files from APT cache
|
||||
ansible.builtin.apt:
|
||||
autoclean: yes
|
||||
|
||||
- name: Clean user thumbnail/cache directories
|
||||
ansible.builtin.shell: |
|
||||
find /home -maxdepth 3 -type d -name ".cache" | while read d; do
|
||||
find "$d" -type f -atime +{{ log_age_days }} -delete 2>/dev/null || true
|
||||
done
|
||||
changed_when: false
|
||||
|
||||
- name: Clean /tmp files older than 7 days
|
||||
ansible.builtin.find:
|
||||
paths: /tmp
|
||||
age: 7d
|
||||
recurse: true
|
||||
register: tmp_files
|
||||
|
||||
- name: Delete old /tmp files
|
||||
ansible.builtin.file:
|
||||
path: "{{ item.path }}"
|
||||
state: absent
|
||||
loop: "{{ tmp_files.files }}"
|
||||
ignore_errors: true
|
||||
Loading…
Reference in New Issue