-
Notifications
You must be signed in to change notification settings - Fork 1
/
clean.yml
70 lines (60 loc) · 2.25 KB
/
clean.yml
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env ansible-playbook
#
# Credits to: https://serverfault.com/questions/644082/running-apt-get-autoremove-with-ansible
#
---
- name: Autoremove 'apt' package for Debian, Ubuntu
hosts: all,!command_and_control
become: True
gather_facts: True
# ansible by default run parallel for every server, but we change this to run on synchronous mode.
serial: "{{ serial_number | default(1) }}"
pre_tasks:
- name: check storage space - before
shell: df -h
register: check_storage_space_before
when: ansible_distribution == 'Ubuntu'
- name: print storage space
debug:
msg: "{{ check_storage_space_before.stdout_lines }}"
when: ansible_distribution == 'Ubuntu'
- name: apt autoremove check
command: apt-get -y --dry-run autoremove
register: apt_autoremove_output
when: ansible_distribution == 'Ubuntu'
- name: print apt autoremove packages
debug:
msg: "{{ apt_autoremove_output.stdout_lines }}"
when: ansible_distribution == 'Ubuntu'
tasks:
- name: autoremove unused packages
become: yes
command: apt-get -y autoremove
changed_when: "'The following packages will be REMOVED' in apt_autoremove_output.stdout"
when: ansible_distribution == 'Ubuntu'
- name: Check If Docker Is Installed
command: docker --version
register: docker_valid
ignore_errors: yes
- name: Debug Docker Output
debug:
var: docker_valid
- name: Docker prune
command: "{{ item }}"
vars:
docker_prune_until_in_hours: 168 # 168 hours == 7 days
with_items:
- docker image prune -a --force --filter "until={{ docker_prune_until_in_hours }}h"
- docker container prune --force --filter "until={{ docker_prune_until_in_hours }}h"
- docker network prune --force --filter "until={{ docker_prune_until_in_hours }}h"
- docker volume prune --force
when: not docker_valid.failed
post_tasks:
- name: check storage space - after
shell: df -h
register: check_storage_space_after
when: ansible_distribution == 'Ubuntu'
- name: print storage space
debug:
msg: "{{ check_storage_space_after.stdout_lines }}"
when: ansible_distribution == 'Ubuntu'