From f96467d5b293bb9a2ad41ec751892e874e5c0553 Mon Sep 17 00:00:00 2001
From: Chris Croome <chris@webarchitects.co.uk>
Date: Tue, 19 Apr 2022 11:14:40 +0100
Subject: [PATCH] Checks for packages present and absent added

---
 defaults/main.yml     | 26 +++++++++++++-------------
 tasks/checks.yml      | 10 +++++++++-
 tasks/local_facts.yml |  4 ++--
 tasks/pkg_checks.yml  | 33 +++++++++++++++++++++++++++++++++
 tasks/sapi_checks.yml | 19 +++++++++++++++----
 5 files changed, 72 insertions(+), 20 deletions(-)
 create mode 100644 tasks/pkg_checks.yml

diff --git a/defaults/main.yml b/defaults/main.yml
index 95488ae..0f3a0f1 100644
--- a/defaults/main.yml
+++ b/defaults/main.yml
@@ -5,15 +5,6 @@ php_phpquery: /usr/sbin/phpquery
 php_versions:
   '8.1':
     state: present
-    sapis:
-      apache2:
-        state: absent
-      cli:
-        state: present
-      fpm:
-        state: present
-      phpdbg:
-        state: absent
     packages_absent:
       - libapache2-mod-php8.1
       - php8.1-phpdbg
@@ -34,8 +25,6 @@ php_versions:
       - php8.1-uploadprogress
       - php8.1-xml
       - php8.1-xsl
-  '8.0':
-    state: present
     sapis:
       apache2:
         state: absent
@@ -45,6 +34,8 @@ php_versions:
         state: present
       phpdbg:
         state: absent
+  '8.0':
+    state: present
     packages_absent:
       - libapache2-mod-php8.0
       - php8.0-phpdbg
@@ -65,8 +56,6 @@ php_versions:
       - php8.0-uploadprogress
       - php8.0-xml
       - php8.0-xsl
-  '7.4':
-    state: present
     sapis:
       apache2:
         state: absent
@@ -76,6 +65,8 @@ php_versions:
         state: present
       phpdbg:
         state: absent
+  '7.4':
+    state: present
     packages_absent:
       - libapache2-mod-php7.4
       - php7.4-phpdbg
@@ -99,6 +90,15 @@ php_versions:
       - php7.4-uploadprogress
       - php7.4-xml
       - php7.4-xsl
+    sapis:
+      apache2:
+        state: absent
+      cli:
+        state: present
+      fpm:
+        state: present
+      phpdbg:
+        state: absent
   '7.3':
     state: absent
   '7.2':
diff --git a/tasks/checks.yml b/tasks/checks.yml
index 9751b51..8f19521 100644
--- a/tasks/checks.yml
+++ b/tasks/checks.yml
@@ -51,6 +51,14 @@
         var: php_versions_proposed
         verbosity: 2
 
+    - name: Check that for each proposed version of PHP there are not packages due to be absent and present at the same time
+      include_tasks: pkg_checks.yml
+      loop: "{{ php_versions_proposed }}"
+      loop_control:
+        loop_var: version
+        label: "{{ version }}"
+      when: ( php_versions_proposed is defined ) and ( php_versions_proposed != [] )
+
     - name: Generate an array of PHP versions to remove
       set_fact:
         php_versions_remove: "{{ php_versions_existing | difference(php_versions_proposed) }}"
@@ -83,7 +91,7 @@
         var: php_versions_absent
         verbosity: 2
 
-    - name: Include SAPI checks for PHP versions due to be absentd
+    - name: Include SAPI checks for PHP versions due to be absent
       include_tasks: sapi_checks.yml
       loop: "{{ php_versions_absent }}"
       loop_control:
diff --git a/tasks/local_facts.yml b/tasks/local_facts.yml
index 5ea60e8..618600a 100644
--- a/tasks/local_facts.yml
+++ b/tasks/local_facts.yml
@@ -42,12 +42,12 @@
 
     - name: Print Ansible dpkg --architecture variables
       debug:
-        msg: "{{ ansible_facts['ansible_local']['dpkg_arch'] }}"
+        msg: "{{ ansible_local.dpkg_arch }}"
         verbosity: 2
 
     - name: Print Ansible phpquery dictionary
       debug:
-        msg: "{{ ansible_facts['ansible_local']['phpquery'] }}"
+        msg: "{{ ansible_local.phpquery }}"
         verbosity: 2
 
   tags:
diff --git a/tasks/pkg_checks.yml b/tasks/pkg_checks.yml
new file mode 100644
index 0000000..dc2c3e9
--- /dev/null
+++ b/tasks/pkg_checks.yml
@@ -0,0 +1,33 @@
+---
+- name: PHP versions package checks
+  block:
+
+    - name: "Check that a package is not set to be present and absent at the same time for PHP {{ version }}"
+      assert:
+        that:
+          - pkg not in php_versions[version].packages_absent
+        fail_msg: "Package {{ pkg }} is set to be both present and absent at the same time for PHP {{ version }}"
+      loop: "{{ php_versions[version].packages_present }}"
+      loop_control:
+        loop_var: pkg
+        label: "{{ pkg }}"
+      when:
+        - ( php_versions[version].packages_absent is defined ) and ( php_versions[version].packages_absent != [] )
+        - ( php_versions[version].packages_present is defined ) and ( php_versions[version].packages_present != [] )
+
+    - name: "Check that a package is not set to be absent and present at the same time for PHP {{ version }}"
+      assert:
+        that:
+          - pkg not in php_versions[version].packages_present
+        fail_msg: "Package {{ pkg }} is set to be both absent and present at the same time for PHP {{ version }}"
+      loop: "{{ php_versions[version].packages_absent }}"
+      loop_control:
+        loop_var: pkg
+        label: "{{ pkg }}"
+      when:
+        - ( php_versions[version].packages_absent is defined ) and ( php_versions[version].packages_absent != [] )
+        - ( php_versions[version].packages_present is defined ) and ( php_versions[version].packages_present != [] )
+
+  tags:
+    - php
+...
diff --git a/tasks/sapi_checks.yml b/tasks/sapi_checks.yml
index 85b948a..61f6346 100644
--- a/tasks/sapi_checks.yml
+++ b/tasks/sapi_checks.yml
@@ -2,14 +2,25 @@
 - name: PHP SAPI checks
   block:
 
-    - name: "Include PHP {{ version }} SAPI state checks"
-      include_tasks: sapi_state_checks.yml
+    - name: "Check that SAPI is not set to be present when PHP {{ version }} is set to be removed"
+      assert:
+        that:
+          - sapi.value.state == "absent"
+        fail_msg: "SAPI {{ sapi.key }} is not set to be absent for PHP {{ version }}, yet PHP {{ version }} is set to be removed."
       loop: "{{ php_versions[version].sapis | dict2items }}"
       loop_control:
         loop_var: sapi
         label: "{{ sapi.key }}"
-      when:
-        - php_versions[version].sapis is defined
+      when: php_versions[version].sapis is defined
+
+#    - name: "Include PHP {{ version }} SAPI state checks"
+#      include_tasks: sapi_state_checks.yml
+#      loop: "{{ php_versions[version].sapis | dict2items }}"
+#      loop_control:
+#        loop_var: sapi
+#        label: "{{ sapi.key }}"
+#      when:
+#        - php_versions[version].sapis is defined
     #     - php_versions[version].sapis.sapi is defined
     #     - php_versions[version].sapis.sapi.state is defined
 
-- 
GitLab