From 81c1231a286679a952a7c0add3fef2b95947e118 Mon Sep 17 00:00:00 2001 From: Alan Orth Date: Wed, 8 Sep 2021 09:32:06 +0300 Subject: [PATCH] roles/php-fpm: Fix logic First, we cannot do a global check for has_wordpress or needs_php, as those are defined per nginx vhost. Second, I realized that this was only working in the past because vhosts that had WordPress or needed PHP were listed first in the nginx_vhosts dict. This changes the logic to first check if any vhosts have WordPress or need PHP, then sets a fact that we can use to decide whether to run php-fpm tasks or not. --- roles/php-fpm/tasks/Debian_10.yml | 3 ++- roles/php-fpm/tasks/Ubuntu_18.04.yml | 3 ++- roles/php-fpm/tasks/Ubuntu_20.04.yml | 3 ++- roles/php-fpm/tasks/main.yml | 31 ++++++++++++++++++++++++---- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/roles/php-fpm/tasks/Debian_10.yml b/roles/php-fpm/tasks/Debian_10.yml index 437d996..82cc2fa 100644 --- a/roles/php-fpm/tasks/Debian_10.yml +++ b/roles/php-fpm/tasks/Debian_10.yml @@ -28,7 +28,8 @@ - name: Update php.ini template: src=php7.3-php.ini.j2 dest=/etc/php/7.3/fpm/php.ini owner=root group=root mode=0644 notify: reload php7.3-fpm + tags: php-fpm - when: (item.has_wordpress is defined and item.has_wordpress) or (item.needs_php is defined and item.needs_php) + when: install_php # vim: set ts=2 sw=2: diff --git a/roles/php-fpm/tasks/Ubuntu_18.04.yml b/roles/php-fpm/tasks/Ubuntu_18.04.yml index d5ae6b3..a5c6cc1 100644 --- a/roles/php-fpm/tasks/Ubuntu_18.04.yml +++ b/roles/php-fpm/tasks/Ubuntu_18.04.yml @@ -28,7 +28,8 @@ - name: Update php.ini template: src=php7.2-php.ini.j2 dest=/etc/php/7.2/fpm/php.ini owner=root group=root mode=0644 notify: reload php7.2-fpm + tags: php-fpm - when: (item.has_wordpress is defined and item.has_wordpress) or (item.needs_php is defined and item.needs_php) + when: install_php # vim: set ts=2 sw=2: diff --git a/roles/php-fpm/tasks/Ubuntu_20.04.yml b/roles/php-fpm/tasks/Ubuntu_20.04.yml index 79b9cdf..84788a3 100644 --- a/roles/php-fpm/tasks/Ubuntu_20.04.yml +++ b/roles/php-fpm/tasks/Ubuntu_20.04.yml @@ -28,7 +28,8 @@ - name: Update php.ini template: src=php7.4-php.ini.j2 dest=/etc/php/7.4/fpm/php.ini owner=root group=root mode=0644 notify: reload php7.4-fpm + tags: php-fpm - when: (item.has_wordpress is defined and item.has_wordpress) or (item.needs_php is defined and item.needs_php) + when: install_php # vim: set ts=2 sw=2: diff --git a/roles/php-fpm/tasks/main.yml b/roles/php-fpm/tasks/main.yml index e8ad775..966a02f 100644 --- a/roles/php-fpm/tasks/main.yml +++ b/roles/php-fpm/tasks/main.yml @@ -4,24 +4,47 @@ # Ubuntu 20.04 uses PHP 7.4 # Debian 11 uses PHP 7.4 +# If any of the vhosts on this host need WordPress then we need to install PHP. +# This uses selectattr to filter the list of dicts in nginx_vhosts, selecting +# any that have has_wordpress defined, and has_wordpress set to True. +# +# See: https://stackoverflow.com/a/31896249 +- name: Check if any vhost needs WordPress + set_fact: + install_php: True + when: "nginx_vhosts | selectattr('has_wordpress', 'defined') | selectattr('has_wordpress', 'equalto', True) | list | length > 0" + +# Legacy, was only for Piwik, but leaving for now. +- name: Check if any vhost needs PHP + set_fact: + install_php: True + when: "nginx_vhosts | selectattr('needs_php', 'defined') | selectattr('needs_php', 'equalto', True) | list | length > 0" + +# If install_php has not been set, then we assume no vhosts need PHP. This is +# a bit hacky, but it's the closest we come to an if/then/else. +- name: Set install_php to False + set_fact: + install_php: False + when: install_php is not defined + - name: Configure php-fpm on Ubuntu 18.04 include_tasks: Ubuntu_18.04.yml - when: ansible_distribution == 'Ubuntu' and ansible_distribution_version is version('18.04', '==') + when: ansible_distribution == 'Ubuntu' and ansible_distribution_version is version('18.04', '==') and install_php tags: php-fpm - name: Configure php-fpm on Debian 10 include_tasks: Debian_10.yml - when: ansible_distribution == 'Debian' and ansible_distribution_version is version('10', '==') + when: ansible_distribution == 'Debian' and ansible_distribution_version is version('10', '==') and install_php tags: php-fpm - name: Configure php-fpm on Ubuntu 20.04 include_tasks: Ubuntu_20.04.yml - when: ansible_distribution == 'Ubuntu' and ansible_distribution_version is version('20.04', '==') + when: ansible_distribution == 'Ubuntu' and ansible_distribution_version is version('20.04', '==') and install_php tags: php-fpm - name: Configure php-fpm on Debian 11 include_tasks: Ubuntu_20.04.yml - when: ansible_distribution == 'Debian' and ansible_distribution_version is version('11', '==') + when: ansible_distribution == 'Debian' and ansible_distribution_version is version('11', '==') and install_php tags: php-fpm # vim: set ts=2 sw=2: