From 366562251ffe8bcdb001a869157833802d7be488 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Sat, 15 Apr 2017 02:46:04 +0200
Subject: [PATCH] Improve idempotency.

---
 ansible/config.yml                            | 102 ++++++++++--------
 ansible/files/apt-mark/3d_acceleration        |   5 +
 ansible/files/apt-mark/basic_x_tools          |   6 ++
 ansible/files/apt-mark/browser_environment    |   4 +
 ansible/files/apt-mark/console                |   2 +
 ansible/files/apt-mark/{required => core}     |   0
 ansible/files/apt-mark/man                    |   2 +
 ansible/files/apt-mark/manual                 |  32 ------
 ...uired-plus => minimal_ansible_environment} |   0
 ansible/files/apt-mark/minimal_x              |   4 +
 ansible/files/apt-mark/newest_kernel          |   2 +
 ansible/files/apt-mark/power_management       |   2 +
 .../files/apt-mark/sudo_git_vim_less_openssh  |   5 +
 13 files changed, 91 insertions(+), 75 deletions(-)
 create mode 100644 ansible/files/apt-mark/3d_acceleration
 create mode 100644 ansible/files/apt-mark/basic_x_tools
 create mode 100644 ansible/files/apt-mark/browser_environment
 create mode 100644 ansible/files/apt-mark/console
 rename ansible/files/apt-mark/{required => core} (100%)
 create mode 100644 ansible/files/apt-mark/man
 delete mode 100644 ansible/files/apt-mark/manual
 rename ansible/files/apt-mark/{required-plus => minimal_ansible_environment} (100%)
 create mode 100644 ansible/files/apt-mark/minimal_x
 create mode 100644 ansible/files/apt-mark/newest_kernel
 create mode 100644 ansible/files/apt-mark/power_management
 create mode 100644 ansible/files/apt-mark/sudo_git_vim_less_openssh

diff --git a/ansible/config.yml b/ansible/config.yml
index 5e7503b..d96d2b6 100644
--- a/ansible/config.yml
+++ b/ansible/config.yml
@@ -25,18 +25,20 @@
   # Upgrade kernel.
   - name: ensure newest kernel and grub are installed
     apt: name={{item}} state=present
-    with_items:
-    - linux-image-amd64
-    - grub2
+    with_lines: cat ~/config/ansible/files/apt-mark/newest_kernel | sed -e 's/ *#.*$//'
+    #with_items:
+    #- linux-image-amd64
+    #- grub2
   - name: update grub
     shell: update-grub
 
   # Ensure power management.
   - name: ensure power management tools are installed
     apt: name={{item}} state=present
-    with_items:
-    - tlp
-    - acpi-call-dkms
+    with_lines: cat ~/config/ansible/files/apt-mark/power_management | sed -e 's/ *#.*$//'
+    #with_items:
+    #- tlp
+    #- acpi-call-dkms
   - name: start TLP
     shell: tlp start
 
@@ -46,9 +48,10 @@
     with_fileglob: ~/config/ansible/files/console/*
   - name: ensure locales and console-setup are installed
     apt: name={{item}} state=present
-    with_items:
-    - locales
-    - console-setup
+    with_lines: cat ~/config/ansible/files/apt-mark/console | sed -e 's/ *#.*$//'
+    #with_items:
+    #- locales
+    #- console-setup
   - name: generate en_US.UTF-8 locale
     locale_gen: name=en_US.UTF-8 state=present
   - name: run setupcon to apply console settings from /etc/default/
@@ -64,19 +67,21 @@
     file: state=directory dest=~/.vimbackups
   - name: ensure man-db, manpages is installed
     apt: name={{item}} state=present
-    with_items:
-    - man-db
-    - manpages
+    with_lines: cat ~/config/ansible/files/apt-mark/man | sed -e 's/ *#.*$//'
+    #with_items:
+    #- man-db
+    #- manpages
   - name: set /etc/localtime
     file: state=link force=yes src=/usr/share/zoneinfo/Europe/Berlin dest=/etc/localtime
   - name: ensure sudo, git, vim, less, openssh are installed
     apt: name={{item}} state=present
-    with_items:
-    - git
-    - vim
-    - less
-    - sudo
-    - openssh-client
+    with_lines: cat ~/config/ansible/files/apt-mark/sudo_git_vim_less_openssh | sed -e 's/ *#.*$//'
+    #with_items:
+    #- git
+    #- vim
+    #- less
+    #- sudo
+    #- openssh-client
   - name: ensure boot messages are not cleared on start up
     replace: dest=/etc/systemd/system/getty.target.wants/getty@tty1.service regexp='^TTYVTDisallocate=yes.*$' replace='TTYVTDisallocate=no'
 
@@ -91,46 +96,57 @@
   # Ensure X window environment.
   - name: ensure minimal X window environment
     apt: name={{item}} state=present
-    with_items:
-    - xserver-xorg-core
-    - xserver-xorg-input-evdev  # supports all input devices the kernel knows about
-    - xinit  # contains startx
-    - libpam-systemd  # needed to start X as non-root
+    with_lines: cat ~/config/ansible/files/apt-mark/minimal_x | sed -e 's/ *#.*$//'
+    #with_items:
+    #- xserver-xorg-core
+    #- xserver-xorg-input-evdev  # supports all input devices the kernel knows about
+    #- xinit  # contains startx
+    #- libpam-systemd  # needed to start X as non-root
   - name: ensure 3d acceleration and optimus switch
     apt: name={{item}} state=present
-    with_items:
-    - linux-headers-amd64  # necessary to build proper nvidia-driver module
-    - libgl1-mesa-dri  # necessary for OpenGL 3D acceleration to work
-    - libglu1-mesa  # necessary for OpenGL 3D acceleration to work
-    - bumblebee-nvidia
-    - primus
+    with_lines: cat ~/config/ansible/files/apt-mark/3d_acceleration | sed -e 's/ *#.*$//'
+    #with_items:
+    #- linux-headers-amd64  # necessary to build proper nvidia-driver module
+    #- libgl1-mesa-dri  # necessary for OpenGL 3D acceleration to work
+    #- libglu1-mesa  # necessary for OpenGL 3D acceleration to work
+    #- bumblebee-nvidia
+    #- primus
   - name: ensure basic X tools
     apt: name={{item}} state=present
-    with_items:
-    - xterm
-    - x11-xserver-utils  # includes xrdb which applies .Xresources files
-    - redshift 
-    - i3
-    - i3status
-    - dmenu
+    with_lines: cat ~/config/ansible/files/apt-mark/basic_x_tools | sed -e 's/ *#.*$//'
+    #with_items:
+    #- xterm
+    #- x11-xserver-utils  # includes xrdb which applies .Xresources files
+    #- redshift 
+    #- i3
+    #- i3status
+    #- suckless-tools 
 
   # Set up pentadactyl.
   - name: ensure browser environment
     apt: name={{item}} state=present
-    with_items:
-    - iceweasel
-    - xul-ext-noscript
-    - xul-ext-pentadactyl
-    - vim-gtk  # used by pentadactyl for text editing
+    with_lines: cat ~/config/ansible/files/apt-mark/browser_environment | sed -e 's/ *#.*$//'
+    #with_items:
+    #- iceweasel
+    #- 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: register number of installed packages
+#    shell: dpkg-query -Wf 'x' | wc -c
+#    register: package_count
+#  
+#
+  - name: collect desired packages
+    shell: cat files/apt-mark/*  | sed -e 's/ *#.*$//' > /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: mark all packages from white list as manually installed
+    shell: apt-mark manual $(cat /tmp/white_list_unsorted)
   - 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/3d_acceleration b/ansible/files/apt-mark/3d_acceleration
new file mode 100644
index 0000000..6d1064f
--- /dev/null
+++ b/ansible/files/apt-mark/3d_acceleration
@@ -0,0 +1,5 @@
+bumblebee-nvidia
+libgl1-mesa-dri  # necessary for OpenGL 3D acceleration to work
+libglu1-mesa  # necessary for OpenGL 3D acceleration to work
+linux-headers-amd64  # necessary to build proper nvidia-driver module
+primus
diff --git a/ansible/files/apt-mark/basic_x_tools b/ansible/files/apt-mark/basic_x_tools
new file mode 100644
index 0000000..7798e3b
--- /dev/null
+++ b/ansible/files/apt-mark/basic_x_tools
@@ -0,0 +1,6 @@
+i3
+i3status
+redshift
+suckless-tools
+xterm
+x11-xserver-utils  # includes xrdb which applies .Xresources files
diff --git a/ansible/files/apt-mark/browser_environment b/ansible/files/apt-mark/browser_environment
new file mode 100644
index 0000000..cc9575c
--- /dev/null
+++ b/ansible/files/apt-mark/browser_environment
@@ -0,0 +1,4 @@
+iceweasel
+vim-gtk  # used by pentadactyl for text editing
+xul-ext-noscript
+xul-ext-pentadactyl
diff --git a/ansible/files/apt-mark/console b/ansible/files/apt-mark/console
new file mode 100644
index 0000000..01bcbf8
--- /dev/null
+++ b/ansible/files/apt-mark/console
@@ -0,0 +1,2 @@
+console-setup
+locales
diff --git a/ansible/files/apt-mark/required b/ansible/files/apt-mark/core
similarity index 100%
rename from ansible/files/apt-mark/required
rename to ansible/files/apt-mark/core
diff --git a/ansible/files/apt-mark/man b/ansible/files/apt-mark/man
new file mode 100644
index 0000000..f688e67
--- /dev/null
+++ b/ansible/files/apt-mark/man
@@ -0,0 +1,2 @@
+man-db
+manpages
diff --git a/ansible/files/apt-mark/manual b/ansible/files/apt-mark/manual
deleted file mode 100644
index d2e1137..0000000
--- a/ansible/files/apt-mark/manual
+++ /dev/null
@@ -1,32 +0,0 @@
-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-plus b/ansible/files/apt-mark/minimal_ansible_environment
similarity index 100%
rename from ansible/files/apt-mark/required-plus
rename to ansible/files/apt-mark/minimal_ansible_environment
diff --git a/ansible/files/apt-mark/minimal_x b/ansible/files/apt-mark/minimal_x
new file mode 100644
index 0000000..f785794
--- /dev/null
+++ b/ansible/files/apt-mark/minimal_x
@@ -0,0 +1,4 @@
+libpam-systemd  # needed to start X as non-root
+xinit  # contains startx
+xserver-xorg-core
+xserver-xorg-input-evdev  # supports all input devices the kernel knows about
diff --git a/ansible/files/apt-mark/newest_kernel b/ansible/files/apt-mark/newest_kernel
new file mode 100644
index 0000000..f85a87c
--- /dev/null
+++ b/ansible/files/apt-mark/newest_kernel
@@ -0,0 +1,2 @@
+linux-image-amd64
+grub2
diff --git a/ansible/files/apt-mark/power_management b/ansible/files/apt-mark/power_management
new file mode 100644
index 0000000..ada304e
--- /dev/null
+++ b/ansible/files/apt-mark/power_management
@@ -0,0 +1,2 @@
+acpi-call-dkms
+tlp
diff --git a/ansible/files/apt-mark/sudo_git_vim_less_openssh b/ansible/files/apt-mark/sudo_git_vim_less_openssh
new file mode 100644
index 0000000..e37a898
--- /dev/null
+++ b/ansible/files/apt-mark/sudo_git_vim_less_openssh
@@ -0,0 +1,5 @@
+git
+less
+openssh-client
+sudo
+vim
-- 
2.30.2