---
- name: Apache packages present
  apt:
    pkg:
      - apache2
      - apache2-utils
      - lynx
    state: present
    update_cache: true
  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 suexec package absent
  apt:
    pkg:
      - apache2-suexec-pristine
    state: absent
    update_cache: false
  when: apache_mods_enabled is not defined or "suexec" not in apache_mods_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
  apache2_module:
    name: "{{ item }}"
    state: absent
  with_items: "{{ apache_mods_disabled }}"
  when: apache_mods_disabled is defined
  tags:
    - apache

- name: Apache modules enabled
  apache2_module:
    name: "{{ item }}"
    state: present
  with_items: "{{ apache_mods_enabled }}"
  when: apache_mods_enabled is defined
  tags:
    - apache

- name: Apache conf disabled
  command: "a2disconf {{ item }}"
  args:
    removes: /etc/apache2/conf-enabled/{{ item }}.conf
  with_items: "{{ apache_conf_disabled }}"
  when: apache_conf_disabled 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: apache2ctl_configtest
  tags:
    - apache

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

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

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