From a1128ee0bd4371a5492a5ac558588107c7dc1174 Mon Sep 17 00:00:00 2001
From: Chris Croome <chris@webarchitects.co.uk>
Date: Wed, 13 Apr 2022 16:32:05 +0100
Subject: [PATCH] Test remove packages

---
 defaults/main.yml     |   2 +-
 tasks/absent.yml      |  16 +++++++
 tasks/checks.yml      | 106 +++++++++++++++++++++---------------------
 tasks/main.yml        |   3 ++
 tasks/sapi_checks.yml |   4 +-
 5 files changed, 76 insertions(+), 55 deletions(-)
 create mode 100644 tasks/absent.yml

diff --git a/defaults/main.yml b/defaults/main.yml
index 30386fb..c33107c 100644
--- a/defaults/main.yml
+++ b/defaults/main.yml
@@ -4,7 +4,7 @@ php_dpkg: /usr/bin/dpkg
 php_phpquery: /usr/sbin/phpquery
 php_versions:
   '8.1':
-    state: present
+    state: absent
     sapis:
       apache2:
         state: absent
diff --git a/tasks/absent.yml b/tasks/absent.yml
new file mode 100644
index 0000000..0d5fa03
--- /dev/null
+++ b/tasks/absent.yml
@@ -0,0 +1,16 @@
+---
+- name: PHP packages absent
+  block:
+
+    - name: PHP packages absent
+      apt:
+        pkg: "php{{ version }}*"
+        state: absent
+      loop: "{{ php_versions_remove }}"
+      loop_control:
+        loop_var: version
+        label: "php{{ version }}"
+
+  tags:
+    - php
+...
diff --git a/tasks/checks.yml b/tasks/checks.yml
index 2c260a4..fe1fe90 100644
--- a/tasks/checks.yml
+++ b/tasks/checks.yml
@@ -61,59 +61,59 @@
         var: php_versions_remove
         verbosity: 2
 
-    - name: Include SAPI checks for PHP proposed versions
-      include_tasks: sapi_checks.yml
-      loop: "{{ php_proposed_versions }}"
-      loop_control:
-        loop_var: version
-        label: "{{ version }}"
-      when: ( php_proposed_versions is defined ) and ( php_proposed_versions != [] )
-
-    - name: Debug fail
-      fail:
-
-    - name: Include version checks
-      include_tasks: version_checks.yml 
-      loop: "{{ php_versions | dict2items }}"
-      loop_control:
-        loop_var: proposed_version
-        label: "{{ proposed_version.key }}"
-
-    - name: Set an array for the PHP packages with version numbers in their names
-      set_fact:
-        php_version_packages: "{{ php_version_packages | default([]) }} + [ 'php{{ php_version }}-{{ php_package }}' ]"
-      loop: "{{ php_versioned_packages }}"
-      loop_control:
-        loop_var: php_package
-        label: "{{ php_package }}"
-
-    - name: Check and enable the www pool
-      block:
-
-        - name: Include www pool server variable checks
-          include_tasks: pool_check.yml
-          when: php_www_pool_pm == "dynamic"
-          vars:
-            php_pm: "{{ php_www_pool_pm }}"
-            php_pm_max_children: "{{ php_www_pool_pm_max_children }}"
-            php_pm_start_servers: "{{ php_www_pool_pm_start_servers }}"
-            php_pm_min_spare_servers: "{{ php_www_pool_pm_min_spare_servers }}"
-            php_pm_max_spare_servers: "{{ php_www_pool_pm_max_spare_servers }}"
-
-      when: ( php_www_pool_enabled is not defined ) or ( php_www_pool_enabled is defined and php_www_pool_enabled )
-
-    - name: Check the PHP-FPM configuration
-      command: "php-fpm{{ php_version }} --test"
-      check_mode: false
-      changed_when: false
-      register: php_test
-      failed_when: ( "test is successful" not in php_test.stderr )
-      notify: Restart php-fpm
-
-    - name: Check that /usr/bin/crontab exists
-      stat:
-        path: /usr/bin/crontab
-      register: php_crontab
+    # - name: Include SAPI checks for PHP proposed versions
+    #   include_tasks: sapi_checks.yml
+    #   loop: "{{ php_proposed_versions }}"
+    #   loop_control:
+    #     loop_var: version
+    #     label: "{{ version }}"
+    #   when: ( php_proposed_versions is defined ) and ( php_proposed_versions != [] )
+
+    # - name: Debug fail
+    #   fail:
+
+    # - name: Include version checks
+    #   include_tasks: version_checks.yml 
+    #   loop: "{{ php_versions | dict2items }}"
+    #   loop_control:
+    #     loop_var: proposed_version
+    #     label: "{{ proposed_version.key }}"
+
+    # - name: Set an array for the PHP packages with version numbers in their names
+    #   set_fact:
+    #     php_version_packages: "{{ php_version_packages | default([]) }} + [ 'php{{ php_version }}-{{ php_package }}' ]"
+    #   loop: "{{ php_versioned_packages }}"
+    #   loop_control:
+    #     loop_var: php_package
+    #     label: "{{ php_package }}"
+
+    # - name: Check and enable the www pool
+    #   block:
+
+    #     - name: Include www pool server variable checks
+    #       include_tasks: pool_check.yml
+    #       when: php_www_pool_pm == "dynamic"
+    #       vars:
+    #         php_pm: "{{ php_www_pool_pm }}"
+    #         php_pm_max_children: "{{ php_www_pool_pm_max_children }}"
+    #         php_pm_start_servers: "{{ php_www_pool_pm_start_servers }}"
+    #         php_pm_min_spare_servers: "{{ php_www_pool_pm_min_spare_servers }}"
+    #         php_pm_max_spare_servers: "{{ php_www_pool_pm_max_spare_servers }}"
+
+    #   when: ( php_www_pool_enabled is not defined ) or ( php_www_pool_enabled is defined and php_www_pool_enabled )
+
+    # - name: Check the PHP-FPM configuration
+    #   command: "php-fpm{{ php_version }} --test"
+    #   check_mode: false
+    #   changed_when: false
+    #   register: php_test
+    #   failed_when: ( "test is successful" not in php_test.stderr )
+    #   notify: Restart php-fpm
+
+    # - name: Check that /usr/bin/crontab exists
+    #   stat:
+    #     path: /usr/bin/crontab
+    #   register: php_crontab
 
   tags:
     - php
diff --git a/tasks/main.yml b/tasks/main.yml
index d5d4ed7..60c551e 100644
--- a/tasks/main.yml
+++ b/tasks/main.yml
@@ -11,6 +11,9 @@
     - name: Include apt repo config for Sury PHP packages
       include_tasks: sury.yml
 
+    - name: Include package removal tasks
+      include_tasks: absent.yml
+
     - name: Debug fail
       fail:
 
diff --git a/tasks/sapi_checks.yml b/tasks/sapi_checks.yml
index 60b9713..f92d157 100644
--- a/tasks/sapi_checks.yml
+++ b/tasks/sapi_checks.yml
@@ -16,7 +16,9 @@
     - name: "Set an array for the existing SAPIs for PHP {{ version }}" 
       set_fact:
         php_existing_sapis: "{{ php_existing_sapis | default([]) }} + [ '{{ existing_sapi.key }}' ]"
-      when: existing_sapi.value.state == "present"
+      when: 
+        - ansible_local.phpquery.versions[version].sapis is defined
+        - existing_sapi.value.state == "present"
       loop: "{{ ansible_local.phpquery.versions[version].sapis | dict2items }}"
       loop_control:
         loop_var: existing_sapi
-- 
GitLab