---
- name: Update Nomad configuration for Podman and fix issues
  hosts: localhost
  become: yes
  connection: local
  
  tasks:
    - name: Stop Nomad service
      systemd:
        name: nomad
        state: stopped
        
    - name: Update Nomad configuration to use Podman and disable Consul
      copy:
        content: |
          datacenter = "dc1"
          region     = "global"
          data_dir   = "/opt/nomad/data"

          bind_addr = "100.116.158.95"

          server {
            enabled          = true
            bootstrap_expect = 1
            encrypt          = "NVOMDvXblgWfhtzFzOUIHnKEOrbXOkPrkIPbRGGf1YQ="
          }

          client {
            enabled = true
          }

          ui {
            enabled = true
          }

          addresses {
            http = "0.0.0.0"
            rpc  = "100.116.158.95"
            serf = "100.116.158.95"
          }

          ports {
            http = 4646
            rpc  = 4647
            serf = 4648
          }

          plugin "podman" {
            config {
              socket_path = "unix:///run/podman/podman.sock"
              volumes {
                enabled = true
              }
            }
          }

          # Disable Consul integration for now
          consul {
            address = ""
          }

          log_level = "INFO"
          log_file  = "/var/log/nomad/nomad.log"
        dest: /etc/nomad.d/nomad.hcl
        owner: nomad
        group: nomad
        mode: '0640'
        backup: yes
        
    - name: Enable Podman socket for systemd
      systemd:
        name: podman.socket
        enabled: yes
        state: started
      ignore_errors: yes
        
    - 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
        
    - name: Check Nomad status
      uri:
        url: http://localhost:4646/v1/status/leader
        method: GET
      register: nomad_status
      retries: 3
      delay: 5
      
    - name: Display Nomad status
      debug:
        msg: "Nomad leader: {{ nomad_status.json if nomad_status.json is defined else 'No leader elected' }}"