From 141aa444f9118da33faabb521144cc1acf91107d Mon Sep 17 00:00:00 2001
From: Chris Croome <chris@webarchitects.co.uk>
Date: Sun, 28 Aug 2022 20:38:04 +0100
Subject: [PATCH] Local fact script updated

---
 meta/main.yml         |  4 +--
 tasks/checks.yml      |  4 +--
 tasks/local_facts.yml |  1 +
 tasks/main.yml        |  1 +
 templates/php.fact.j2 | 81 ++++++++++++++++++++++++-------------------
 templates/php.pref.j2 |  1 -
 6 files changed, 51 insertions(+), 41 deletions(-)

diff --git a/meta/main.yml b/meta/main.yml
index 120d601..61c5d01 100644
--- a/meta/main.yml
+++ b/meta/main.yml
@@ -3,17 +3,15 @@ galaxy_info:
   role_name: php
   author: Chris Croome
   namespace: chriscroome
-  description: Ansible role for installing and configuring PHP on Debian
+  description: Ansible role for installing and configuring PHP on Debian Bullseye and Buster
   company: Webarchitects Co-operative
   license: GNU General Public License v3.0 (GPLv3)
   min_ansible_version: 2.11
   platforms:
     - name: Debian
       versions:
-        - bookworm
         - bullseye
         - buster
-        - stretch
   galaxy_tags:
     - debian
     - php
diff --git a/tasks/checks.yml b/tasks/checks.yml
index 195b5c9..80f604d 100644
--- a/tasks/checks.yml
+++ b/tasks/checks.yml
@@ -19,11 +19,11 @@
           - php_opcache_validate_root | type_debug == "bool"
         quiet: "{% if ansible_verbosity == 0 %}true{% else %}false{% endif %}"
 
-    - name: Check that the distro is Debian Bookworm, Bullseye, Buster or Stretch or Ubuntu Jammy, Focal or Bionic
+    - name: Check that the distro is Debian Bullseye or Buster
       ansible.builtin.assert:
         that:
           - ansible_distribution_release is defined
-          - ansible_distribution_release is regex("^stretch|buster|bullseye|bookworm|focal|bionic|jammy$")
+          - ansible_distribution_release is regex("^buster|bullseye$")
         quiet: "{% if ansible_verbosity == 0 %}true{% else %}false{% endif %}"
         fail_msg: "The Linux distro {{ ansible_distribution }} {{ ansible_distribution_release }} is not supported by this role"
 
diff --git a/tasks/local_facts.yml b/tasks/local_facts.yml
index 2170cf9..0381b96 100644
--- a/tasks/local_facts.yml
+++ b/tasks/local_facts.yml
@@ -5,6 +5,7 @@
     - name: Required packages present
       ansible.builtin.apt:
         pkg:
+          - aptitude
           - jo
         state: present
 
diff --git a/tasks/main.yml b/tasks/main.yml
index 544e60f..ca5a20d 100644
--- a/tasks/main.yml
+++ b/tasks/main.yml
@@ -10,6 +10,7 @@
 
     - name: Include apt repo config for Sury PHP packages
       ansible.builtin.include_tasks: apt.yml
+      when: php_sury | bool
 
     - name: PHP versions absent
       block:
diff --git a/templates/php.fact.j2 b/templates/php.fact.j2
index 5b7dddf..48974e8 100644
--- a/templates/php.fact.j2
+++ b/templates/php.fact.j2
@@ -2,43 +2,54 @@
 # {{ ansible_managed }}
 
 set -euo pipefail
+APTITUDE="{{ php_aptitude }}"
 PHPQUERY="{{ php_phpquery }}"
 
-if [[ -f "${PHPQUERY}" ]] && [[ -f "/usr/lib/php/php-helper" ]]
+if [[ -f "${APTITUDE}" ]]
 then
-  declare -a versions=()
-  readarray -t versions < <("${PHPQUERY}" -V | sort -V)
-  jo state=present versions=$(
-    jo $(
-      for v in "${versions[@]}"
-      do
-        declare -a mods_available=()
-        readarray -t mods_available < <("${PHPQUERY}" -v "${v}" -M | sort)
-        declare -a sapis=()
-        readarray -t sapis < <("${PHPQUERY}" -v "${v}" -S | sort)
-        echo ${v}=$(
-          jo state=present mods_available=$(jo -a "${mods_available[@]}") $(
-            for s in "${sapis[@]}"
-            do
-              declare -a mods_enabled=()
-              for m in "${mods_available[@]}"
-              do
-                if "${PHPQUERY}" -v "${v}" -s "${s}" -m "${m}" -q
-                then
-                  mods_enabled=(${mods_enabled[@]} ${m})
-                fi
-              done
-              echo sapis["${s}"]=$(
-                jo state=present mods_enabled=$(
-                  jo -e -a "${mods_enabled[@]}"
-                )
+  declare -a packages=()
+  readarray -t packages < <(aptitude -q=99 search '~i!~M php' -F '%p')
+  if [[ "${#packages[@]}" == 0 ]]
+  then
+    jo state=absent
+  else
+    if [[ -f "${PHPQUERY}" ]]
+    then
+      declare -a versions=()
+      readarray -t versions < <("${PHPQUERY}" -V | sort -V)
+      jo state=present packages=$(jo -e -a "${packages[@]}") versions=$(i
+        jo $(
+          for v in "${versions[@]}"
+          do
+            declare -a mods_available=()
+            readarray -t mods_available < <("${PHPQUERY}" -v "${v}" -M | sort)
+            declare -a sapis=()
+            readarray -t sapis < <("${PHPQUERY}" -v "${v}" -S | sort)
+            echo ${v}=$(
+              jo state=present mods_available=$(jo -a "${mods_available[@]}") $(
+                for s in "${sapis[@]}"
+                do
+                  declare -a mods_enabled=()
+                  for m in "${mods_available[@]}"
+                  do
+                    if "${PHPQUERY}" -v "${v}" -s "${s}" -m "${m}" -q
+                    then
+                      mods_enabled=(${mods_enabled[@]} ${m})
+                    fi
+                  done
+                  echo sapis["${s}"]=$(
+                    jo state=present mods_enabled=$(
+                      jo -e -a "${mods_enabled[@]}"
+                    )
+                  )
+                done
               )
-            done
             )
-          )
-      done
-    )
-  )
-else
-  jo state=absent
-fi
+          done
+        )
+      )
+    else
+      jo state=absent
+    fi
+  fi
+fi  
diff --git a/templates/php.pref.j2 b/templates/php.pref.j2
index 5b07c99..aa11ce2 100644
--- a/templates/php.pref.j2
+++ b/templates/php.pref.j2
@@ -15,5 +15,4 @@ Pin-Priority: 100
 
 {% endfor %}
 {% endif %}
-
 # vim: ft=debsources
-- 
GitLab