---
# Ubuntu 20.04 uses PHP 7.4
# Debian 11 uses PHP 7.4
# Debian 12 uses PHP 8.2

# 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
  ansible.builtin.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
  ansible.builtin.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
  ansible.builtin.set_fact:
    install_php: false
  when: install_php is not defined

- name: Configure php-fpm on Ubuntu 20.04
  ansible.builtin.include_tasks: Ubuntu_20.04.yml
  when:
    - ansible_distribution == 'Ubuntu'
    - ansible_distribution_version is version('20.04', '==')
    - install_php
  tags: php-fpm

- name: Configure php-fpm on Debian 11
  ansible.builtin.include_tasks: Ubuntu_20.04.yml
  when:
    - ansible_distribution == 'Debian'
    - ansible_distribution_major_version is version('11', '==')
    - install_php
  tags: php-fpm

- name: Configure php-fpm on Debian 12
  ansible.builtin.include_tasks: Debian_12.yml
  when:
    - ansible_distribution == 'Debian'
    - ansible_distribution_major_version is version('12', '==')
    - install_php
  tags: php-fpm

# vim: set ts=2 sw=2: