---
- name: 更新所有Nomad节点的Consul配置
  hosts: nomad_nodes
  become: yes
  vars:
    consul_addresses: "master.tailnet-68f9.ts.net:8500,ash3c.tailnet-68f9.ts.net:8500,warden.tailnet-68f9.ts.net:8500"
  
  tasks:
    - name: 备份原始Nomad配置
      copy:
        src: /etc/nomad.d/nomad.hcl
        dest: /etc/nomad.d/nomad.hcl.backup.{{ ansible_date_time.epoch }}
        remote_src: yes
        backup: yes
    
    - name: 更新Nomad Consul配置
      lineinfile:
        path: /etc/nomad.d/nomad.hcl
        regexp: '^\s*address\s*=\s*".*"'
        line: '  address = "{{ consul_addresses }}"'
        state: present
    
    - name: 重启Nomad服务
      systemd:
        name: nomad
        state: restarted
        enabled: yes
        daemon_reload: yes
    
    - name: 等待Nomad服务启动
      wait_for:
        port: 4646
        host: "{{ ansible_host }}"
        timeout: 30
    
    - name: 检查Nomad服务状态
      systemd:
        name: nomad
      register: nomad_status
    
    - name: 显示Nomad服务状态
      debug:
        msg: "节点 {{ inventory_hostname }} Nomad服务状态: {{ nomad_status.status.ActiveState }}"
