57 lines
1.7 KiB
YAML
57 lines
1.7 KiB
YAML
---
|
|
- name: 批量部署Consul配置到所有节点
|
|
hosts: all
|
|
become: yes
|
|
serial: 8 # 并行处理8个节点
|
|
|
|
vars:
|
|
consul_config_dir: "/etc/consul.d"
|
|
consul_service_name: "consul"
|
|
|
|
tasks:
|
|
- 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' }}"
|
|
node_zone: "{{ 'server' if inventory_hostname in ['ch4', 'ash3c', 'warden'] else 'client' }}"
|
|
|
|
- name: 生成Consul配置文件
|
|
template:
|
|
src: consul.j2
|
|
dest: "{{ consul_config_dir }}/consul.hcl"
|
|
owner: consul
|
|
group: consul
|
|
mode: '0644'
|
|
backup: yes
|
|
vars:
|
|
node_name: "{{ inventory_hostname }}"
|
|
bind_addr: "{{ ansible_host }}"
|
|
node_zone: "{{ node_zone }}"
|
|
ui_enabled: "{{ ui_enabled }}"
|
|
|
|
- name: 验证Consul配置文件
|
|
command: consul validate {{ consul_config_dir }}/consul.hcl
|
|
register: consul_validate
|
|
failed_when: consul_validate.rc != 0
|
|
|
|
- name: 重启Consul服务
|
|
systemd:
|
|
name: "{{ consul_service_name }}"
|
|
state: restarted
|
|
enabled: yes
|
|
|
|
- name: 等待Consul服务启动
|
|
wait_for:
|
|
port: 8500
|
|
host: "{{ ansible_host }}"
|
|
timeout: 30
|
|
|
|
- name: 检查Consul服务状态
|
|
systemd:
|
|
name: "{{ consul_service_name }}"
|
|
register: consul_status
|
|
|
|
- name: 显示部署结果
|
|
debug:
|
|
msg: "{{ inventory_hostname }} ({{ node_type }}) Consul服务状态: {{ consul_status.status.ActiveState }}"
|