---
- name: apache2 install apache
  become: true
  tags: apache2
  ansible.builtin.apt:
    name: apache2
    state: present

- name: create private folder for ssl certs
  become: true
  tags: apache2
  ansible.builtin.file:
    path: /etc/apache2/private
    state: directory

- name: apache2 install php
  become: true
  tags: apache2
  ansible.builtin.apt:
    name: "{{ item }}"
    state: present
  with_items:
    - "{{ php_version }}"
    - "{{ php_version }}-bcmath"
    - "{{ php_version }}-cli"
    - "{{ php_version }}-common"
    - "{{ php_version }}-curl"
    - "{{ php_version }}-gd"
    - "{{ php_version }}-gmp"
    - "{{ php_version }}-intl"
#   - "{{ php_version }}-json"
    - "{{ php_version }}-mbstring"
    - "{{ php_version }}-opcache"
    - "{{ php_version }}-pgsql"
    - "{{ php_version }}-readline"
    - "{{ php_version }}-xml"
    - "{{ php_version }}-zip"
  when: php_version is defined

- name: apache2 install php-json
  become: true
  tags: apache2
  ansible.builtin.apt:
    name: "{{ item }}"
    state: present
  with_items:
    - "{{ php_version }}-json"
  when:
    - (php_version is defined) and (not "bookworm" in ansible_distribution_release)


- name: apache2 create symlinks conf-enabled
  become: true
  tags: apache2
  ansible.builtin.command:
    cmd: "a2enconf {{ item }}"
  with_items:
    - charset
    - localized-error-pages
    - security
    - other-vhosts-access-log
    - serve-cgi-bin
  notify: restart apache2

- name: apache2 enable modules
  become: true
  tags: apache2
  ansible.builtin.command:
    cmd: "a2enmod {{ item }}"
  with_items:
    - headers
    - proxy
    - proxy_http
    - ssl
    - rewrite
  notify: restart apache2

- name: apache2 create sites-available files
  become: true
  tags: apache2
  template:
    src: "{{ item }}.j2"
    dest: "/etc/apache2/sites-available/{{ item }}"
  with_items: "{{ apache2_sites }}"

- name: apache2 create symlinks sites-enabled
  become: true
  tags: apache2
  ansible.builtin.file:
    src: "../sites-available/{{ item }}"
    dest: "/etc/apache2/sites-enabled/{{ item }}"
    state: link
  with_items: "{{ apache2_sites }}"

- name: apache2 delete default sites conf
  become: true
  tags: apache2
  ansible.builtin.command:
    cmd: "a2dissite {{ item }}"
  with_items:
    - "000-default.conf"
    - "default-ssl.conf"
  failed_when: false
  ignore_errors: true

- name: apache2 a2enmod create symlink for php
  become: true
  tags: apache2
  ansible.builtin.command:
    cmd: "a2enmod {{ php_version }}"
  when: php_version is defined
