#####################################
### someone's ansible provisioner ###
#####################################
# Part of: https://git.somenet.org/root/pub/somesible.git
# 2017-2025 by someone <someone@somenet.org>
#
# cleanup stuff.
# The aggressive mode should be followed by or part of a full somesible run.
#
---
- name: cleanup /var/ansible/.ansible
  file:
    path: "/var/ansible/.ansible"
    state: absent
  changed_when: False


- name: cleanup apt download cache (TEMP)
  include_tasks: run-command.yml
  vars:
    cmd: 'apt-get clean'
  when: 'cleanup_level|int >= 2'


- name: cleanup old logfiles (543210 min = 377.23 days)
  include_tasks: run-command.yml
  vars:
    cmd: 'find /var/log/ -type f -mmin +543210 -print -delete'
  when: 'cleanup_level|int >= 3'


- name: cleanup old dist-files
  include_tasks: run-command.yml
  vars:
    cmd: 'find /etc \( -name "*.dpkg-*" -o -name "*.ucf-*" -o -name "*.merge-error" \) -print -delete'
  when: 'cleanup_level|int >= 4'


- name: cleanup old munin files (543210 min = 377.23 days)
  include_tasks: run-command.yml
  vars:
    cmd: 'find /var/lib/munin*/* -type f -mmin +543210 -print -delete'
  when: 'cleanup_level|int >= 5'


- name: cleanup old anope db files (543210 min = 377.23 days)
  include_tasks: run-command.yml
  vars:
    cmd: 'find /srv/anope/db/backups -type f -mmin +543210 -print -delete'
  when: 'cleanup_level|int >= 5'


###################################################
# cleanup possibly stale configs generated by us. #
###################################################
- name: remove systemd presets
  include_tasks: run-command.yml
  vars:
    cmd: 'rm -vf /etc/systemd/system-preset/*-somesible-*'
  when: 'cleanup_level|int >= 6'


# level 7 can cause issues by skipping over stuff if run at the wrong time.

- name: remove all awstat configs
  include_tasks: run-command.yml
  vars:
    cmd: 'rm -vf /etc/awstats/awstats.*.conf'
  when: 'cleanup_level|int >= 7'


# level 9 is expected to cause downtimes + the need to run multiple times/manually start (now/temp.) broken services.

- name: remove all vhost configs
  include_tasks: run-command.yml
  vars:
    cmd: 'rm -vf /etc/nginx/sites-enabled/*.vhost'
  when: 'cleanup_level|int >= 9'
