# 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: Debug Apache modules enabled
      ansible.builtin.debug:
        var: ansible_local.a2query.modules_enabled
        verbosity: 1

    # The Ansible module does not to work here so use command
    # - name: "Disable Apache module {{ apache_phpfpm_mod }}"
    #   community.general.apache2_module:
    #     name: "{{ apache_phpfpm_mod }}"
    #     state: absent
    #   when:
    #     - apache_phpfpm_mod is defined
    #     - apache_phpfpm_mod in ansible_local.a2query.modules_enabled

    - name: "Disable Apache module {{ apache_phpfpm_mod }}"
      ansible.builtin.command: "a2dismod {{ apache_phpfpm_mod }}"
      args:
        removes: "/etc/apache2/mods-enabled/{{ apache_phpfpm_mod }}.load"
      when:
        - apache_phpfpm_mod is defined
        - apache_phpfpm_mod in ansible_local.a2query.modules_enabled

    - name: Disable Apache module mpm_prefork
      community.general.apache2_module:
        name: mpm_prefork
        state: absent
        ignore_configcheck: true
      when: ansible_local.a2query.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:
    - ansible_local.a2query.mpm == "prefork"
    - ( "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: ansible_local.a2query.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:
    - ansible_local.a2query.mpm == "event"
    - ( "mpm_prefork" in apache_mods_enabled )
  tags:
    - apache

- name: Re-read Ansible local facts
  ansible.builtin.setup:
    filter: ansible_local
  tags:
    - apache
...