---
- name: Apache packages present
  apt:
    pkg:
      - apache2
      - apache2-utils
      - lynx
    state: present
    update_cache: true
  tags:
    - apache

- name: Apache envars in place
  template:
    src: templates/envvars.j2
    dest: /etc/apache2/envvars
    mode: 0755
    owner: root
    group: root
  tags:
    - apache

- name: Apache ITK MPM package absent
  apt:
    pkg:
      - libapache2-mpm-itk
    state: absent
    update_cache: false
  when: ( apache_mods_enabled is defined ) and ( "mpm-itk" in apache_mods_disabled )
  tags:
    - apache

- name: Apache ITK MPM configuration
  block:

    - name: Apache ITK MPM package present
      apt:
        pkg:
          - libapache2-mpm-itk
        state: present
        update_cache: false
      tags:
        - apache

    - name: Apache mpm_prefork.conf in place
      template:
        src: templates/mpm_prefork.conf.j2
        dest: /etc/apache2/mods-available/mpm_prefork.conf
      tags:
        - apache

  when: ( apache_mods_enabled is defined ) and ( "mpm-itk" in apache_mods_enabled )

- name: Apache suEXEC package absent
  apt:
    pkg:
      - apache2-suexec-pristine
    state: absent
    update_cache: false
  when: ( apache_mods_enabled is defined ) and ( "suexec" in apache_mods_disabled )
  tags:
    - apache

- name: Apache Suexec package present
  apt:
    pkg:
      - apache2-suexec-pristine
    state: present
    update_cache: false
  when: ( apache_mods_enabled is defined ) and ( "suexec" in apache_mods_enabled )
  tags:
    - apache

- name: Apache FastCGI absent
  apt:
    pkg:
      - libapache2-mod-fcgid
    state: absent
    update_cache: false
  when: ( apache_mods_enabled is defined ) and ( "fcgid" not in apache_mods_disabled )
  tags:
    - apache

- name: Apache FastCGI present
  apt:
    pkg:
      - libapache2-mod-fcgid
    state: present
    update_cache: false
  when: ( apache_mods_enabled is defined ) and ( "fcgid" in apache_mods_enabled )
  tags:
    - apache

- name: Apache ratelimit conf present
  template:
    src: templates/ratelimit.conf.j2
    dest: /etc/apache2/mods-available/ratelimit.conf
  when: ( apache_mods_enabled is defined ) and ( "ratelimit" in apache_mods_enabled )
  tags:
    - apache

- name: Check we we have a HTTPS cert
  stat:
    path: "/etc/ssl/le/{{ inventory_hostname }}.cert.pem"
  register: apache_cert
  tags:
    - apache

- name: Conditionally include Webarchitects config
  include_tasks: webarch.yml
  when: ( apache_conf_enabled is defined ) and ( "webarch" in apache_conf_enabled )
  tags:
    - apache

- name: Custom Apache ssl.conf in place
  copy:
    src: files/ssl.conf
    dest: /etc/apache2/mods-available/ssl.conf
  tags:
    - apache

- name: Apache modules disabled
  include_tasks: a2dismod.yml
  with_items: "{{ apache_mods_disabled }}"
  when: apache_mods_disabled is defined
  tags:
    - apache

- name: Apache modules enabled
  include_tasks: a2enmod.yml
  with_items: "{{ apache_mods_enabled }}"
  when: apache_mods_enabled is defined
  tags:
    - apache

- name: Apache conf disabled
a include_tasks: a2disconf.yml
  with_items: "{{ apache_conf_disabled }}"
  when: apache_conf_disabled is defined
  tags:
    - apache

- name: Apache conf enabled
  include_tasks: a2enconf.yml
  with_items: "{{ apache_conf_enabled }}"
  when: apache_conf_enabled is defined
  tags:
    - apache

- name: Apache localhost directory in place
  file:
    path: /var/www/localhost
    state: directory
  tags:
    - apache

- name: Apache localhost.conf in place
  copy:
    src: files/localhost.conf
    dest: /etc/apache2/sites-available/localhost.conf
  tags:
    - apache

- name: Apache localhost enabled
  command: a2ensite localhost
  args:
    creates: /etc/apache2/sites-enabled/localhost.conf
  tags:
    - apache

- name: Apache configtest
  command: apache2ctl configtest
  register: apache_configtest
  changed_when: '"Syntax OK" not in apache_configtest.stderr'
  tags:
    - apache

- debug:
    msg: "{{ apache_configtest.stderr }}"
    verbosity: 1
  tags:
    - apache

- name: Apache restarted
  service:
    name: apache2
    state: restarted
  when: '"Syntax OK" in apache_configtest.stderr'
  tags:
    - apache

- name: Fail if Apache configtest is not OK
  fail:
    msg: "{{ apache2ctl_configtest.stdout }}"
  when: '"Syntax OK" not in apache_configtest.stderr'
  tags:
    - apache
