Files
ansible-homelab/roles/consul/tasks/main.yml
T
2026-02-04 10:00:08 -06:00

105 lines
2.3 KiB
YAML

---
- name: Install dependencies
apt:
name:
- curl
- gnupg
- lsb-release
state: present
update_cache: yes
- name: Add HashiCorp GPG key
apt_key:
url: https://apt.releases.hashicorp.com/gpg
state: present
- name: Add HashiCorp repository
apt_repository:
repo: "deb [arch=amd64] https://apt.releases.hashicorp.com {{ ansible_distribution_release }} main"
state: present
filename: hashicorp
- name: Install Consul
apt:
name: "consul={{ consul_version }}-1"
state: present
update_cache: yes
- name: Create Consul data directory
file:
path: "{{ consul_data_dir }}"
state: directory
owner: consul
group: consul
mode: '0755'
- name: Create Consul config directory
file:
path: "{{ consul_config_dir }}"
state: directory
owner: consul
group: consul
mode: '0755'
- name: Generate encryption key if not provided
command: consul keygen
register: consul_generated_key
when: consul_encrypt_key == "" and consul_server
run_once: true
changed_when: false
- name: Set encryption key fact
set_fact:
consul_encrypt_key: "{{ consul_generated_key.stdout }}"
when: consul_encrypt_key == "" and consul_generated_key.stdout is defined
run_once: true
- name: Deploy Consul configuration
template:
src: consul.hcl.j2
dest: "{{ consul_config_dir }}/consul.hcl"
owner: consul
group: consul
mode: '0640'
notify: Restart Consul
- name: Deploy server configuration
template:
src: server.hcl.j2
dest: "{{ consul_config_dir }}/server.hcl"
owner: consul
group: consul
mode: '0640'
when: consul_server
notify: Restart Consul
- name: Remove server config on clients
file:
path: "{{ consul_config_dir }}/server.hcl"
state: absent
when: not consul_server
notify: Restart Consul
- name: Enable and start Consul service
systemd:
name: consul
enabled: yes
state: started
daemon_reload: yes
- name: Wait for Consul to be ready
uri:
url: "http://127.0.0.1:8500/v1/status/leader"
method: GET
register: consul_leader
until: consul_leader.status == 200
retries: 30
delay: 2
when: consul_server
- name: Display Consul encryption key
debug:
msg: "Consul encryption key: {{ consul_encrypt_key }}"
when: consul_server and consul_encrypt_key != ""
run_once: true