---
- name: Step by step installation test - Fixed
  hosts: test_hosts
  become: yes
  serial: 1
  tasks:
    - name: Step 1 - System information
      debug:
        msg:
          - "Host: {{ inventory_hostname }}"
          - "OS: {{ ansible_distribution }} {{ ansible_distribution_version }}"
          - "Family: {{ ansible_os_family }}"

    # RHEL/CentOS/Anolis 系统
    - block:
        - name: Step 2 - Import GPG key for RHEL systems
          rpm_key:
            key: https://repo.zabbix.com/RPM-GPG-KEY-ZABBIX-A14FE591
            state: present
            validate_certs: false  # 解决SSL问题
          register: gpg_import

        - name: Step 3 - Verify GPG key
          command: rpm -q gpg-pubkey --qf '%{NAME}-%{VERSION}-%{RELEASE}\t%{SUMMARY}\n'
          register: gpg_list
          changed_when: false

        - name: Display GPG keys
          debug:
            msg: "{{ gpg_list.stdout_lines | select('search', 'zabbix|Zabbix') | list }}"

        - name: Step 4 - Check if repository is already installed
          stat:
            path: /etc/yum.repos.d/zabbix.repo
          register: repo_exists

        - name: Step 5 - Download repository package (if not exists)
          get_url:
            url: https://repo.zabbix.com/zabbix/6.4/rhel/8/x86_64/zabbix-release-6.4-1.el8.noarch.rpm
            dest: /tmp/zabbix-release-6.4-1.el8.noarch.rpm
            mode: '0644'
            timeout: 30
            validate_certs: false
          when: 
            - not repo_exists.stat.exists
            - ansible_distribution_major_version == "8"

        - name: Step 5 - Download repository package for CentOS 7 (if not exists)
          get_url:
            url: https://repo.zabbix.com/zabbix/6.4/rhel/7/x86_64/zabbix-release-6.4-1.el7.noarch.rpm
            dest: /tmp/zabbix-release-6.4-1.el7.noarch.rpm
            mode: '0644'
            timeout: 30
            validate_certs: false
          when: 
            - not repo_exists.stat.exists
            - ansible_distribution_major_version == "7"

        - name: Step 6 - Install repository package for version 8
          command: rpm -Uvh /tmp/zabbix-release-6.4-1.el8.noarch.rpm
          register: repo_install_8
          failed_when: 
            - repo_install_8.rc != 0
            - "'已经安装' not in repo_install_8.stderr"
            - "'already installed' not in repo_install_8.stderr"
          when: 
            - not repo_exists.stat.exists
            - ansible_distribution_major_version == "8"

        - name: Step 6 - Install repository package for version 7
          command: rpm -Uvh /tmp/zabbix-release-6.4-1.el7.noarch.rpm
          register: repo_install_7
          failed_when: 
            - repo_install_7.rc != 0
            - "'已经安装' not in repo_install_7.stderr"
            - "'already installed' not in repo_install_7.stderr"
          when: 
            - not repo_exists.stat.exists
            - ansible_distribution_major_version == "7"

        - name: Display repository installation result
          debug:
            msg: "Repository installation: {{ 'Already exists' if repo_exists.stat.exists else 'Completed' }}"

        - name: Step 7 - Verify repository file
          shell: ls -la /etc/yum.repos.d/zabbix*
          register: repo_files

        - name: Display repository files
          debug:
            var: repo_files.stdout_lines

        - name: Step 8 - Clean and update cache
          shell: |
            if command -v dnf >/dev/null 2>&1; then
              dnf clean all && dnf makecache
            else
              yum clean all && yum makecache
            fi
          register: cache_update

        - name: Step 9 - Search for Zabbix packages
          shell: |
            if command -v dnf >/dev/null 2>&1; then
              dnf search zabbix-agent2
            else
              yum search zabbix-agent2
            fi
          register: package_search

        - name: Display available packages
          debug:
            msg: "{{ package_search.stdout_lines | select('search', 'zabbix-agent2') | list }}"

        - name: Step 10 - Install Zabbix Agent2 with dnf
          dnf:
            name: zabbix-agent2
            state: present
          register: install_result_dnf
          when: ansible_distribution_major_version == "8"

        - name: Step 10 - Install Zabbix Agent2 with yum
          yum:
            name: zabbix-agent2
            state: present
          register: install_result_yum
          when: ansible_distribution_major_version == "7"

        - name: Display installation result
          debug:
            msg: "Zabbix Agent2 installation: {{ 'Success' if (install_result_dnf is defined and install_result_dnf.changed) or (install_result_yum is defined and install_result_yum.changed) else 'Already installed or failed' }}"

      when: ansible_os_family in ["RedHat", "Anolis OS"]

    # Ubuntu 系统
    - block:
        - name: Step 2 - Import GPG key for Ubuntu
          apt_key:
            url: https://repo.zabbix.com/zabbix-official-repo.key
            state: present
          register: gpg_import_ubuntu

        - name: Step 3 - Check if repository is already configured
          stat:
            path: /etc/apt/sources.list.d/zabbix.list
          register: repo_exists_ubuntu

        - name: Step 4 - Download repository package (if not exists)
          get_url:
            url: https://repo.zabbix.com/zabbix/6.4/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.4-1+ubuntu18.04_all.deb
            dest: /tmp/zabbix-release.deb
            mode: '0644'
            timeout: 30
          when: not repo_exists_ubuntu.stat.exists

        - name: Step 5 - Install Zabbix repository
          apt:
            deb: /tmp/zabbix-release.deb
            state: present
          when: not repo_exists_ubuntu.stat.exists

        - name: Step 6 - Update apt cache
          apt:
            update_cache: yes

        - name: Step 7 - Install Zabbix Agent2
          apt:
            name: zabbix-agent2
            state: present
          register: install_result_ubuntu

        - name: Display installation result
          debug:
            msg: "Zabbix Agent2 installation: {{ 'Success' if install_result_ubuntu.changed else 'Already installed' }}"

      when: ansible_os_family == "Debian"

    # 通用验证步骤
    - name: Step 11 - Verify Zabbix Agent2 installation
      command: zabbix_agent2 --version
      register: version_check
      changed_when: false

    - name: Display Zabbix Agent2 version
      debug:
        msg: "Installed version: {{ version_check.stdout_lines[0] if version_check.stdout_lines else 'Not found' }}"

  post_tasks:
    - name: Clean up downloaded files
      file:
        path: "{{ item }}"
        state: absent
      loop:
        - /tmp/zabbix-release-6.4-1.el8.noarch.rpm
        - /tmp/zabbix-release-6.4-1.el7.noarch.rpm
        - /tmp/zabbix-release.deb
      ignore_errors: yes

    - name: Final status summary
      debug:
        msg:
          - "Host: {{ inventory_hostname }}"
          - "OS: {{ ansible_distribution }} {{ ansible_distribution_version }}"
          - "Zabbix Agent2: {{ 'Installed' if version_check.rc == 0 else 'Not installed' }}"
