- name: Install zabbix agent
  become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"
  block:
    - name: Include facts
      ansible.builtin.include_role:
        name: "setup"
      when: ansible_facts.architecture is not defined

    - name: Get config for not nas
      ansible.builtin.set_fact:
        zabbix_agent_cfg: "/etc/zabbix/zabbix_agent2.conf"
      when: inventory_hostname != 'nas.home.lan'

    - name: Get config for nas
      ansible.builtin.set_fact:
        zabbix_agent_cfg: "/opt/ZabbixAgent/etc/zabbix_agent2.conf"
      when: inventory_hostname  == 'nas.home.lan'

    - name: Print all available facts
      ansible.builtin.debug:
        msg: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"

    - name: Print all available facts
      ansible.builtin.debug:
        var: ansible_facts.architecture
    - name: Print all available facts
      ansible.builtin.debug:
        var: ansible_distribution
    - name: Print all available facts
      ansible.builtin.debug:
        var: ansible_distribution_major_version
    # - name: Upload zabbix package
    #   ansible.builtin.copy:
    #     src: packages/zabbix-release_6.4-1+ubuntu22.04_all.deb
    #     dest: /tmp/
    - name: Install a .deb package from the Ubuntu
      ansible.builtin.apt:
        deb: https://repo.zabbix.com/zabbix/7.4/release/ubuntu/pool/main/z/zabbix-release/zabbix-release_latest_7.4+ubuntu24.04_all.deb
      when:
        - ansible_facts.architecture != "armv7l" and  ( ansible_distribution == "Ubuntu" or ansible_distribution == "Linux Mint" )

    # - name: Install a .deb package from the internet2
    #   ansible.builtin.apt:
    #     # deb: https://repo.zabbix.com/zabbix/6.4/raspbian/pool/main/z/zabbix-release/zabbix-release_6.4-1+debian11_all.deb
    #     deb: https://repo.zabbix.com/zabbix/7.2/release/raspbian/pool/main/z/zabbix-release/zabbix-release_latest_7.2+debian12_all.deb
    #   retries: 5
    #   delay: 5
    #   when:
    #     - ansible_facts.architecture == "armv7l" or ansible_facts.architecture == "aarch64"
    #   register: command_result
    #   failed_when: "'FAILED' in command_result.stderr"

    - name: Install a .deb package from the internet3
      ansible.builtin.apt:
        deb: https://repo.zabbix.com/zabbix/6.4/debian/pool/main/z/zabbix-release/zabbix-release_6.4-1+debian11_all.deb
      when:
        - ansible_facts.architecture != "armv7l"  and ansible_distribution == "Debian"  and ansible_distribution_major_version  == "11"

    - name: Install a .deb package from the Debian 12
      ansible.builtin.apt:
        # deb: https://repo.zabbix.com/zabbix/6.4/debian/pool/main/z/zabbix-release/zabbix-release_6.4-1+debian12_all.deb
        deb: https://repo.zabbix.com/zabbix/7.4/release/debian/pool/main/z/zabbix-release/zabbix-release_latest_7.4+debian12_all.deb
      when:
        - ansible_facts.architecture != "armv7l"
        - ansible_facts.architecture != "aarch64"
        - ansible_distribution == "Debian"
        - ansible_distribution_major_version  == "12"
      register: command_result
      failed_when: "'FAILED' in command_result.stderr"
    - name: Install a .deb package from the Debian 13
      ansible.builtin.apt:
        # deb: https://repo.zabbix.com/zabbix/6.4/debian/pool/main/z/zabbix-release/zabbix-release_6.4-1+debian12_all.deb
        deb: https://repo.zabbix.com/zabbix/7.4/release/debian/pool/main/z/zabbix-release/zabbix-release_latest_7.4+debian13_all.deb
      when:
        - ansible_facts.architecture != "armv7l"
        - ansible_facts.architecture != "aarch64"
        - ansible_distribution == "Debian"
        - ansible_distribution_major_version  == "13"
      register: command_result
      failed_when: "'FAILED' in command_result.stderr"
    # - name: Install a .deb package localy
    #   ansible.builtin.apt:
    #     deb: /tmp/zabbix-release_6.4-1+ubuntu22.04_all.deb
    - name: Install zabbix packages
      ansible.builtin.apt:
        name:
          - zabbix-agent2
          - zabbix-agent2-plugin-mongodb
          - zabbix-agent2-plugin-postgresql
          # - zabbix-agent2-plugin-mysql
        update_cache: true
      when: inventory_hostname not in ['nas.home.lan','router.home.lan']
    - name: Install zabbix packages
      ansible.builtin.apt:
        name:
          - zabbix-agent2
          # - zabbix-agent2-plugin-mysql
        update_cache: true
      when: inventory_hostname == 'router.home.lan'


    - name: Upgrade zabbix packages
      ansible.builtin.apt:
        name:
          - zabbix-agent2
          - zabbix-agent2-plugin-mongodb
          - zabbix-agent2-plugin-postgresql
          # - zabbix-agent2-plugin-mysql
        only_upgrade: true
        state: present
      when: inventory_hostname not in ['nas.home.lan','router.home.lan']

    - name: Reconfigure zabbix agent Server
      ansible.builtin.lineinfile:
        path: "{{ zabbix_agent_cfg }}"
        regexp: "^Server=.*"
        insertafter: '^# Server='
        line: "Server=192.168.77.0/24,192.168.89.0/28"


    - name: Reconfigure zabbix agent ServerActive
      ansible.builtin.lineinfile:
        path: "{{ zabbix_agent_cfg }}"
        regexp: "^ServerActive=.*"
        line: "ServerActive={{ ZABBIX_SERVER }}"


    - name: Reconfigure zabbix agent ListenPort
      ansible.builtin.lineinfile:
        path: "{{ zabbix_agent_cfg }}"
        regexp: "^ListenPort=.*"
        line: "ListenPort=10050"
    # - name: Reconfigure zabbix agent ListenIP
    #   ansible.builtin.lineinfile:
    #     path: /"{{ zabbix_agent_cfg }}"
    #     regexp: "^ListenIP=.*"
    #     line: "ListenIP=0.0.0.0"

    - name: Reconfigure zabbix-agent2 HeartbeatFrequency
      ansible.builtin.lineinfile:
        path: "{{ zabbix_agent_cfg }}"
        insertafter: '^# HeartbeatFrequency='
        regexp: "^HeartbeatFrequency=.*"
        line: "HeartbeatFrequency=30"

    - name: Reconfigure zabbix-agent2 ForceActiveChecksOnStart
      ansible.builtin.lineinfile:
        path: "{{ zabbix_agent_cfg }}"
        insertafter: '^# ForceActiveChecksOnStart='
        regexp: "^ForceActiveChecksOnStart=.*"
        line: "ForceActiveChecksOnStart=1"

    - name: Reconfigure zabbix-agent2 hostname
      ansible.builtin.lineinfile:
        path: "{{ zabbix_agent_cfg }}"
        regexp: "^Hostname=.*"
        line: "Hostname={{ inventory_hostname }}"


    - name: Reconfigure zabbix-agent2 config
      ansible.builtin.lineinfile:
        path: "{{ zabbix_agent_cfg }}"
        insertafter: '^# UserParameter='
        regexp: "^UserParameter=system.certs.*"
        line: "UserParameter=system.certs, /share/ZFS530_DATA/.qpkg/QPython312/bin/python3 /share/Data/__GITLAB/cert_check/cert_check.py"
      when: inventory_hostname == 'nas.home.lan'


    - name: Reconfigure zabbix-agent2 config
      ansible.builtin.lineinfile:
        path: "{{ zabbix_agent_cfg }}"
        insertafter: '^# UserParameter='
        regexp: "^UserParameter=system.certs.*"
        line: "UserParameter=system.certs,sudo /usr/bin/cert_check2.py"
      when: inventory_hostname == 'm-server.home.lan'


    - name: Reconfigure zabbix-agent2 config
      ansible.builtin.lineinfile:
        path: "{{ zabbix_agent_cfg }}"
        insertafter: '^# UserParameter='
        regexp: "^UserParameter=rpi.hw.temp.*"
        line: "UserParameter=rpi.hw.temp,/usr/bin/vcgencmd measure_temp|awk -F'=' '{ print $2 }'|sed \"s/'C//\""
      when: inventory_hostname == 'rpi5.home.lan'


    - name: Reconfigure zabbix-agent2 hostname
      ansible.builtin.lineinfile:
        path: "{{ zabbix_agent_cfg }}"
        regexp: "^HostMetadata=.*"
        insertafter: '^# HostMetadata='
        line: "HostMetadata=linux;jaydee"

    - name: Reconfigure zabbix-agent2 hostname
      ansible.builtin.lineinfile:
        path: "{{ zabbix_agent_cfg }}"
        regexp: "^HostMetadata=.*"
        insertafter: '^# HostMetadata='
        line: "HostMetadata=server;jaydee"
      when: inventory_hostname == 'm-server.home.lan'

    - name: Create docker-swarm plugin directory
      ansible.builtin.file:
        path: /usr/lib/zabbix/plugins/docker-swarm/
        state: directory
        mode: '0755'
        owner: zabbix
        group: zabbix

    - name: Copy docker-swarm plugin
      ansible.builtin.copy:
        src: docker-swarm-linux-amd64
        dest: /usr/lib/zabbix/plugins/docker-swarm/docker-swarm-linux-x86_64
        mode: '0755'
        owner: zabbix
        group: zabbix
      when: inventory_hostname == 'm-server.home.lan'

    - name: Reconfigure zabbix-agent2 hostname
      ansible.builtin.lineinfile:
        path: "{{ zabbix_agent_cfg }}"
        regexp: "^Plugins.DockerSwarm.System.Path=.*"
        insertafter: '^# HostMetadata='
        line: "Plugins.DockerSwarm.System.Path=/usr/lib/zabbix/plugins/docker-swarm/docker-swarm-linux-x86_64"
      when: inventory_hostname == 'm-server.home.lan'
    - name: Reconfigure zabbix-agent2 hostname
      ansible.builtin.lineinfile:
        path: "{{ zabbix_agent_cfg }}"
        regexp: "^Plugins.DockerSwarm.System.Timeout=.*"
        line: "Plugins.DockerSwarm.System.Timeout=30"
      when: inventory_hostname == 'm-server.home.lan'

    - name: Add the user 'zabbix' to group video
      ansible.builtin.user:
        name: zabbix
        groups: video
        append: true
      when: inventory_hostname != 'nas.home.lan'


    - name: Restart zabbix-agent2 service
      ansible.builtin.service:
        name: zabbix-agent2.service
        state: restarted
        enabled: true
      when: inventory_hostname != 'nas.home.lan'


    - name: Restart agent
      ansible.builtin.command: /etc/init.d/ZabbixAgent.sh restart
      when: inventory_hostname == 'nas.home.lan'
      changed_when: false
