# Copyright 2018-2023 Chris Croome
#
# This file is part of the Webarchitects Apache Ansible role.
#
# The Webarchitects Apache 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 Apache 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 Apache Ansible role. If not, see <https://www.gnu.org/licenses/>.
---
- name: Enable Apache mpm_event if mpm_prefork or no MPM loaded
  block:

    - name: Include Apache checks
      ansible.builtin.include_tasks: checks.yml

    - name: Debug Apache modules loaded
      ansible.builtin.debug:
        msg: "Apache modules loaded: {{ apache_modules_loaded }}"
        verbosity: 1
      when:
        - ( apache_modules_loaded is defined ) and ( apache_modules_loaded | length > 0 )

    - name: "Disable Apache module {{ apache_phpfpm_mod }}"
      community.general.apache2_module:
        name: "{{ apache_phpfpm_mod }}"
        state: absent
      when:
        - ( apache_modules_loaded is defined ) and ( apache_modules_loaded | length > 0 )
        - ( apache_phpfpm_mod is defined ) and ( apache_phpfpm_mod in apache_modules_loaded )

    - name: Disable Apache module mpm_itk
      community.general.apache2_module:
        name: mpm_itk
        state: absent
      when: ( apache_mpm_loaded is defined ) and ( apache_mpm_loaded == "mpm_itk" )

    - name: Disable Apache module mpm_prefork
      community.general.apache2_module:
        name: mpm_prefork
        state: absent
        ignore_configcheck: true
      when: ( apache_mpm_loaded is defined ) and ( apache_mpm_loaded == "mpm_prefork" )

    - name: "Enable Apache mpm_event"
      community.general.apache2_module:
        name: mpm_event
        state: present
        ignore_configcheck: true
      register: apache_mpm_event_enabled

    - name: Restart Apache if the MPM has changed
      ansible.builtin.service:
        name: apache2
        state: restarted
      when: apache_mpm_event_enabled.changed

  when:
    - ( apache_mpm_loaded == "mpm_prefork" ) or ( apache_mpm_loaded | length == 0 )
    - ( apache_mods_enabled is defined ) and ( "mpm_event" in apache_mods_enabled )
  tags:
    - apache

- name: Enable Apache mpm_prefork if mpm_event or no MPM loaded
  block:

    - name: "Disable Apache module mpm_event"
      community.general.apache2_module:
        name: mpm_event
        state: absent
        ignore_configcheck: true
      when: ( apache_mpm_loaded is defined ) and  ( apache_mpm_loaded == "mpm_event" )

    - name: "Enable Apache mpm_prefork"
      community.general.apache2_module:
        name: mpm_prefork
        state: present
        ignore_configcheck: true
      register: apache_mpm_prefork_enabled

    - name: Restart Apache if the MPM has changed
      ansible.builtin.service:
        name: apache2
        state: restarted
      when: apache_mpm_prefork_enabled.changed

  when:
    - ( apache_mpm_loaded == "mpm_event" ) or ( apache_mpm_loaded | length == 0 )
    - ( "mpm_prefork" in apache_mods_enabled )
  tags:
    - apache

- name: Re-check the MPM
  block:

    - name: Check the enabled MPM
      ansible.builtin.command: a2query -M
      check_mode: false
      changed_when: false
      register: apache_mpm_check

    - name: Debug the enabled MPM
      ansible.builtin.debug:
        msg: "{{ apache_mpm_check.stdout }}"
        verbosity: 2

    - name: Set a fact for the enabled MPM
      ansible.builtin.set_fact:
        apache_mpm_loaded: "mpm_{{ apache_mpm_check.stdout | trim }}"
      when: apache_mpm_check.stdout != "invalid"

    - name: Fail if no Apache MPM loaded
      ansible.builtin.fail:
        msg: "No Apache MPM loaded"
      when: apache_mpm_check.stdout == "invalid"

  tags:
    - apache

- name: Re-check modules enabled
  block:

    - name: Check loaded modules
      block:

        - name: Register the loaded modules
          ansible.builtin.command: a2query -m
          check_mode: false
          changed_when: false
          register: apache_modules_check

      rescue:

        - name: No mod enabled
          ansible.builtin.set_fact:
            apache_modules_loaded: []
          when: '"No module matches" in apache_modules_check.stderr'

    - name: Debug the loaded modules
      ansible.builtin.debug:
        var: apache_modules_check.stdout_lines
        verbosity: 2
      when: apache_modules_check.stdout is defined

    - name: Set an array of the loaded modules
      ansible.builtin.set_fact:
        apache_modules_loaded: "{{ apache_modules_loaded | default([]) + [line.split(' ')[0]] }}"
      when: apache_modules_check.stdout is defined
      loop: "{{ apache_modules_check.stdout_lines }}"
      loop_control:
        loop_var: line
        label: "{{ line }}"

    - name: Debug loaded modules
      ansible.builtin.debug:
        var: apache_modules_loaded
        verbosity: 2
      when: apache_modules_loaded is defined

  tags:
    - apache
...
