---
- name: Configure Podman driver for all Nomad client nodes
  hosts: nomad_clients,nomad_servers
  become: yes
  
  tasks:
    - name: Stop Nomad service
      systemd:
        name: nomad
        state: stopped
        
    - name: Install Podman if not present
      package:
        name: podman
        state: present
      ignore_errors: yes
        
    - name: Enable Podman socket
      systemd:
        name: podman.socket
        enabled: yes
        state: started
      ignore_errors: yes
        
    - name: Update Nomad configuration to use Podman
      lineinfile:
        path: /etc/nomad.d/nomad.hcl
        regexp: '^plugin "docker"'
        line: 'plugin "podman" {'
        state: present
        
    - name: Add Podman plugin configuration
      blockinfile:
        path: /etc/nomad.d/nomad.hcl
        marker: "# {mark} PODMAN PLUGIN CONFIG"
        block: |
          plugin "podman" {
            config {
              socket_path = "unix:///run/podman/podman.sock"
              volumes {
                enabled = true
              }
            }
          }
        insertafter: 'client {'
        
    - name: Start Nomad service
      systemd:
        name: nomad
        state: started
        
    - name: Wait for Nomad to be ready
      wait_for:
        port: 4646
        host: localhost
        delay: 5
        timeout: 30