---
-
  name: Configure Apache.
  ansible.builtin.lineinfile:
    dest: "{{ apache_server_root }}/conf/{{ apache_daemon }}.conf"
    regexp: "{{ item.regexp }}"
    line: "{{ item.line }}"
    state: present
    mode: "0644"
  with_items: "{{ apache_ports_configuration_items }}"
  notify: restart apache

-
  name: Check whether certificates defined in vhosts exist.
  ansible.builtin.stat:
    path: "{{ item.certificate_file }}"
  register: apache_ssl_certificates
  with_items: "{{ apache_vhosts_ssl }}"
  no_log: "{{ apache_ssl_no_log }}"

-
  name: Enable Apache mods.
  ansible.builtin.copy:
    dest: "{{ apache_server_root }}/conf.modules.d/99-ansible-{{ item }}.conf"
    content: |
      LoadModule {{ item }}_module modules/mod_{{ item }}.so
    mode: "0644"
  with_items: "{{ apache_mods_enabled }}"
  notify: restart apache

-
  name: Disable Apache mods
  ansible.builtin.file:
    path: "{{ apache_server_root }}/conf.modules.d/99-ansible-{{ item }}.conf"
    state: absent
  with_items: "{{ apache_mods_disabled }}"
  notify: restart apache

-
  name: Add apache vhosts configuration.
  ansible.builtin.template:
    src: "{{ apache_vhosts_template }}"
    dest: "{{ apache_conf_path }}/{{ apache_vhosts_filename }}"
    owner: root
    group: root
    mode: "0644"
  notify: restart apache
  when: apache_create_vhosts | bool

-
  name: Check if localhost cert exists (RHEL 8 and later).
  ansible.builtin.stat:
    path: /etc/pki/tls/certs/localhost.crt
  register: localhost_cert
  when: ansible_distribution_major_version | int >= 8

-
  name: Ensure httpd certs are installed (RHEL 8 and later).
  ansible.builtin.command: /usr/libexec/httpd-ssl-gencerts
  changed_when: false
  when:
    - ansible_distribution_major_version | int >= 8
    - not localhost_cert.stat.exists
