---
- name: Reinstall Zabbix Agent (switch from agent2 to agent)
  hosts: test_hosts
  become: yes
  vars:
    zabbix_server: "192.168.99.146"
    zabbix_agent_package: "zabbix-agent"
    zabbix_agent_service: "zabbix-agent"
    zabbix_agent_config: "/etc/zabbix/zabbix_agentd.conf"
    
  tasks:
    - name: Display current system info
      debug:
        msg:
          - "Host: {{ inventory_hostname }}"
          - "System: {{ system_type }}"
          - "Switching to zabbix-agent (v1)"

    - name: Stop and disable zabbix-agent2 if exists
      systemd:
        name: zabbix-agent2
        state: stopped
        enabled: no
      ignore_errors: yes

    - name: Remove zabbix-agent2 package (RHEL/CentOS/Anolis)
      package:
        name: zabbix-agent2
        state: absent
      when: ansible_os_family in ["RedHat"] or system_type in ["anolis8", "anolis7", "centos7", "centos8"]
      ignore_errors: yes

    - name: Remove zabbix-agent2 package (Ubuntu)
      package:
        name: zabbix-agent2
        state: absent
      when: ansible_os_family == "Debian" or system_type in ["ubuntu18", "ubuntu22"]
      ignore_errors: yes

    - name: Clean up agent2 config files
      file:
        path: "{{ item }}"
        state: absent
      loop:
        - /etc/zabbix/zabbix_agent2.conf
        - /etc/zabbix/zabbix_agent2.conf.backup
        - /run/zabbix/zabbix_agent2.pid
      ignore_errors: yes

    - name: Install Zabbix Agent for RHEL/CentOS/Anolis
      package:
        name: "{{ zabbix_agent_package }}"
        state: present
      when: ansible_os_family in ["RedHat"] or system_type in ["anolis8", "anolis7", "centos7", "centos8"]

    - name: Install Zabbix Agent for Ubuntu
      package:
        name: "{{ zabbix_agent_package }}"
        state: present
      when: ansible_os_family == "Debian" or system_type in ["ubuntu18", "ubuntu22"]

    - name: Backup original agent config if exists
      copy:
        src: "{{ zabbix_agent_config }}"
        dest: "{{ zabbix_agent_config }}.backup-{{ ansible_date_time.epoch }}"
        remote_src: yes
        backup: no
      ignore_errors: yes

    - name: Ensure Zabbix directories exist
      file:
        path: "{{ item }}"
        state: directory
        owner: zabbix
        group: zabbix
        mode: '0755'
      loop:
        - /var/log/zabbix
        - /run/zabbix
        - /etc/zabbix/zabbix_agentd.d
      ignore_errors: yes

    - name: Configure Zabbix Agent
      template:
        src: zabbix_agentd.conf.j2
        dest: "{{ zabbix_agent_config }}"
        owner: root
        group: root
        mode: '0644'
      notify: restart zabbix-agent

    - name: Start and enable Zabbix Agent
      systemd:
        name: "{{ zabbix_agent_service }}"
        state: started
        enabled: yes
        daemon_reload: yes

    - name: Wait for agent to start
      wait_for:
        port: 10050
        host: "{{ ansible_default_ipv4.address }}"
        delay: 3
        timeout: 30

    - name: Test agent connectivity
      command: zabbix_agentd -t agent.ping
      register: agent_test
      changed_when: false
      failed_when: false

    - name: Display connectivity test
      debug:
        msg: "Agent test result: {{ agent_test.stdout if agent_test.rc == 0 else 'Failed: ' + (agent_test.stderr | default('Unknown error')) }}"

    - name: Configure firewall (RHEL/CentOS/Anolis)
      firewalld:
        port: "10050/tcp"
        permanent: yes
        state: enabled
        immediate: yes
      when: ansible_os_family in ["RedHat"] or system_type in ["anolis8", "anolis7", "centos7", "centos8"]
      ignore_errors: yes

    - name: Configure firewall (Ubuntu)
      ufw:
        rule: allow
        port: '10050'
        proto: tcp
      when: ansible_os_family == "Debian" or system_type in ["ubuntu18", "ubuntu22"]
      ignore_errors: yes

    - name: Verify final status
      shell: |
        echo "=== Service Status ==="
        systemctl status {{ zabbix_agent_service }} --no-pager -l | head -20
        echo ""
        echo "=== Port Status ==="
        ss -tlnp | grep 10050 || netstat -tlnp | grep 10050
        echo ""
        echo "=== Version ==="
        zabbix_agentd --version 2>/dev/null | head -1
      register: final_status

    - name: Display final verification
      debug:
        var: final_status.stdout_lines

  handlers:
    - name: restart zabbix-agent
      systemd:
        name: "{{ zabbix_agent_service }}"
        state: restarted
