mgmt/ansible/consul-client-deployment.yml

80 lines
2.3 KiB
YAML

---
- name: 批量部署Consul配置到所有节点
hosts: nomad_cluster # 部署到所有Nomad集群节点
become: yes
vars:
consul_server_ips:
- "100.117.106.136" # ch4
- "100.122.197.112" # warden
- "100.116.80.94" # ash3c
tasks:
- name: 创建Consul数据目录
file:
path: /opt/consul
state: directory
owner: consul
group: consul
mode: '0755'
- name: 创建Consul数据子目录
file:
path: /opt/consul/data
state: directory
owner: consul
group: consul
mode: '0755'
- name: 创建Consul配置目录
file:
path: /etc/consul.d
state: directory
owner: consul
group: consul
mode: '0755'
- name: 检查节点类型
set_fact:
node_type: "{{ 'server' if inventory_hostname in ['ch4', 'ash3c', 'warden'] else 'client' }}"
ui_enabled: "{{ true if inventory_hostname in ['ch4', 'ash3c', 'warden'] else false }}"
bind_addr: "{{ hostvars[inventory_hostname]['tailscale_ip'] }}" # 使用inventory中指定的Tailscale IP
- name: 生成Consul配置文件
template:
src: ../infrastructure/consul/templates/consul.j2
dest: /etc/consul.d/consul.hcl
owner: root
group: root
mode: '0644'
vars:
node_name: "{{ inventory_hostname }}"
bind_addr: "{{ hostvars[inventory_hostname]['tailscale_ip'] }}"
node_zone: "{{ node_type }}"
ui_enabled: "{{ ui_enabled }}"
consul_servers: "{{ consul_server_ips }}"
- name: 验证Consul配置文件
command: consul validate /etc/consul.d/consul.hcl
register: consul_validate_result
failed_when: consul_validate_result.rc != 0
- name: 重启Consul服务
systemd:
name: consul
state: restarted
enabled: yes
- name: 等待Consul服务启动
wait_for:
port: 8500
host: "{{ hostvars[inventory_hostname]['tailscale_ip'] }}"
timeout: 60
- name: 显示Consul服务状态
systemd:
name: consul
register: consul_status
- name: 显示服务状态
debug:
msg: "{{ inventory_hostname }} ({{ node_type }}) Consul服务状态: {{ consul_status.status.ActiveState }}"