---
- name: NUC12 to PVE Web Access Diagnosis
  hosts: nuc12
  gather_facts: yes
  tasks:
    - name: Test DNS resolution
      command: nslookup pve
      register: dns_test
      ignore_errors: yes

    - name: Display DNS resolution
      debug:
        msg: "{{ dns_test.stdout_lines }}"

    - name: Test ping to PVE
      command: ping -c 3 pve
      register: ping_test
      ignore_errors: yes

    - name: Display ping results
      debug:
        msg: "{{ ping_test.stdout_lines }}"

    - name: Test port connectivity
      command: nc -zv pve 8006
      register: port_test
      ignore_errors: yes

    - name: Display port test results
      debug:
        msg: "{{ port_test.stdout_lines }}"

    - name: Test HTTP access with different methods
      uri:
        url: "https://pve:8006"
        method: GET
        validate_certs: no
        timeout: 10
      register: http_test
      ignore_errors: yes

    - name: Display HTTP test results
      debug:
        msg: |
          Status: {{ http_test.status if http_test.status is defined else 'FAILED' }}
          Content Length: {{ http_test.content | length if http_test.content is defined else 'N/A' }}

    - name: Test with different hostnames
      uri:
        url: "https://{{ item }}:8006"
        method: GET
        validate_certs: no
        timeout: 10
      register: hostname_tests
      loop:
        - "pve"
        - "pve.tailnet-68f9.ts.net"
        - "100.71.59.40"
        - "192.168.31.4"
      ignore_errors: yes

    - name: Display hostname test results
      debug:
        msg: "{{ item.item }}: {{ 'SUCCESS' if item.status == 200 else 'FAILED' }}"
      loop: "{{ hostname_tests.results }}"

    - name: Check browser user agent simulation
      uri:
        url: "https://pve:8006"
        method: GET
        validate_certs: no
        timeout: 10
        headers:
          User-Agent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36"
      register: browser_test
      ignore_errors: yes

    - name: Display browser test results
      debug:
        msg: |
          Browser Simulation: {{ 'SUCCESS' if browser_test.status == 200 else 'FAILED' }}
          Status Code: {{ browser_test.status }}

    - name: Check SSL certificate details
      command: openssl s_client -connect pve:8006 -servername pve < /dev/null 2>/dev/null | openssl x509 -noout -subject -issuer
      register: ssl_cert
      ignore_errors: yes

    - name: Display SSL certificate info
      debug:
        msg: "{{ ssl_cert.stdout_lines }}"

    - name: Check network routing to PVE
      command: traceroute pve
      register: traceroute_test
      ignore_errors: yes

    - name: Display traceroute results
      debug:
        msg: "{{ traceroute_test.stdout_lines }}"
