-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete-files.yaml
42 lines (36 loc) · 1.2 KB
/
delete-files.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
---
- name: Check root filesystem usage
hosts: all
become: true
vars:
filesystem: "/"
tasks:
- name: Get filesystem usage
ansible.builtin.shell: df -h {{ filesystem }} | awk 'NR==2 {print $5}' | sed 's/%//'
register: filesystem_usage
changed_when: false
- name: Set fact for filesystem usage
ansible.builtin.set_fact:
root_fs_usage: "{{ filesystem_usage.stdout | int }}"
- name: Delete files is root filesystem is more than 80% full
when: root_fs_usage > "80"
block:
- name: Categorize filesystem usage
ansible.builtin.debug:
msg: >
Root filesystem usage is ({{ root_fs_usage }}%) utilized
- name: Analyzing the directories to delete...
ansible.builtin.find:
paths: /tmp
patterns: "*.file"
age: 5
age_stamp: atime
register: files_to_delete
- name: Displaying the result...
ansible.builtin.debug:
var: files_to_delete.files
- name: Deleting the files...
ansible.builtin.file:
path: "{{ item.path }}"
state: absent
with_items: "{{ files_to_delete.files }}"