---
- name: Debug Nomad Podman Driver Issues
  hosts: all
  become: yes
  vars:
    nomad_user: nomad

  tasks:
    - name: Check Nomad configuration
      shell: cat /etc/nomad.d/nomad.hcl
      register: nomad_config

    - name: Display Nomad configuration
      debug:
        var: nomad_config.stdout_lines

    - name: Check plugin directory contents
      shell: ls -la /opt/nomad/data/plugins/
      register: plugin_dir

    - name: Display plugin directory
      debug:
        var: plugin_dir.stdout_lines

    - name: Check Nomad logs for plugin loading
      shell: journalctl -u nomad -n 50 --no-pager | grep -E "(plugin|driver|podman)"
      register: nomad_logs
      failed_when: false

    - name: Display relevant Nomad logs
      debug:
        var: nomad_logs.stdout_lines

    - name: Check if plugin is executable
      stat:
        path: /opt/nomad/data/plugins/nomad-driver-podman
      register: plugin_stat

    - name: Display plugin file info
      debug:
        var: plugin_stat

    - name: Test plugin directly
      shell: /opt/nomad/data/plugins/nomad-driver-podman --version
      register: plugin_version
      failed_when: false
      become_user: "{{ nomad_user }}"

    - name: Display plugin version
      debug:
        msg: "Plugin version test: {{ 'SUCCESS' if plugin_version.rc == 0 else 'FAILED' }} - {{ plugin_version.stdout if plugin_version.rc == 0 else plugin_version.stderr }}"

    - name: Check Podman socket accessibility
      shell: sudo -u {{ nomad_user }} curl --unix-socket /run/user/1001/podman/podman.sock http://localhost/v1.0.0/libpod/info 2>/dev/null | head -3
      register: podman_socket_test
      failed_when: false

    - name: Display Podman socket test
      debug:
        msg: "Podman socket test: {{ 'SUCCESS' if podman_socket_test.rc == 0 else 'FAILED' }}"