- hosts: datacenter
  name: Install zabbix agent
  vars:
    ZABBIX_SERVER_IP: "192.168.77.216"
  tasks:
    - name: Combine list1 and list2 into a merged_list var
      ansible.builtin.set_fact:
        zabbix_agent_cfg: "/etc/zabbix/zabbix_agentd.conf"
      when: inventory_hostname in groups['rhasspy']

    - name: Combine list1 and list2 into a merged_list var
      ansible.builtin.set_fact:
        zabbix_agent_cfg: "/etc/zabbix/zabbix_agent2.conf"
      when: inventory_hostname not in groups['nas'] and inventory_hostname not in groups['rhasspy']
      
    - name: Combine list1 and list2 into a merged_list var
      ansible.builtin.set_fact:
        zabbix_agent_cfg: "/opt/ZabbixAgent/etc/zabbix_agentd.conf"
      when: inventory_hostname in groups['nas']

    - name: Print all available facts
      ansible.builtin.debug:
        msg: "{{ false if inventory_hostname not in groups['nas'] else true }}"

    - name: Print all available facts
      ansible.builtin.debug:
        var: ansible_facts.architecture
    # - 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 internet
      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"
      become: "{{ 'no' if inventory_hostname in groups['nas'] else 'yes' }}"

    - name: Install a .deb package from the internet
      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"
      ignore_errors: true
      become: "{{ 'no' if inventory_hostname in groups['nas'] else 'yes' }}"

    - name: Install a .deb package from the internet
      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"
      become: "{{ 'no' if inventory_hostname in groups['nas'] else 'yes' }}"
      
    - name: Install a .deb package from the internet
      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_distribution == "Debian"  and ansible_distribution_major_version  == "12"
    


    # - 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-agent
        update_cache: yes
      when: inventory_hostname not in groups['nas']
      become: "{{ false if inventory_hostname in groups['nas'] else true }}"


    - name: Reconfigure zabbix agent Server
      ansible.builtin.lineinfile:
        path: "{{ zabbix_agent_cfg }}"
        regexp: "^Server=.*"
        insertafter: '^# Server='
        line: "Server=192.168.77.0/24"
      become: "{{ false if inventory_hostname in groups['nas'] else true }}"

    - name: Reconfigure zabbix agent ServerActive
      ansible.builtin.lineinfile:
        path: "{{ zabbix_agent_cfg }}"
        regexp: "^ServerActive=.*"
        line: "ServerActive={{ ZABBIX_SERVER_IP }}"
      become: "{{ 'no' if inventory_hostname in groups['nas'] else 'yes' }}"

    - name: Reconfigure zabbix agent ListenPort
      ansible.builtin.lineinfile:
        path: "{{ zabbix_agent_cfg }}"
        regexp: "^ListenPort=.*"
        line: "ListenPort=10050"
      become: "{{ 'no' if inventory_hostname in groups['nas'] else 'yes' }}"
    # - 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={{ansible_hostname}}"
      become: "{{ 'no' if inventory_hostname in groups['nas'] else 'yes' }}"

    - name: Reconfigure zabbix-agent2 hostname
      ansible.builtin.lineinfile:
        path: "{{ zabbix_agent_cfg }}"
        regexp: "^UserParameter=.*"
        insertafter: '^# UserParameter='
        line: "UserParameter=system.temperature,vcgencmd measure_temp"
      become: "{{ 'no' if inventory_hostname in groups['nas'] else 'yes' }}"

    - name: Reconfigure zabbix-agent2 hostname
      ansible.builtin.lineinfile:
        path: "{{ zabbix_agent_cfg }}"
        regexp: "^HostMetadata=.*"
        insertafter: '^# HostMetadata='
        line: "HostMetadata=linux;jaydee"
      become: "{{ 'no' if inventory_hostname in groups['nas'] else 'yes' }}"

    - name: Add the user 'james' with a bash shell, appending the group 'admins' and 'developers' to the user's groups
      ansible.builtin.user:
        name: zabbix
        groups: video
        append: yes
      when: inventory_hostname not in groups['nas']


    - name: Restart zabbix-agent2 service
      ansible.builtin.service:
        name: zabbix-agent.service
        state: restarted
        enabled: true
      become: true
      when: inventory_hostname not in groups['nas']

    - name: Restart agent
      ansible.builtin.shell: /etc/init.d/ZabbixAgent.sh restart
      when: inventory_hostname in groups['nas']