---
-
  name: "Including OS-specific vars"
  ansible.builtin.include_vars: "{{ ansible_os_family | lower }}.yml"

-
  name: "Updating apt cache"
  ansible.builtin.apt:
    cache_valid_time: 3600
    update_cache: true

-
  name: "Upgrading all apt packages"
  ansible.builtin.apt:
    force_apt_get: true
    upgrade: dist

-
  name: "Removing Unneeded Dev Packages, Docs and Man Pages"
  ansible.builtin.shell: "{{ item }}"  # noqa: command-instead-of-shell
  loop:
    - "dpkg --list | awk '{ print $2 }' | grep linux-source | xargs apt-get -y purge"
    - "dpkg --list | awk '{ print $2 }' | grep -- '-doc$' | xargs apt-get -y purge"
  changed_when: true
  failed_when: false

-
  name: "Removing LXD Snap"
  community.general.snap:
    name:
      - lxd
    state: absent
  failed_when: false

-
  name: "Removing core18 and core20 Snap"
  community.general.snap:
    name:
      - core18
      - core20
    state: absent
  failed_when: false

-
  name: "Removing Snapd"
  community.general.snap:
    name:
      - snapd
    state: absent
  failed_when: false

-
  name: "Removing unwanted packages"
  ansible.builtin.apt:
    name:
      - snapd
      - ppp
      - pppconfig
      - pppoeconf
      - linux-source
      - popularity-contest
      - installation-report
      - landscape-common
      - wireless-tools
      - wpasupplicant
      - ubuntu-serverguide
    state: absent

-
  name: "Using Deborphan to clean up packages"
  ansible.builtin.shell: "{{ item }}"  # noqa: command-instead-of-shell
  loop:
    - "deborphan --find-config | xargs apt-get -y purge"
    - "deborphan | xargs apt-get -y purge"
  changed_when: true
  failed_when: false

-
  name: "Cleaning up udev rules"
  ansible.builtin.file:
    path: "{{ item }}"
    state: absent
  loop:
    - /dev/.udev/
    - /lib/udev/rules.d/75-persistent-net-generator.rules

-
  name: "Cleaning leftover DHCP Leases"
  ansible.builtin.file:
    path: "{{ item }}"
    state: absent
  loop:
    - /var/lib/dhcp3/*
    - /var/lib/dhcp/*

-
  name: "Removing Machine ID file"
  ansible.builtin.file:
    path: "{{ item }}"
    state: absent
  loop:
    - /etc/machine-id

-
  name: "Creating empty Machine ID file"
  ansible.builtin.copy:
    content: ""
    dest: "{{ item }}"
    force: false
    mode: "0644"
  loop:
    - /etc/machine-id

-
  name: "Deregistering the Machine ID"
  ansible.builtin.file:
    path: /var/lib/dbus/machine-id
    state: absent

-
  name: "Linking Machine ID file"
  ansible.builtin.file:
    src: /etc/machine-id
    dest: /var/lib/dbus/machine-id
    state: link

-
  name: "Finding Apt cache directories"
  ansible.builtin.find:
    file_type: directory
    paths: /var/lib/apt
  register: apt_cache_files_to_delete

-
  name: "Removing Apt cache directories"
  ansible.builtin.file:
    path: "{{ item.path }}"
    state: absent
  loop: "{{ apt_cache_files_to_delete.files }}"

-
  name: "Finding var cache directories"
  ansible.builtin.find:
    file_type: directory
    paths: /var/cache
  register: var_cache_files_to_delete

-
  name: "Removing var cache directories"
  ansible.builtin.file:
    path: "{{ item.path }}"
    state: absent
  loop: "{{ var_cache_files_to_delete.files }}"

-
  name: "Finding /var/log files.old or .log via regex"
  ansible.builtin.find:
    paths: /var/log
    patterns: "^.*?\\.(?:old|log\\.gz)$"
    use_regex: true
  register: log_files_to_delete

-
  name: "Removing old log files"
  ansible.builtin.file:
    path: "{{ item.path }}"
    state: absent
  loop: "{{ log_files_to_delete.files }}"

-
  name: "Removing specific log files"
  ansible.builtin.file:
    path: "{{ item }}"
    state: absent
  loop:
    - /var/log/lastlog
    - /var/log/wtmp
    - /var/log/btmp

-
  name: "Truncating Audit Logs"
  ansible.builtin.copy:
    content: ""
    dest: "{{ item }}"
    force: false
    mode: "0644"
  loop:
    - /var/log/lastlog
    - /var/log/wtmp
    - /var/log/btmp

-
  name: "Clearing the History File"
  ansible.builtin.command: "unset HISTFILE"
  changed_when: false
  failed_when: false

-
  name: "Finding all log files to truncate"
  ansible.builtin.find:
    paths: /var/log
    file_type: file
    recurse: true
  register: all_log_files

-
  name: "Truncating all log files"
  ansible.builtin.copy:
    content: ""
    dest: "{{ item.path }}"
    backup: false
    force: true
    mode: preserve
  loop: "{{ all_log_files.files }}"
  failed_when: false

-
  name: "Removing Caches, Logs, Docs, Man Pages, History, Tmp"
  ansible.builtin.file:
    path: "{{ item }}"
    state: absent
  loop:
    - /root/.bash_history
    - "/home/{{ user }}/.bash_history"
    - /usr/share/doc/*
    - /usr/share/man/*
    - /tmp/*

-
  name: "Recreating Doc and Man directories"
  ansible.builtin.file:
    path: "{{ item }}"
    state: directory
    mode: "0755"
  loop:
    - /usr/share/doc/
    - /usr/share/man/

-
  name: "Recreating TMP"
  ansible.builtin.file:
    path: "{{ item }}"
    state: directory
    mode: "0777"
  loop:
    - /tmp/

-
  name: "Removing useless packages from the cache"
  ansible.builtin.apt:
    autoclean: true

-
  name: "Registering Stale Netplan Configs"
  ansible.builtin.find:
    paths: /etc/netplan
    patterns: "*.yaml"
  register: netplan_files_to_delete

-
  name: "Removing old YAML configs"
  ansible.builtin.file:
    path: "{{ item.path }}"
    state: absent
  loop: "{{ netplan_files_to_delete.files }}"

-
  name: "Adding enp0s6 for DHCP for BHYVE Zones"
  ansible.builtin.template:
    dest: /etc/netplan/50-cloud-init.yaml
    mode: "0755"
    src: dhcp-all.debian.yaml.j2

-
  name: "Purging and Autoremoving packages"
  ansible.builtin.apt:
    autoremove: true
    purge: true

-
  name: "Running Final Cleanup Commands"
  ansible.builtin.shell: "{{ item }}"  # noqa: command-instead-of-shell
  loop:
    - "sudo apt-get --purge autoremove"
    - "rm /var/lib/dbus/machine-id"
    - "sudo rm -rf /snap"
    - "sudo rm -rf /root/snap"
    - "sudo rm -rf /var/snap"
    - "sudo rm -rf /etc/cloud"
    - "sudo rm -rf /var/lib/cloud"
  changed_when: true
  failed_when: false

-
  name: "Removing old directories and files"
  ansible.builtin.file:
    path: "{{ item }}"
    state: absent
  loop:
    - "/snap"
    - "/root/snap"
    - "/var/snap"
    - "/etc/cloud"
    - "/var/lib/cloud"
