---
- name: PVE Cluster Diagnosis
  hosts: pve_cluster
  gather_facts: yes
  tasks:
    - name: Check PVE service status
      systemd:
        name: pve-cluster
        state: started
      register: pve_cluster_status

    - name: Check PVE proxy service status
      systemd:
        name: pveproxy
        state: started
      register: pve_proxy_status

    - name: Check PVE firewall service status
      systemd:
        name: pve-firewall
        state: started
      register: pve_firewall_status

    - name: Check PVE daemon service status
      systemd:
        name: pvedaemon
        state: started
      register: pve_daemon_status

    - name: Display PVE service status
      debug:
        msg: |
          PVE Cluster: {{ pve_cluster_status.status.ActiveState }}
          PVE Proxy: {{ pve_proxy_status.status.ActiveState }}
          PVE Firewall: {{ pve_firewall_status.status.ActiveState }}
          PVE Daemon: {{ pve_daemon_status.status.ActiveState }}

    - name: Check PVE cluster configuration
      command: pvecm status
      register: pve_cluster_config
      ignore_errors: yes

    - name: Display PVE cluster configuration
      debug:
        msg: "{{ pve_cluster_config.stdout_lines }}"
      when: pve_cluster_config.rc == 0

    - name: Check PVE cluster nodes
      command: pvecm nodes
      register: pve_nodes
      ignore_errors: yes

    - name: Display PVE cluster nodes
      debug:
        msg: "{{ pve_nodes.stdout_lines }}"
      when: pve_nodes.rc == 0

    - name: Check network connectivity to other nodes
      command: ping -c 3 {{ item }}
      loop: "{{ groups['pve_cluster'] }}"
      when: item != inventory_hostname
      register: ping_results
      ignore_errors: yes

    - name: Display ping results
      debug:
        msg: "{{ inventory_hostname }} -> {{ item.item }}: {{ 'SUCCESS' if item.rc == 0 else 'FAILED' }}"
      loop: "{{ ping_results.results }}"
      when: ping_results is defined

    - name: Check SSH service status
      systemd:
        name: ssh
        state: started
      register: ssh_status

    - name: Display SSH service status
      debug:
        msg: "SSH Service: {{ ssh_status.status.ActiveState }}"

    - name: Check SSH configuration
      command: sshd -T
      register: sshd_config
      ignore_errors: yes

    - name: Display SSH configuration (key settings)
      debug:
        msg: |
          PasswordAuthentication: {{ sshd_config.stdout | regex_search('passwordauthentication (yes|no)') }}
          PubkeyAuthentication: {{ sshd_config.stdout | regex_search('pubkeyauthentication (yes|no)') }}
          PermitRootLogin: {{ sshd_config.stdout | regex_search('permitrootlogin (yes|no|prohibit-password)') }}

    - name: Check disk space
      command: df -h
      register: disk_usage

    - name: Display disk usage
      debug:
        msg: "{{ disk_usage.stdout_lines }}"

    - name: Check memory usage
      command: free -h
      register: memory_usage

    - name: Display memory usage
      debug:
        msg: "{{ memory_usage.stdout_lines }}"

    - name: Check system load
      command: uptime
      register: system_load

    - name: Display system load
      debug:
        msg: "{{ system_load.stdout }}"
