- 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'
  become: 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/7.2/release/ubuntu/pool/main/z/zabbix-release/zabbix-release_latest_7.2+ubuntu24.04_all.deb
  when:
    - ansible_facts.architecture != "armv7l" and  ( ansible_distribution == "Ubuntu1" or ansible_distribution == "Linux Mint" )
  become: true
- 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"
  become: true
  failed_when: my_output.rc != 0

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

- name: Install zabbix packages
  ansible.builtin.apt:
    name:
      - zabbix-agent2
      - zabbix-agent2-plugin-mongodb
      - zabbix-agent2-plugin-postgresql
    update_cache: false
  become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"
  failed_when: my_output.rc != 0
  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/24"
  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 }}"
  become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"
- name: Reconfigure zabbix agent ListenPort
  ansible.builtin.lineinfile:
    path: "{{ zabbix_agent_cfg }}"
    regexp: "^ListenPort=.*"
    line: "ListenPort=10050"
  become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"
# - 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 }}"
  become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"

- 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'
  become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"

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

- 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'
  become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"

- name: Reconfigure zabbix-agent2 hostname
  ansible.builtin.lineinfile:
    path: "{{ zabbix_agent_cfg }}"
    regexp: "^HostMetadata=.*"
    insertafter: '^# HostMetadata='
    line: "HostMetadata=linux;jaydee"
  become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"

- 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'
  become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"

- name: Add the user 'to group video
  ansible.builtin.user:
    name: zabbix
    groups: video
    append: true
  when: inventory_hostname != 'nas.home.lan'
  become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"

- name: Restart zabbix-agent2 service
  ansible.builtin.service:
    name: zabbix-agent2.service
    state: restarted
    enabled: true
  when: inventory_hostname != 'nas.home.lan'
  become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"

- name: Restart agent
  ansible.builtin.command: /etc/init.d/ZabbixAgent.sh restart
  when: inventory_hostname == 'nas.home.lan'
  changed_when: my_output.rc != 0
  become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"
  