---
- name: "Include OS-specific variables"
  ansible.builtin.include_vars: "{{ ansible_os_family }}.yml"
  tags:
    - always

- name: Determine Latest Supported Zabbix Version
  ansible.builtin.set_fact:
    zabbix_web_version: "{{ zabbix_valid_web_versions[ansible_distribution_major_version][0] | default(6.4) }}"
  when: zabbix_web_version is not defined
  tags:
    - always

- name: Set More Variables
  ansible.builtin.set_fact:
    zabbix_db_type_long: "{{ 'postgresql' if zabbix_server_database == 'pgsql' else 'mysql' }}"
  tags:
    - always

- name: Check that version is supported
  when: enable_version_check | default(true) | bool
  ansible.builtin.assert:
    that:
      - "{{ zabbix_web_version|float in zabbix_valid_web_versions[ ansible_facts['distribution_major_version'] ] }}"
    fail_msg: Zabbix version {{ zabbix_web_version }} is not supported on {{ ansible_facts['distribution'] }} {{ ansible_facts['distribution_major_version'] }}
  tags:
    - always

- name: Determine PHP Version
  ansible.builtin.shell: php --version | head -1 | awk '{ print $2 }' | awk -F '.' '{print $1"."$2}'
  register: _zabbix_web_php_installed_version
  changed_when: false
  tags:
    - config
    - install

- name: Set PHP Version
  ansible.builtin.set_fact:
    zabbix_web_php_installed_version: "{{ _zabbix_web_php_installed_version.stdout }}"
  tags:
    - config
    - install

- name: Set PHP Variables
  ansible.builtin.set_fact:
    zabbix_php_fpm_listen: "{{ zabbix_php_fpm_listen if zabbix_php_fpm_listen is defined else _zabbix_php_fpm_listen }}"
    zabbix_php_fpm_dir: "{{ zabbix_php_fpm_dir if zabbix_php_fpm_dir is defined else _php_fpm_dir }}"
    zabbix_php_fpm_session: "{{ zabbix_php_fpm_session if zabbix_php_fpm_session is defined else _php_fpm_session }}"
  tags:
    - config
    - install

- name: Install Repository
  ansible.builtin.include_role:
    name: community.zabbix.zabbix_repo
  vars:
    zabbix_repo_version: "{{ zabbix_web_version }}"
    zabbix_repo_package: "zabbix-web-service"
    zabbix_repo_apt_priority: "{{ zabbix_web_apt_priority | default (omit) }}"
  when: zabbix_manage_repo | default(true)

- name: Include OS Specific Tasks
  ansible.builtin.include_tasks: "{{ ansible_os_family }}.yml"

- name: "Install zabbix-web package"
  ansible.builtin.package:
    name: "{{ _zabbix_web_package }}"
    state: "{{ zabbix_web_package_state }}"
    update_cache: true
    disablerepo: "{{ zabbix_web_disable_repo | default(_zabbix_web_disable_repo | default(omit)) }}"
  environment:
    http_proxy: "{{ zabbix_http_proxy | default(None) | default(omit) }}"
    https_proxy: "{{ zabbix_https_proxy | default(None) | default(omit) }}"
  register: _zabbix_web_package_installed
  until: _zabbix_web_package_installed is succeeded
  become: true
  tags:
    - install

- name: "Debian | Link graphfont.ttf (workaround ZBX-10467)"
  when: 
    - ansible_facts['os_family'] == 'Debian'
    - zabbix_web_version is version ('7.2', '<')
  ansible.builtin.file:
    src: "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"
    path: "/usr/share/zabbix/fonts/graphfont.ttf"
    state: link
  become: true
  tags:
    - install

- name: "Install the web server specific tasks"
  ansible.builtin.include_tasks: "{{ zabbix_web_http_server }}.yml"

- name: "Create zabbix-web directory"
  ansible.builtin.file:
    path: /etc/zabbix/web
    owner: "{{ zabbix_web_user }}"
    group: "{{ zabbix_web_group }}"
    state: directory
    mode: 0755
  become: true
  tags:
    - install
    - config

- name: "Configure zabbix-web"
  ansible.builtin.template:
    src: zabbix.conf.php.j2
    dest: /etc/zabbix/web/zabbix.conf.php
    owner: "{{ zabbix_web_user }}"
    group: "{{ zabbix_web_group }}"
    mode: "{{ zabbix_web_conf_mode }}"
  become: true
  tags:
    - config

- name: "Debian / Suse | Install PHP"
  ansible.builtin.template:
    src: php-fpm.conf.j2
    dest: "{{ zabbix_php_fpm_dir }}/zabbix.conf"
    owner: "{{ zabbix_web_user }}"
    group: "{{ zabbix_web_group }}"
    mode: 0644
  become: true
  when:
    - zabbix_web_create_php_fpm
    - ansible_os_family in ["Debian", "Suse"]
  notify:
    - restart php-fpm-version
    - restart php-fpm
  tags:
    - config
