---
- name: 实现路由反射器架构 - 所有节点通过Traefik访问Consul
  hosts: nomad_nodes
  become: yes
  vars:
    traefik_endpoint: "hcp1.tailnet-68f9.ts.net:80"
  
  tasks:
    - name: 📊 显示架构优化信息
      debug:
        msg: |
          🎯 实现BGP路由反射器模式
          📉 连接数优化：Full Mesh (54连接) → Star Topology (21连接)
          🌐 所有节点 → Traefik → Consul Leader
      run_once: true

    - name: 🔍 检查当前Consul配置
      shell: grep "address.*=" /etc/nomad.d/nomad.hcl
      register: current_config
      ignore_errors: yes

    - name: 📋 显示当前配置
      debug:
        msg: "当前配置: {{ current_config.stdout }}"

    - name: 🔧 更新Consul地址为Traefik端点
      replace:
        path: /etc/nomad.d/nomad.hcl
        regexp: 'address = "[^"]*"'
        replace: 'address = "{{ traefik_endpoint }}"'
      notify: restart nomad

    - name: ✅ 验证配置更新
      shell: grep "address.*=" /etc/nomad.d/nomad.hcl
      register: new_config

    - name: 📋 显示新配置
      debug:
        msg: "新配置: {{ new_config.stdout }}"

  handlers:
    - name: restart nomad
      systemd:
        name: nomad
        state: restarted
