---
- name: PVE Web Interface Diagnosis
  hosts: pve_cluster
  gather_facts: yes
  tasks:
    - name: Check PVE web services status
      systemd:
        name: "{{ item }}"
        state: started
      register: pve_web_services
      loop:
        - pveproxy
        - pvedaemon
        - pve-cluster
        - pve-firewall

    - name: Display PVE web services status
      debug:
        msg: |
          {{ item.item }}: {{ item.status.ActiveState }}
      loop: "{{ pve_web_services.results }}"

    - name: Check PVE web port status
      wait_for:
        port: 8006
        host: "{{ ansible_default_ipv4.address }}"
        timeout: 5
      register: pve_web_port
      ignore_errors: yes

    - name: Display PVE web port status
      debug:
        msg: "PVE Web Port 8006: {{ 'OPEN' if pve_web_port.rc == 0 else 'CLOSED' }}"

    - name: Check listening ports
      command: netstat -tlnp | grep :8006
      register: listening_ports
      ignore_errors: yes

    - name: Display listening ports
      debug:
        msg: "{{ listening_ports.stdout_lines }}"
      when: listening_ports.rc == 0

    - name: Check PVE firewall status
      command: pve-firewall status
      register: firewall_status
      ignore_errors: yes

    - name: Display firewall status
      debug:
        msg: "{{ firewall_status.stdout_lines }}"
      when: firewall_status.rc == 0

    - name: Check PVE firewall rules
      command: pve-firewall show
      register: firewall_rules
      ignore_errors: yes

    - name: Display firewall rules
      debug:
        msg: "{{ firewall_rules.stdout_lines }}"
      when: firewall_rules.rc == 0

    - name: Check network interfaces
      command: ip addr show
      register: network_interfaces

    - name: Display network interfaces
      debug:
        msg: "{{ network_interfaces.stdout_lines }}"

    - name: Check routing table
      command: ip route show
      register: routing_table

    - name: Display routing table
      debug:
        msg: "{{ routing_table.stdout_lines }}"

    - name: Test connectivity to PVE web port from other nodes
      command: nc -zv {{ inventory_hostname }} 8006
      delegate_to: "{{ item }}"
      loop: "{{ groups['pve_cluster'] }}"
      when: item != inventory_hostname
      register: connectivity_test
      ignore_errors: yes

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

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

    - name: Display cluster status
      debug:
        msg: "{{ cluster_status.stdout_lines }}"
      when: cluster_status.rc == 0

    - name: Check PVE logs for errors
      command: journalctl -u pveproxy -n 20 --no-pager
      register: pveproxy_logs
      ignore_errors: yes

    - name: Display PVE proxy logs
      debug:
        msg: "{{ pveproxy_logs.stdout_lines }}"
      when: pveproxy_logs.rc == 0

    - name: Check system logs for network errors
      command: journalctl -n 50 --no-pager | grep -i "route\|network\|connection"
      register: network_logs
      ignore_errors: yes

    - name: Display network error logs
      debug:
        msg: "{{ network_logs.stdout_lines }}"
      when: network_logs.rc == 0

    - name: Check if PVE web interface is accessible locally
      uri:
        url: "https://localhost:8006"
        method: GET
        validate_certs: no
        timeout: 10
      register: local_web_test
      ignore_errors: yes

    - name: Display local web test result
      debug:
        msg: "Local PVE web access: {{ 'SUCCESS' if local_web_test.status == 200 else 'FAILED' }}"
      when: local_web_test is defined

    - name: Check PVE configuration files
      stat:
        path: /etc/pve/local/pve-ssl.key
      register: ssl_key_stat

    - name: Check SSL certificate
      stat:
        path: /etc/pve/local/pve-ssl.pem
      register: ssl_cert_stat

    - name: Display SSL status
      debug:
        msg: |
          SSL Key exists: {{ ssl_key_stat.stat.exists }}
          SSL Cert exists: {{ ssl_cert_stat.stat.exists }}

    - name: Check PVE datacenter configuration
      stat:
        path: /etc/pve/datacenter.cfg
      register: datacenter_cfg

    - name: Display datacenter config status
      debug:
        msg: "Datacenter config exists: {{ datacenter_cfg.stat.exists }}"

    - name: Check PVE cluster configuration
      stat:
        path: /etc/pve/corosync.conf
      register: corosync_conf

    - name: Display corosync config status
      debug:
        msg: "Corosync config exists: {{ corosync_conf.stat.exists }}"
