---
- name: Install Nomad Podman Driver Plugin
  hosts: all
  become: yes
  vars:
    nomad_user: nomad
    nomad_data_dir: /opt/nomad/data
    nomad_plugins_dir: "{{ nomad_data_dir }}/plugins"
    podman_driver_version: "0.6.1"
    podman_driver_url: "https://releases.hashicorp.com/nomad-driver-podman/{{ podman_driver_version }}/nomad-driver-podman_{{ podman_driver_version }}_linux_amd64.zip"

  tasks:
    - name: Stop Nomad service
      systemd:
        name: nomad
        state: stopped

    - name: Create plugins directory
      file:
        path: "{{ nomad_plugins_dir }}"
        state: directory
        owner: "{{ nomad_user }}"
        group: "{{ nomad_user }}"
        mode: '0755'

    - name: Download Nomad Podman driver
      get_url:
        url: "{{ podman_driver_url }}"
        dest: "/tmp/nomad-driver-podman_{{ podman_driver_version }}_linux_amd64.zip"
        mode: '0644'

    - name: Extract Nomad Podman driver
      unarchive:
        src: "/tmp/nomad-driver-podman_{{ podman_driver_version }}_linux_amd64.zip"
        dest: "/tmp"
        remote_src: yes

    - name: Install Nomad Podman driver
      copy:
        src: "/tmp/nomad-driver-podman"
        dest: "{{ nomad_plugins_dir }}/nomad-driver-podman"
        owner: "{{ nomad_user }}"
        group: "{{ nomad_user }}"
        mode: '0755'
        remote_src: yes

    - name: Update Nomad configuration for plugin directory
      blockinfile:
        path: /etc/nomad.d/nomad.hcl
        marker: "# {mark} PLUGIN DIRECTORY CONFIGURATION"
        block: |
          plugin_dir = "{{ nomad_plugins_dir }}"
        insertafter: 'data_dir = "/opt/nomad/data"'

    - name: Fix Podman socket permissions
      file:
        path: /run/user/1001/podman/podman.sock
        mode: '0666'
      ignore_errors: yes

    - name: Ensure nomad user can access Podman socket
      user:
        name: "{{ nomad_user }}"
        groups: ben
        append: yes

    - name: Start Nomad service
      systemd:
        name: nomad
        state: started
        enabled: yes

    - name: Wait for Nomad to be ready
      wait_for:
        port: 4646
        host: localhost
        delay: 10
        timeout: 60

    - name: Verify Nomad is running
      systemd:
        name: nomad
      register: nomad_service_status

    - name: Display Nomad service status
      debug:
        msg: "Nomad service is {{ nomad_service_status.status.ActiveState }}"

    - name: Wait for plugins to load
      pause:
        seconds: 15

    - name: Check available drivers
      shell: |
        sudo -u {{ nomad_user }} /usr/local/bin/nomad node status -self | grep -A 20 "Driver Status"
      register: driver_status
      failed_when: false

    - name: Display driver status
      debug:
        var: driver_status.stdout_lines

    - name: Test Podman driver functionality
      shell: |
        sudo -u {{ nomad_user }} /usr/local/bin/nomad node status -json | jq -r '.Drivers | keys[]'
      register: available_drivers
      failed_when: false

    - name: Display available drivers
      debug:
        msg: "Available drivers: {{ available_drivers.stdout_lines | join(', ') }}"

    - name: Clean up downloaded files
      file:
        path: "{{ item }}"
        state: absent
      loop:
        - "/tmp/nomad-driver-podman_{{ podman_driver_version }}_linux_amd64.zip"
        - "/tmp/nomad-driver-podman"

    - name: Final verification - Check if Podman driver is loaded
      shell: |
        sudo -u {{ nomad_user }} /usr/local/bin/nomad node status -json | jq -r '.Drivers.podman.Detected'
      register: podman_driver_detected
      failed_when: false

    - name: Display final result
      debug:
        msg: |
          Podman driver installation: {{ 'SUCCESS' if podman_driver_detected.stdout == 'true' else 'NEEDS VERIFICATION' }}
          Driver detected: {{ podman_driver_detected.stdout | default('unknown') }}