---
- name: Prepare
  hosts: all

  pre_tasks:
    - name: "Installing packages on CentOS"
      ansible.builtin.yum:
        name:
          - net-tools
          - which
          - sudo
        state: present
      register: installation_dependencies
      until: installation_dependencies is succeeded
      when:
        - ansible_os_family == 'RedHat'

    - name: "Apt update"
      ansible.builtin.shell: "apt-get update"
      register: installation_dependencies
      until: installation_dependencies is succeeded
      when: ansible_facts['os_family'] == 'Debian'

    - name: "Installing packages on Debian family"
      ansible.builtin.apt:
        name:
          - net-tools
          - apt-utils
          - python3-pip
          - gpg-agent
          - sudo
          - curl
        update_cache: true
        state: present
      register: installation_dependencies
      until: installation_dependencies is succeeded
      when: ansible_facts['os_family'] == 'Debian'

    - name: "Installing packages on Suse"
      community.general.zypper:
        name:
          - iproute2
          - python3-pip
          - sudo
          - curl
        update_cache: true
        state: present
      register: installation_dependencies
      until: installation_dependencies is succeeded
      when: ansible_facts['os_family'] == 'Suse'

    - name: "Configure SUDO."
      ansible.builtin.lineinfile:
        dest: /etc/sudoers
        line: "Defaults    !requiretty"
        state: present

    - name: Enabling PHP 8.0
      ansible.builtin.dnf:
        name: "@php:8.0/common"
      when:
        - ansible_facts['os_family'] == 'RedHat'
        - ansible_distribution_major_version == "8"

    - name: Set PHP packages (Centos 8+)
      ansible.builtin.set_fact:
        php_packages:
          - php
          - php-cli
          - php-common
          - php-devel
          - php-fpm
          - php-gd
          - php-ldap
          - php-mbstring
          - php-opcache
          - php-pdo
          - php-pear
          - php-pecl-apcu
          - php-xml
      when: ansible_facts['os_family'] == 'RedHat'

    - name: Workaround for geerlingguy.php missing ubuntu-24.04 support
      when:
        - ansible_facts['distribution'] == "Ubuntu"
        - ansible_facts['distribution_major_version'] >= '24'
      block:
        - name: Set PHP Version
          ansible.builtin.set_fact:
            __php_default_version_debian: "8.3"

        - name: Set PHP packages
          ansible.builtin.set_fact:
            __php_packages:
              - php{{ __php_default_version_debian }}-common
              - php{{ __php_default_version_debian }}-cli
              - php{{ __php_default_version_debian }}-dev
              - php{{ __php_default_version_debian }}-fpm
              - libpcre3-dev
              - php{{ __php_default_version_debian }}-gd
              - php{{ __php_default_version_debian }}-curl
              - php{{ __php_default_version_debian }}-imap
              - php-json
              - php{{ __php_default_version_debian }}-opcache
              - php{{ __php_default_version_debian }}-xml
              - php{{ __php_default_version_debian }}-mbstring
              - php{{ __php_default_version_debian }}-apcu
              - php{{ __php_default_version_debian }}-sqlite3

    - name: Set PHP Version (Suse)
      ansible.builtin.set_fact:
        __php_default_version_suse: "8"
      when: ansible_facts['os_family'] == 'Suse'

  roles:
    - role: geerlingguy.apache
      when:
        - zabbix_web_http_server == "apache"
    - role: geerlingguy.nginx
      when:
        - zabbix_web_http_server == "nginx"
    - role: geerlingguy.php
    - role: zabbix_server

  post_tasks:
    - name: Suse | Create default php-fpm config
      ansible.builtin.copy:
        remote_src: true
        src: "/etc/php{{ __php_default_version_suse }}/fpm/php-fpm.conf.default"
        dest: "/etc/php{{ __php_default_version_suse }}/fpm/php-fpm.conf"
      when: ansible_facts['os_family'] == 'Suse'

    - name: "Remove file"
      ansible.builtin.file:
        path: "{{ item }}"
        state: absent
      loop:
        - /var/www/html/index.html
