---
- name: Remove Consul configuration from all Nomad servers
  hosts: semaphore,ash1d,ash2e,ch2,ch3,onecloud1,de
  become: yes
  
  tasks:
    - name: Create clean Nomad server configuration
      copy:
        content: |
          datacenter = "dc1"
          data_dir = "/opt/nomad/data"
          plugin_dir = "/opt/nomad/plugins"
          log_level = "INFO"
          name = "{{ inventory_hostname }}"

          bind_addr = "{{ inventory_hostname }}.tailnet-68f9.ts.net"

          addresses {
            http = "{{ inventory_hostname }}.tailnet-68f9.ts.net"
            rpc  = "{{ inventory_hostname }}.tailnet-68f9.ts.net"
            serf = "{{ inventory_hostname }}.tailnet-68f9.ts.net"
          }

          advertise {
            http = "{{ inventory_hostname }}.tailnet-68f9.ts.net:4646"
            rpc  = "{{ inventory_hostname }}.tailnet-68f9.ts.net:4647"
            serf = "{{ inventory_hostname }}.tailnet-68f9.ts.net:4648"
          }

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

          server {
            enabled = true
            bootstrap_expect = 7
            retry_join = ["ash1d.tailnet-68f9.ts.net","ash2e.tailnet-68f9.ts.net","ch2.tailnet-68f9.ts.net","ch3.tailnet-68f9.ts.net","onecloud1.tailnet-68f9.ts.net","de.tailnet-68f9.ts.net"]
          }

          client {
            enabled = false
          }

          plugin "nomad-driver-podman" {
            config {
              socket_path = "unix:///run/podman/podman.sock"
              volumes {
                enabled = true
              }
            }
          }
        dest: /etc/nomad.d/nomad.hcl
        mode: '0644'

    - name: Restart Nomad service
      systemd:
        name: nomad
        state: restarted

    - name: Wait for Nomad to be ready
      wait_for:
        port: 4646
        host: "{{ ansible_default_ipv4.address }}"
        delay: 5
        timeout: 30

    - name: Display completion message
      debug:
        msg: "Removed Consul configuration from {{ inventory_hostname }}"

