---
- name: Configure Nomad Dynamic Host Volumes for NFS
  hosts: nomad_clients
  become: yes
  vars:
    nfs_server: "snail"
    nfs_share: "/fs/1000/nfs/Fnsync"
    mount_point: "/mnt/fnsync"

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

    - name: Update Nomad configuration for dynamic host volumes
      blockinfile:
        path: /etc/nomad.d/nomad.hcl
        marker: "# {mark} DYNAMIC HOST VOLUMES CONFIGURATION"
        block: |
          client {
            # 启用动态host volumes
            host_volume "fnsync" {
              path = "{{ mount_point }}"
              read_only = false
            }
            
            # 添加NFS相关的节点元数据
            meta {
              nfs_server = "{{ nfs_server }}"
              nfs_share = "{{ nfs_share }}"
              nfs_mounted = "true"
            }
          }
        insertafter: 'client {'

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

    - name: Wait for Nomad to start
      wait_for:
        port: 4646
        delay: 10
        timeout: 60

    - name: Check Nomad status
      command: nomad node status
      register: nomad_status
      ignore_errors: yes

    - name: Display Nomad status
      debug:
        var: nomad_status.stdout_lines






