From a394ac09af7183b44138909cfb667d24b16c0c41 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Sat, 15 Apr 2017 00:10:36 +0200
Subject: [PATCH] Ansible script: Try a more idempotent approach for package
 clearing.

---
 ansible/config.yml                   | 26 +++++++++----
 ansible/files/apt-mark/manual        | 32 ++++++++++++++++
 ansible/files/apt-mark/required      | 55 ++++++++++++++++++++++++++++
 ansible/files/apt-mark/required-plus |  3 ++
 4 files changed, 109 insertions(+), 7 deletions(-)
 create mode 100644 ansible/files/apt-mark/manual
 create mode 100644 ansible/files/apt-mark/required
 create mode 100644 ansible/files/apt-mark/required-plus

diff --git a/ansible/config.yml b/ansible/config.yml
index ec592c6..5e7503b 100644
--- a/ansible/config.yml
+++ b/ansible/config.yml
@@ -13,12 +13,12 @@
   # Configure package management.
   - name: update package lists
     apt: update_cache=yes
-  - name: check for initial_purge_happened flag
-    stat: path=flags/initial_purge_happened
-    register: initial_purge
-  - name: perform initial purge
-    include: tasks/initial_purge.yml
-    when: initial_purge.stat.exists == False
+  #- name: check for initial_purge_happened flag
+  #  stat: path=flags/initial_purge_happened
+  #  register: initial_purge
+  #- name: perform initial purge
+  #  include: tasks/initial_purge.yml
+  #  when: initial_purge.stat.exists == False
   - name: APT - dist-upgrade
     apt: upgrade=dist
 
@@ -32,7 +32,7 @@
     shell: update-grub
 
   # Ensure power management.
-  - name: ensure newest kernel and grub are installed
+  - name: ensure power management tools are installed
     apt: name={{item}} state=present
     with_items:
     - tlp
@@ -122,3 +122,15 @@
     - xul-ext-noscript
     - xul-ext-pentadactyl
     - vim-gtk  # used by pentadactyl for text editing
+
+  # Remove undesired packages
+  - name: collect required packages
+    shell: cat files/apt-mark/* > /tmp/white_list_unsorted && sort /tmp/white_list_unsorted > /tmp/white_list_sorted
+  - name: collect currently installed packages
+    shell: dpkg-query -Wf '${Package}\n' > /tmp/all_unsorted && sort /tmp/all_unsorted > /tmp/all_sorted
+  - name: create black list of packages to mark as automatically installed from the difference between the required packages and the packages currently installed
+    shell: comm -3 /tmp/all_sorted /tmp/white_list_sorted > /tmp/list_black
+  - name: mark all packages from black list as automatically installed
+    shell: apt-mark auto $(cat /tmp/list_black)
+  - name: purge all packages automatically installed that are not depended on
+    shell: DEBIAN_FRONTEND=noninteractive apt-get -y --purge autoremove
diff --git a/ansible/files/apt-mark/manual b/ansible/files/apt-mark/manual
new file mode 100644
index 0000000..d2e1137
--- /dev/null
+++ b/ansible/files/apt-mark/manual
@@ -0,0 +1,32 @@
+acpi-call-dkms
+bumblebee-nvidia
+console-setup
+dmenu
+git
+grub2
+i3
+i3status
+iceweasel
+less
+libgl1-mesa-dri
+libglu1-mesa
+libpam-systemd
+linux-headers-amd64
+linux-image-amd64
+locales
+man-db
+manpages
+openssh-client
+primus
+redshift
+sudo
+tlp
+vim
+vim-gtk
+xinit
+x11-xserver-utils
+xserver-xorg-core
+xserver-xorg-input-evdev
+xterm
+xul-ext-noscript
+xul-ext-pentadactyl
diff --git a/ansible/files/apt-mark/required b/ansible/files/apt-mark/required
new file mode 100644
index 0000000..43afba8
--- /dev/null
+++ b/ansible/files/apt-mark/required
@@ -0,0 +1,55 @@
+base-files
+base-passwd
+bash
+bsdutils
+coreutils
+dash
+debconf
+debianutils
+diffutils
+dpkg
+e2fslibs
+e2fsprogs
+findutils
+gcc-6-base
+grep
+gzip
+hostname
+init-system-helpers
+libacl1
+libattr1
+libblkid1
+libc6
+libc-bin
+libcomerr2
+libfdisk1
+libgcc1
+liblzma5
+libmount1
+libpam0g
+libpam-modules
+libpam-modules-bin
+libpam-runtime
+libpcre3
+libselinux1
+libsepol1
+libsmartcols1
+libss2
+libtinfo5
+libuuid1
+login
+lsb-base
+mawk
+mount
+multiarch-support
+ncurses-base
+ncurses-bin
+passwd
+perl-base
+sed
+sensible-utils
+sysvinit-utils
+tar
+tzdata
+util-linux
+zlib1g
diff --git a/ansible/files/apt-mark/required-plus b/ansible/files/apt-mark/required-plus
new file mode 100644
index 0000000..c4e6c7f
--- /dev/null
+++ b/ansible/files/apt-mark/required-plus
@@ -0,0 +1,3 @@
+ansible
+ifupdown
+isc-dhcp-client
-- 
2.30.2