- hosts: datacenter
  name: Install zabbix agent
  become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"
  vars:
    ZABBIX_SERVER: "zabbix.home.lan"
  tasks:

    - 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_agentd.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 internet11
      ansible.builtin.apt:
        deb: https://repo.zabbix.com/zabbix/6.4/ubuntu-arm64/pool/main/z/zabbix-release/zabbix-release_6.4-1+ubuntu22.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.0/raspbian/pool/main/z/zabbix-release/zabbix-release_7.0-1+debian11_all.deb
      retries: 5
      delay: 5
      when:
        - ansible_facts.architecture == "armv7l" or ansible_facts.architecture == "aarch64"

      ignore_errors: true

    - 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 internet4
      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.0/debian/pool/main/z/zabbix-release/zabbix-release_7.0-1+debian12_all.deb
      when:
        - ansible_facts.architecture != "armv7l" and  ansible_facts.architecture != "aarch64" and ansible_distribution == "Debian"  and ansible_distribution_major_version  == "12"
      ignore_errors: true

    # - 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: yes
      ignore_errors: true
      when: inventory_hostname != 'nas.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.80.0/28"
      become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"

    - 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 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,python3 /share/ZFS530_DATA/.qpkg/ZabbixAgent/cert_check2.py"
      when: inventory_hostname == 'nas.home.lan' or inventory_hostname == 'm-server.home.lan' 

    - name: Reconfigure zabbix-agent2 config
      ansible.builtin.lineinfile:
        path: "{{ zabbix_agent_cfg }}"
        insertafter: '^# UserParameter='
        line: "UserParameter=rpi.hw.temp,/usr/bin/vcgencmd measure_temp"
      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 == 'nas.home.lan' or inventory_hostname == 'm-server.home.lan' 

    - name: Add the user 'to group video
      ansible.builtin.user:
        name: zabbix
        groups: video
        append: yes
      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.shell: /etc/init.d/ZabbixAgent.sh restart
      when: inventory_hostname == 'nas.home.lan'