forked from chrisvanmeer/at-hashi-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_consul-deployment.yml
437 lines (375 loc) · 19 KB
/
03_consul-deployment.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
---
- name: "AT Computing - HashiCorp Demo - Consul"
hosts: all
become: true
vars_files:
- vars/hashicorp/general.yml
- vars/hashicorp/consul.yml
# vars:
# Uncomment and fill the variables below if you need to run the playbook again.
# You can find the relevant data on any of the hosts in /etc/consul.d/consul.hcl
# consul_ecryption_key: "<ENCRYPTION KEY>"
tasks:
- name: "HashiCorp - Consul : Ensure data directory is present."
ansible.builtin.file:
path: "{{ consul_data_directory }}"
state: directory
mode: "0755"
owner: "{{ consul_data_directory_user }}"
group: "{{ consul_data_directory_group }}"
- name: "HashiCorp - Consul : Ensure etc directory is present."
ansible.builtin.file:
path: "{{ consul_etc_directory }}"
state: directory
mode: "0755"
owner: "{{ consul_data_directory_user }}"
group: "{{ consul_data_directory_group }}"
- name: "HashiCorp - Consul : Ensure log directory is present."
ansible.builtin.file:
path: "{{ consul_log_directory }}"
state: directory
mode: "0755"
owner: "{{ consul_log_directory_owner }}"
group: "{{ consul_data_directory_group }}"
- name: "HashiCorp - Consul : Ensure Consul is excluded from the unattended upgrades."
ansible.builtin.lineinfile:
path: "/usr/share/unattended-upgrades/50unattended-upgrades"
state: present
line: ' "consul";'
insertafter: 'Unattended-Upgrade::Package-Blacklist \{\n'
backup: true
# ENCRYPTION
- name: "HashiCorp - Consul : Encryption."
block:
# CA CERTIFICATES
- name: "HashiCorp - Consul : CA Certificate."
block:
# FIRST SERVER
- name: "HashiCorp - Consul : First server actions."
block:
- name: "HashiCorp - Consul : Register first server as a fact."
ansible.builtin.set_fact:
consul_first_server_hostname: "{{ inventory_hostname }}"
delegate_to: "{{ groups['servers'] | first }}"
run_once: true
- name: "HashiCorp - Consul : Look for existing CA certificate on first server as benchmark."
ansible.builtin.stat:
path: "{{ consul_etc_directory }}/consul-agent-ca.pem"
register: consul_agent_ca_file_first
- name: "HashiCorp - Consul : CA Certificate creation."
block:
- name: "HashiCorp - Consul : Create CA Certificate if not exists."
ansible.builtin.command: "{{ consul_binary }} tls ca create -days {{ consul_tls_ca_certificate_days }}"
args:
chdir: "{{ consul_etc_directory }}"
changed_when: true
register: consul_tls_ca_certificate_create
- name: "HashiCorp - Consul : Get file location of CA Certificate."
ansible.builtin.set_fact:
consul_tls_ca_certificate_file: "{{ consul_etc_directory }}/{{ consul_tls_ca_certificate_create.stdout_lines[0].split('==> Saved ')[1] }}"
- name: "HashiCorp - Consul : Get contents of CA Certificate."
ansible.builtin.command: "cat {{ consul_tls_ca_certificate_file }}"
register: consul_tls_ca_certificate_content_tmp
changed_when: false
- name: "HashiCorp - Consul : Register contents of CA Certificate as a fact."
ansible.builtin.set_fact:
consul_tls_ca_certificate_content: "{{ consul_tls_ca_certificate_content_tmp.stdout }}"
- name: "HashiCorp - Consul : Get file location of CA key."
ansible.builtin.set_fact:
consul_tls_ca_key_file: "{{ consul_etc_directory }}/{{ consul_tls_ca_certificate_create.stdout_lines[1].split('==> Saved ')[1] }}"
- name: "HashiCorp - Consul : Get contents of CA key."
ansible.builtin.command: "cat {{ consul_tls_ca_key_file }}"
register: consul_tls_ca_key_content_tmp
changed_when: false
- name: "HashiCorp - Consul : Register contents of CA key as a fact."
ansible.builtin.set_fact:
consul_tls_ca_key_content: "{{ consul_tls_ca_key_content_tmp.stdout }}"
when:
- ansible_hostname in groups['servers']
- not consul_agent_ca_file_first.stat.exists
delegate_to: "{{ groups['servers'] | first }}"
run_once: true
# OTHER NODES
- name: "HashiCorp - Consul - CA : Ensure CA certificate and key are present on all other nodes."
ansible.builtin.copy:
dest: "{{ consul_etc_directory }}/{{ item.dest }}"
content: "{{ item.content }}"
owner: "{{ consul_data_directory_user }}"
group: "{{ consul_data_directory_group }}"
mode: "{{ item.mode }}"
with_items:
- dest: "consul-agent-ca.pem"
content: "{{ consul_tls_ca_certificate_content }}"
mode: "0644"
- dest: "consul-agent-ca-key.pem"
content: "{{ consul_tls_ca_key_content }}"
mode: "0600"
loop_control:
label: "{{ item.dest }}"
when:
- consul_tls_ca_certificate_content is defined
- inventory_hostname != consul_first_server_hostname
# SERVER CERTIFICATES
- name: "HashiCorp - Consul : Server Certificates."
block:
- name: "HashiCorp - Consul : Look for existing server certificate."
ansible.builtin.stat:
path: "{{ consul_etc_directory }}/{{ hashicorp_datacenter_name }}-server-consul-0.pem"
register: consul_server_certificate_details
- name: "HashiCorp - Consul : Create server certificate."
ansible.builtin.command: "{{ consul_binary }} tls cert create -server -dc {{ hashicorp_datacenter_name }} -days {{ consul_tls_server_certificate_days }}"
args:
chdir: "{{ consul_etc_directory }}"
when: not consul_server_certificate_details.stat.exists
when: ansible_hostname in groups['servers']
# CLIENT CERTIFICATES
- name: "HashiCorp - Consul : Client Certificates."
block:
- name: "HashiCorp - Consul : Look for existing client certificate."
ansible.builtin.stat:
path: "{{ consul_etc_directory }}/{{ hashicorp_datacenter_name }}-client-consul-0.pem"
register: consul_client_certificate_details
- name: "HashiCorp - Consul : Create client certificate."
ansible.builtin.command: "{{ consul_binary }} tls cert create -client -dc {{ hashicorp_datacenter_name }} -days {{ consul_tls_client_certificate_days }}"
args:
chdir: "{{ consul_etc_directory }}"
when: not consul_client_certificate_details.stat.exists
when: ansible_hostname in groups['clients']
# ENCRYPTION KEY
- name: "HashiCorp - Consul : Check the first server if we are dealing with an already running cluster."
ansible.builtin.stat:
path: "{{ consul_systemd_service_file }}"
register: consul_systemd_service_file_details
- name: "HashiCorp - Consul : Encryption key."
block:
- name: "HashiCorp - Consul : Generate encryption key when cluster is not running yet."
block:
- name: "HashiCorp - Consul : Run the consul keygen command."
ansible.builtin.command: "consul keygen"
register: consul_ecryption_key_tmp
changed_when: false
- name: "HashiCorp - Consul : Retrieve encryption key and store it as a fact."
ansible.builtin.set_fact:
consul_ecryption_key: "{{ consul_ecryption_key_tmp.stdout }}"
when:
- ansible_hostname in groups['servers']
- not consul_systemd_service_file_details.stat.exists
delegate_to: "{{ groups['servers'] | first }}"
run_once: true
when: consul_ecryption_key is not defined
- name: "HashiCorp - Consul : Find all certificates."
ansible.builtin.find:
paths: "{{ consul_etc_directory }}"
patterns: "*.pem"
register: consul_certificate_files
tags: chown
- name: "HashiCorp - Consul : Ensure the correct file owner / group is set for all certificates."
ansible.builtin.file:
path: "{{ item.path }}"
owner: "{{ consul_data_directory_user }}"
group: "{{ consul_data_directory_group }}"
with_items: "{{ consul_certificate_files.files }}"
loop_control:
label: "{{ item.path }}"
tags: chown
# CONSUL CONFIGURATION
- name: "HashiCorp - Consul : Configuration."
block:
- name: "HashiCorp - Consul : Ensure configuration is present."
ansible.builtin.template:
src: "templates/hashicorp/consul/consul.hcl.j2"
dest: "{{ consul_etc_directory }}/consul.hcl"
mode: "0644"
owner: "{{ consul_data_directory_user }}"
group: "{{ consul_data_directory_group }}"
notify: Restart Consul
- name: "HashiCorp - Consul : Ensure systemd service is present."
ansible.builtin.template:
src: "templates/hashicorp/consul/consul.service.j2"
dest: "{{ consul_systemd_service_file }}"
mode: "0644"
- name: "HashiCorp - Consul : Ensure systemd is reloaded."
ansible.builtin.systemd:
daemon_reload: true
- name: "HashiCorp - Consul : Ensure systemd service is enabled and started."
ansible.builtin.systemd:
name: "{{ consul_service_name }}"
enabled: true
state: started
- name: "HashiCorp - Consul : A little pause to allow for the consul members to find each other. You could check with 'consul members' on a random server to see if there is output."
ansible.builtin.pause:
seconds: "{{ consul_pause_seconds }}"
- name: "HashiCorp - Consul : Run consul members command."
ansible.builtin.command: "{{ consul_binary }} members"
changed_when: false
ignore_errors: true
register: consul_members_output
- name: "HashiCorp - Consul : Ouput the results of the consul members command."
ansible.builtin.debug:
var: consul_members_output.stdout_lines
# CONSUL ACL BOOTSTRAPPING
- name: "HashiCorp - Consul - ACL : Check if system is already bootstrapped."
ansible.builtin.stat:
path: "{{ consul_bootstrapped_true_file }}"
register: consul_bootstrapped
when: ansible_hostname in groups['servers']
delegate_to: "{{ groups['servers'] | first }}"
run_once: true
- name: "HashiCorp - Consul - Bootstrap ACL."
block:
- name: "HashiCorp - Consul - ACL : Run consul acl bootstrap command."
ansible.builtin.command: "{{ consul_binary }} acl bootstrap"
changed_when: false
register: consul_acl_bootstrap_raw
- name: "HashiCorp - Consul - ACL : Store bootstrap token local for safekeeping."
ansible.builtin.copy:
content: "{{ consul_acl_bootstrap_raw.stdout }}"
dest: "{{ consul_bootstrap_token_local_path }}"
mode: "0600"
become: false
delegate_to: localhost
when:
- ansible_hostname in groups['servers']
- not consul_bootstrapped.stat.exists
delegate_to: "{{ groups['servers'] | first }}"
run_once: true
- name: "HashiCorp - Consul - ACL : Retrieve Secret ID from bootstrap output."
ansible.builtin.shell: "awk '/SecretID/ {print $2}' {{ consul_bootstrap_token_local_path }}"
changed_when: false
become: false
delegate_to: localhost
register: consul_acl_bootstrap_output
- name: "HashiCorp - Consul - ACL : Set the Secret ID as a fact."
ansible.builtin.set_fact:
consul_acl_bootstrap_secret_id: "{{ consul_acl_bootstrap_output.stdout }}"
# AGENT TOKEN
- name: "HashiCorp - Consul - Agent."
block:
- name: "HashiCorp - Consul - Agent : Ensure consul-agent-policy file is present on the system."
ansible.builtin.copy:
src: "{{ consul_agent_policy_file_local }}"
dest: "{{ consul_agent_policy_file_remote }}"
mode: 0644
- name: "HashiCorp - Consul - Agent : Ensure consul-agent policy is registered."
ansible.builtin.command: "{{ consul_binary }} acl policy create -name {{ consul_agent_policy_name }} -rules @{{ consul_agent_policy_file_name }}"
environment:
CONSUL_HTTP_TOKEN: "{{ consul_acl_bootstrap_secret_id }}"
args:
chdir: "{{ consul_data_directory }}/"
changed_when: false
ignore_errors: true
- name: "HashiCorp - Consul - Agent : Ensure consul agent token is generated."
ansible.builtin.command: "{{ consul_binary }} acl token create -description '{{ consul_agent_policy_description }}' -policy-name {{ consul_agent_policy_name }}"
environment:
CONSUL_HTTP_TOKEN: "{{ consul_acl_bootstrap_secret_id }}"
changed_when: false
register: agent_policy_token_output_raw
- name: "HashiCorp - Consul - Agent : Store consul agent token local for safekeeping."
ansible.builtin.copy:
content: "{{ agent_policy_token_output_raw.stdout }}"
dest: "{{ consul_agent_token_local_path }}"
mode: 0644
become: false
delegate_to: localhost
- name: "HashiCorp - Consul - Agent : Retrieve Secret ID from agent token output."
ansible.builtin.shell: "awk '/SecretID/ {print $2}' {{ consul_agent_token_local_path }}"
changed_when: false
become: false
delegate_to: localhost
register: consul_agent_token_output
- name: "HashiCorp - Consul - Agent : Create a fact of the agent token."
ansible.builtin.set_fact:
consul_agent_token: "{{ consul_agent_token_output.stdout }}"
- name: "HashiCorp - Consul - Agent : Cleanup the placed policy file."
ansible.builtin.file:
path: "{{ consul_agent_policy_file_remote }}"
state: absent
when:
- ansible_hostname in groups['servers']
delegate_to: "{{ groups['servers'] | first }}"
run_once: true
- name: "HashiCorp - Consul - Agent : Ensure agent token is part of the configuration."
ansible.builtin.lineinfile:
path: "{{ consul_etc_directory }}/consul.hcl"
regexp: '#agent = "<WILL BE FILLED LATER>"'
line: ' agent = "{{ consul_agent_token }}"'
notify: Restart Consul
# DEFAULT DENY
- name: "HashiCorp - Consul - ACL : Ensure default ACL is set to deny."
ansible.builtin.lineinfile:
path: "{{ consul_etc_directory }}/consul.hcl"
regexp: 'default_policy = "allow"'
line: ' default_policy = "deny"'
notify: Restart Consul
# DNS REQUESTS
- name: "HashiCorp - Consul - ACL : Ensure the DNS request policy file is in place."
ansible.builtin.copy:
src: "{{ consul_dns_policy_file_local }}"
dest: "{{ consul_dns_policy_file_remote }}"
mode: 0644
- name: "HashiCorp - Consul - ACL : DNS requests."
block:
- name: "HashiCorp - Consul - ACL : Ensure that the policy is present in Consul."
ansible.builtin.command: "{{ consul_binary }} acl policy create -name {{ consul_dns_policy_name }} -rules @{{ consul_dns_policy_file_name }}"
environment:
CONSUL_HTTP_TOKEN: "{{ consul_acl_bootstrap_secret_id }}"
args:
chdir: "{{ consul_data_directory }}/"
changed_when: false
- name: "HashiCorp - Consul - ACL : Create ACL token using the above policy."
ansible.builtin.command: "{{ consul_binary }} acl token create -description '{{ consul_dns_policy_description }}' -policy-name {{ consul_dns_policy_name }}"
environment:
CONSUL_HTTP_TOKEN: "{{ consul_acl_bootstrap_secret_id }}"
changed_when: false
register: dns_policy_token_output_raw
- name: "HashiCorp - Consul - ACL : Store bootstrap token local for safekeeping."
ansible.builtin.copy:
content: "{{ dns_policy_token_output_raw.stdout }}"
dest: "{{ consul_dns_token_local_path }}"
mode: 0644
become: false
delegate_to: localhost
- name: "HashiCorp - Consul - ACL : Retrieve Secret ID from bootstrap output."
ansible.builtin.shell: "awk '/SecretID/ {print $2}' {{ consul_dns_token_local_path }}"
changed_when: false
become: false
delegate_to: localhost
register: dns_token_output
- name: "HashiCorp - Consul - ACL : Cleanup the placed policy file."
ansible.builtin.file:
path: "{{ consul_dns_policy_file_remote }}"
state: absent
delegate_to: "{{ groups['servers'] | first }}"
run_once: true
- name: "HashiCorp - Consul - ACL : Set the DNS token as default for the Consul agent."
ansible.builtin.command: "{{ consul_binary }} acl set-agent-token default '{{ dns_token_output.stdout }}'"
environment:
CONSUL_HTTP_TOKEN: "{{ consul_acl_bootstrap_secret_id }}"
changed_when: false
# CONSUL SERVICE MESH
- name: "HashiCorp - Consul - Service Mesh"
block:
- name: "HashiCorp - Consul - Service Mesh : Ensure CNI directory exists."
ansible.builtin.file:
path: "{{ consul_cni_target_directory }}"
state: directory
mode: "0644"
- name: "HashiCorp - Consul - Service Mesh : Ensure CNI plugins exist in the CNI directory."
ansible.builtin.unarchive:
src: "{{ consul_cni_plugin_url }}"
dest: "{{ consul_cni_target_directory }}"
remote_src: yes
- name: "HashiCorp - Consul - Service Mesh : Ensure sysctl settings are present and loaded."
ansible.posix.sysctl:
name: "{{ item.key }}"
value: "{{ item.value }}"
state: present
sysctl_file: "{{ consul_cni_sysctl_dest_file }}"
reload: yes
with_items: "{{ consul_cni_sysctl_settings }}"
when: ansible_hostname in groups['clients']
handlers:
- name: Restart Consul
ansible.builtin.service:
name: "{{ consul_service_name }}"
state: restarted