From 04ed21e74f5f742ccae1a02d15f59544c6d191f3 Mon Sep 17 00:00:00 2001
From: Chris Croome <chris@webarchitects.co.uk>
Date: Wed, 13 Apr 2022 17:36:45 +0100
Subject: [PATCH] debug test

---
 defaults/main.yml     |  2 +-
 tasks/absent.yml      | 12 ++---------
 tasks/checks.yml      | 11 +++++++---
 tasks/main.yml        | 12 +++++++++++
 tasks/sapi_checks.yml | 49 +++++++++++++++++++++++++++----------------
 5 files changed, 54 insertions(+), 32 deletions(-)

diff --git a/defaults/main.yml b/defaults/main.yml
index 70affad..067d88c 100644
--- a/defaults/main.yml
+++ b/defaults/main.yml
@@ -15,7 +15,7 @@ php_versions:
       phpdbg:
         state: absent
   '8.0':
-    state: present
+    state: absent
     sapis:
       apache2:
         state: absent
diff --git a/tasks/absent.yml b/tasks/absent.yml
index 125a8e2..ece2cc5 100644
--- a/tasks/absent.yml
+++ b/tasks/absent.yml
@@ -2,26 +2,18 @@
 - name: PHP packages absent
   block:
 
-    - name: PHP packages absent
+    - name: "PHP {{ version }} packages absent"
       apt:
         pkg: "php{{ version }}*"
         state: absent
         purge: true
         autoclean: true
         autoremove: true
-      loop: "{{ php_versions_remove }}"
-      loop_control:
-        loop_var: version
-        label: "{{ version }}"
 
-    - name: PHP config files absent
+    - name: "PHP {{ version }} config files absent"
       file:
         path: "/etc/php/{{ version }}/"
         state: absent
-      loop: "{{ php_versions_remove }}"
-      loop_control:
-        loop_var: version
-        label: "{{ version }}"
 
   tags:
     - php
diff --git a/tasks/checks.yml b/tasks/checks.yml
index e26ba2a..bea03fd 100644
--- a/tasks/checks.yml
+++ b/tasks/checks.yml
@@ -2,9 +2,6 @@
 - name: PHP checks
   block:
 
-    # TODO
-    # - Check that when a version SAPI is set to present the version is also set to present
-    # - Check that when a version is set to absent the SAPIs are also set to absent, if the state is set
     - name: Check that depreciated variables are not used
       assert:
         that:
@@ -64,6 +61,14 @@
         var: php_versions_remove
         verbosity: 2
 
+    - name: Include SAPI checks for PHP proposed versions
+      include_tasks: sapi_checks.yml
+      loop: "{{ php_versions_remove }}"
+      loop_control:
+        loop_var: version
+        label: "{{ version }}"
+      when: ( php_versions_remove is defined ) and ( php_versions_remove != [] )
+
     # - name: Include SAPI checks for PHP proposed versions
     #   include_tasks: sapi_checks.yml
     #   loop: "{{ php_proposed_versions }}"
diff --git a/tasks/main.yml b/tasks/main.yml
index b9626a2..b250646 100644
--- a/tasks/main.yml
+++ b/tasks/main.yml
@@ -13,8 +13,20 @@
 
     - name: Include package removal tasks
       include_tasks: absent.yml
+      loop: "{{ php_versions_remove }}"
+      loop_control:
+        loop_var: version
+        label: "{{ version }}"
       when: ( php_versions_remove is defined ) and ( php_versions_remove != [] )
 
+    - name: Include package install tasks
+      include_tasks: present.yml
+      loop: "{{ php_versions_install }}"
+      loop_control:
+        loop_var: version
+        label: "{{ version }}"
+      when: ( php_versions_install is defined ) and ( php_versions_install != [] )
+
     - name: Debug fail
       fail:
 
diff --git a/tasks/sapi_checks.yml b/tasks/sapi_checks.yml
index f92d157..fc97e03 100644
--- a/tasks/sapi_checks.yml
+++ b/tasks/sapi_checks.yml
@@ -7,26 +7,39 @@
         var: version
         verbosity: 2
 
-    - name: "Debug existing SAPIs for PHP {{ version }}"
-      debug:
-        var: ansible_local.phpquery.versions[version].sapis
-        verbosity: 3
-      when: ansible_local.phpquery.versions[version].sapis is defined
-
-    - name: "Set an array for the existing SAPIs for PHP {{ version }}" 
-      set_fact:
-        php_existing_sapis: "{{ php_existing_sapis | default([]) }} + [ '{{ existing_sapi.key }}' ]"
-      when: 
-        - ansible_local.phpquery.versions[version].sapis is defined
-        - existing_sapi.value.state == "present"
-      loop: "{{ ansible_local.phpquery.versions[version].sapis | dict2items }}"
+    - name: "Check that no SAPI is set to be present when PHP {{ version }} is set to be removed"
+      assert:
+        that:
+          - sapi.state == absent )
+      loop: "{{ php_versions[version].sapis }}"
       loop_control:
-        loop_var: existing_sapi
-        label: "{{ existing_sapi.key }}"
+        loop_var: sapi
+        label: 
+      when:
+        - php_versions[version].sapis is defined 
+        - php_versions[version].sapis.sapi is defined 
+        - php_versions[version].sapis.sapi.state is defined 
 
-    - name: "Debug existing SAPIs for PHP {{ version }}"
-      debug:
-        var: php_existing_sapis
+    # - name: "Debug existing SAPIs for PHP {{ version }}"
+    #   debug:
+    #     var: ansible_local.phpquery.versions[version].sapis
+    #     verbosity: 3
+    #   when: ansible_local.phpquery.versions[version].sapis is defined
+
+    # - name: "Set an array for the existing SAPIs for PHP {{ version }}" 
+    #   set_fact:
+    #     php_existing_sapis: "{{ php_existing_sapis | default([]) }} + [ '{{ existing_sapi.key }}' ]"
+    #   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
+    #     label: "{{ existing_sapi.key }}"
+
+    # - name: "Debug existing SAPIs for PHP {{ version }}"
+    #   debug:
+    #     var: php_existing_sapis
 
     # - name: Generate an array of proposed PHP SAPIs 
     #   set_fact:
-- 
GitLab