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

    - name: Include Apache checks
      include_tasks: checks.yml

    - name: Debug Apache modules loaded
      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 }}"
      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
      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
      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"
      apache2_module:
        name: mpm_event
        state: present
        ignore_configcheck: true
      register: apache_mpm_event_enabled

    - name: Restart Apache if the MPM has changed
      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"
      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"
      apache2_module:
        name: mpm_prefork
        state: present
        ignore_configcheck: true
      register: apache_mpm_prefork_enabled

    - name: Restart Apache if the MPM has changed
      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
      command: a2query -M
      check_mode: false
      changed_when: false
      register: apache_mpm_check

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

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

    - name: Fail if no Apache MPM loaded
      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
          command: a2query -m
          check_mode: false
          changed_when: false
          register: apache_modules_check

      rescue:

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

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

    - name: Set an array of the loaded modules
      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
      debug:
        var: apache_modules_loaded
        verbosity: 2
      when: apache_modules_loaded is defined

  tags:
    - apache
...