Skip to content

Commit eecf1dc

Browse files
fix: Make Consul commands conditional on TLS configuration
Problem ------- Consul operator commands in remove_node.yml were hardcoded to use HTTPS and TLS certificates, causing failures when TLS is disabled on the cluster. Error encountered: FAILED! => {"attempts": 3, "cmd": ["consul", "operator", "raft", "list-peers", "-http-addr=https://127.0.0.1:8500", "-ca-file=/etc/consul/tls/ca.crt"], "msg": "non-zero return code", "rc": 1, "stderr": "Error initializing client: Error loading CA File: open /etc/consul/tls/ca.crt: no such file or directory"} Solution -------- Made all Consul CLI commands conditionally use TLS based on the consul_tls_enable variable: - Added play-level variables to eliminate code duplication: * consul_http_addr: Conditionally uses https/http * consul_ca_flag: Conditionally includes -ca-file flag * consul_client_flags: Conditionally includes client cert/key flags - Updated four Consul commands to use these variables: 1. consul operator raft list-peers (pre-removal check) 2. consul force-leave 3. consul operator raft remove-peer 4. consul operator raft list-peers (post-removal verification) Implementation -------------- Uses Jinja2 templating to check consul_tls_enable | default(false) | bool, defaulting to false if undefined. This ensures backward compatibility and allows the playbook to work with both TLS-enabled and TLS-disabled Consul clusters. Resolved with: Claude Sonnet 4.5 (Cascade IDE)
1 parent 5d1676a commit eecf1dc

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

automation/playbooks/remove_node.yml

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,20 @@
216216
gather_facts: true
217217
vars:
218218
target_node: "{{ node_to_remove | default('') }}"
219+
consul_http_addr: "{% if consul_tls_enable | default(false) | bool %}https{% else %}http{% endif %}://127.0.0.1:8500"
220+
consul_ca_flag: "{% if consul_tls_enable | default(false) | bool %}-ca-file=/etc/consul/tls/ca.crt{% endif %}"
221+
consul_client_flags: >-
222+
{% if consul_tls_enable | default(false) | bool %}
223+
-client-cert=/etc/consul/tls/server.crt -client-key=/etc/consul/tls/server.key
224+
{% endif %}
219225
tasks:
220226
- block:
221227
- name: Fetch consul cluster members before removal
222228
run_once: true # noqa run-once
223229
ansible.builtin.command: >-
224-
consul operator raft list-peers \
225-
-http-addr=https://127.0.0.1:8500 \
226-
-ca-file=/etc/consul/tls/ca.crt
230+
consul operator raft list-peers
231+
-http-addr={{ consul_http_addr }}
232+
{{ consul_ca_flag }}
227233
changed_when: false
228234
register: consul_members_list_before
229235
until: consul_members_list_before.rc == 0
@@ -250,10 +256,10 @@
250256
- name: Force-leave target node from consul cluster
251257
run_once: true # noqa run-once
252258
ansible.builtin.command: >-
253-
consul force-leave \
254-
-http-addr=https://127.0.0.1:8500 \
255-
-ca-file=/etc/consul/tls/ca.crt \
256-
{{ hostvars[target_node].ansible_hostname | default(target_node) }}
259+
consul force-leave
260+
-http-addr={{ consul_http_addr }}
261+
{{ consul_ca_flag }}
262+
{{ hostvars[target_node].ansible_hostname | default(target_node) }}
257263
when:
258264
- inventory_hostname != target_node
259265
- consul_members_list_before.stdout | default('') is search(hostvars[target_node].ansible_hostname | default(target_node))
@@ -278,11 +284,10 @@
278284
- name: Remove target node from the Raft configuration
279285
run_once: true # noqa run-once
280286
ansible.builtin.command: >-
281-
consul operator raft remove-peer -id="{{ target_raft_id }}" \
282-
-http-addr=https://127.0.0.1:8500 \
283-
-ca-file=/etc/consul/tls/ca.crt \
284-
-client-cert=/etc/consul/tls/server.crt \
285-
-client-key=/etc/consul/tls/server.key
287+
consul operator raft remove-peer -id="{{ target_raft_id }}"
288+
-http-addr={{ consul_http_addr }}
289+
{{ consul_ca_flag }}
290+
{{ consul_client_flags }}
286291
register: raft_remove_result
287292
until: raft_remove_result.rc == 0
288293
retries: 3
@@ -313,9 +318,9 @@
313318
- name: Fetch consul cluster members after removal
314319
run_once: true # noqa run-once
315320
ansible.builtin.command: >-
316-
consul operator raft list-peers \
317-
-http-addr=https://127.0.0.1:8500 \
318-
-ca-file=/etc/consul/tls/ca.crt
321+
consul operator raft list-peers
322+
-http-addr={{ consul_http_addr }}
323+
{{ consul_ca_flag }}
319324
changed_when: false
320325
register: consul_members_list_after
321326
until: consul_members_list_after.rc == 0

0 commit comments

Comments
 (0)