From 8621455a165ce18ea83613358f9c934ca8b0f40e Mon Sep 17 00:00:00 2001
From: Chris Croome <chris@webarchitects.co.uk>
Date: Sun, 26 Mar 2023 09:52:21 +0100
Subject: [PATCH] Only restart PHP-FPM when init system is systemd

---
 tasks/check_init.yml     | 39 +++++++++++++++++++++++++++++++++++++++
 tasks/file_edited.yml    |  3 ++-
 tasks/file_templated.yml |  3 ++-
 tasks/main.yml           |  9 +++++++++
 4 files changed, 52 insertions(+), 2 deletions(-)
 create mode 100644 tasks/check_init.yml

diff --git a/tasks/check_init.yml b/tasks/check_init.yml
new file mode 100644
index 0000000..200ffe8
--- /dev/null
+++ b/tasks/check_init.yml
@@ -0,0 +1,39 @@
+# Copyright 2019-2023 Chris Croome
+#
+# This file is part of the Webarchitects PHP Ansible role.
+#
+# The Webarchitects PHP Ansible role is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+#
+# The Webarchitects PHP Ansible role is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along with the Webarchitects PHP Ansible role. If not, see <https://www.gnu.org/licenses/>.
+---
+- name: Check the init system
+  block:
+
+    # When in a docker container php_init.stdout == "sh"
+    - name: Check if system has been booted with systemd as init system
+      ansible.builtin.command: ps -p 1 -o comm=
+      changed_when: false
+      check_mode: false
+      register: php_ps
+      failed_when: php_ps.rc is not regex('^0|47$')
+
+    - name: Set a fact for the init system
+      ansible.builtin.set_fact:
+        php_init: "{{ php_ps.stdout }}"
+      when: php_ps.rc == 0
+
+    - name: When the exit code is 47 assume is is because this is a chroot
+      ansible.builtin.set_fact:
+        php_init: "{{ php_ps.stdout }}"
+      when: php_ps.rc == 47
+
+    - name: Debug the ini system
+      ansible.builtin.debug:
+        var: php_init
+      verbosity: "{% if ansible_check_mode | bool %}0{% else %}1{% endif %}"
+
+  tags:
+    - php
+...
diff --git a/tasks/file_edited.yml b/tasks/file_edited.yml
index 95b3cb6..264bf96 100644
--- a/tasks/file_edited.yml
+++ b/tasks/file_edited.yml
@@ -110,7 +110,7 @@
         owner: root
         group: root
 
-    - name: Test and reload PHP configuration file when file is in a FPM directory
+    - name: Test and reload PHP configuration file when file is in a FPM directory and the init system is systemd
       block:
 
         - name: Test PHP configuration
@@ -159,6 +159,7 @@
             state: reloaded
 
       when:
+        - php_init == "systemd"
         - php_conf_file_version in php_ver_installed
         - php_conf_file_changed | bool
         - php_conf_file_sapi is defined
diff --git a/tasks/file_templated.yml b/tasks/file_templated.yml
index f9c4d47..96f2677 100644
--- a/tasks/file_templated.yml
+++ b/tasks/file_templated.yml
@@ -64,7 +64,7 @@
         group: root
       register: php_conf_file_templated
 
-    - name: Test and reload PHP configuration file when file is in a FPM directory
+    - name: Test and reload PHP configuration file when file is in a FPM directory and the init system is systemd
       block:
 
         - name: Test PHP configuration
@@ -113,6 +113,7 @@
             state: reloaded
 
       when:
+        - php_init == "systemd"
         - php_conf_file_version is defined
         - php_conf_file_version in php_ver_installed
         - php_conf_file_templated.changed is defined
diff --git a/tasks/main.yml b/tasks/main.yml
index 782909c..601a2f0 100644
--- a/tasks/main.yml
+++ b/tasks/main.yml
@@ -43,6 +43,15 @@
         - php_cfg
         - php_conf
 
+    - name: Include the init system check tasks
+      ansible.builtin.include_tasks: check_vars.yml
+      tags:
+        - php_apt
+        - php_cfg
+        - php_conf
+        - php_mods
+        - php_pkg
+
     - name: Include Sury PHP apt repo tasks
       ansible.builtin.include_tasks: apt.yml
       when: php_sury is defined
-- 
GitLab