From 75bac75a214a0c3ac5559a8801806379d2b84c7c Mon Sep 17 00:00:00 2001 From: jeremy Date: Wed, 4 Feb 2026 10:00:08 -0600 Subject: [PATCH] Add Consul role - main.yml --- roles/consul/tasks/main.yml | 104 ++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 roles/consul/tasks/main.yml diff --git a/roles/consul/tasks/main.yml b/roles/consul/tasks/main.yml new file mode 100644 index 0000000..b2eea88 --- /dev/null +++ b/roles/consul/tasks/main.yml @@ -0,0 +1,104 @@ +--- +- 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