mgmt/ansible/playbooks/consul-persistent-storage.yml

137 lines
3.5 KiB
YAML

---
- name: Configure Consul Persistent Storage
hosts: ch4,ash3c,warden
become: yes
gather_facts: yes
vars:
consul_data_dir: "/opt/consul/data"
nomad_config_file: "/etc/nomad.d/nomad.hcl"
tasks:
- name: Create consul data directory
file:
path: "{{ consul_data_dir }}"
state: directory
owner: nomad
group: nomad
mode: '0755'
- name: Backup existing nomad configuration
copy:
src: "{{ nomad_config_file }}"
dest: "{{ nomad_config_file }}.backup.{{ ansible_date_time.epoch }}"
remote_src: yes
backup: yes
- name: Check if consul-data host volume already configured
lineinfile:
path: "{{ nomad_config_file }}"
line: ' host_volume "consul-data" {'
state: absent
check_mode: yes
register: consul_volume_check
changed_when: false
- name: Add consul-data host volume configuration
blockinfile:
path: "{{ nomad_config_file }}"
marker: "# {mark} CONSUL PERSISTENT STORAGE"
block: |
# Consul 持久化存储
client {
host_volume "consul-data" {
path = "{{ consul_data_dir }}"
read_only = false
}
}
insertafter: EOF
when: consul_volume_check is not changed
notify: restart nomad
- name: Verify consul data directory permissions
file:
path: "{{ consul_data_dir }}"
owner: nomad
group: nomad
mode: '0755'
state: directory
- name: Display consul data directory info
stat:
path: "{{ consul_data_dir }}"
register: consul_dir_stat
- name: Show directory information
debug:
msg: |
Consul data directory: {{ consul_data_dir }}
Owner: {{ consul_dir_stat.stat.pw_name }}:{{ consul_dir_stat.stat.gr_name }}
Permissions: {{ consul_dir_stat.stat.mode }}
handlers:
- name: restart nomad
systemd:
name: nomad
state: restarted
daemon_reload: yes
- name: wait for nomad
wait_for:
port: 4646
host: "{{ ansible_host }}"
delay: 5
timeout: 30
listen: restart nomad
- name: Verify Nomad client status
hosts: ch4,ash3c,warden
become: yes
gather_facts: no
tasks:
- name: Check nomad service status
systemd:
name: nomad
register: nomad_status
- name: Display nomad status
debug:
msg: |
Node: {{ inventory_hostname }}
Nomad Status: {{ nomad_status.status.ActiveState }}
- name: Verify nomad client connectivity
uri:
url: "http://{{ ansible_host }}:4646/v1/status/leader"
method: GET
timeout: 10
register: nomad_api_check
ignore_errors: yes
- name: Show connectivity result
debug:
msg: |
Node: {{ inventory_hostname }}
API Check: {{ 'SUCCESS' if nomad_api_check.status == 200 else 'FAILED' }}
- name: Summary Report
hosts: localhost
gather_facts: no
run_once: true
tasks:
- name: Display completion summary
debug:
msg: |
✅ Consul 持久化存储配置完成!
已配置节点: ch4, ash3c, warden
数据目录: /opt/consul/data
权限: nomad:nomad (755)
下一步:
1. 部署持久化 Consul job
2. 验证集群状态
3. 恢复 KV 数据