forked from vitabaks/autobase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy_pgcluster.yml
464 lines (399 loc) · 16.8 KB
/
deploy_pgcluster.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
---
- name: Deploy PostgreSQL HA Cluster (based on "Patroni")
hosts: localhost
gather_facts: true
any_errors_fatal: true
pre_tasks:
- name: Include main variables
ansible.builtin.include_vars: "roles/common/defaults/main.yml"
tags: always
# set_fact: 'pgbackrest_install' to configure Postgres backups (TODO: Add the ability to configure backups in the UI)
# Note: Applicable only for "aws", "gcp", "azure", because:
# "digitalocean" - requires the Spaces access keys ("AWS_ACCESS_KEY_ID" and "AWS_SECRET_ACCESS_KEY" variables)
# "hetzner" - requires the S3 credentials ("hetzner_object_storage_access_key" and "hetzner_object_storage_secret_key" variables).
- name: "Set variable: 'pgbackrest_install' to configure Postgres backups"
ansible.builtin.set_fact:
pgbackrest_install: true
when:
- not (pgbackrest_install | bool or wal_g_install | bool)
- cloud_provider | default('') | lower in ['aws', 'gcp', 'azure']
- pgbackrest_auto_conf | default(true) | bool # to be able to disable auto backup settings
tags: always
roles:
- role: cloud_resources
when: cloud_provider | default('') | length > 0
tags: always
- name: deploy_pgcluster.yml | Perform pre-checks
hosts: all
become: true
become_method: sudo
gather_facts: true
any_errors_fatal: true
environment: "{{ proxy_env | default({}) }}"
pre_tasks:
- name: Include main variables
ansible.builtin.include_vars: "roles/common/defaults/main.yml"
tags: always
- name: Include system variables
ansible.builtin.include_vars: "roles/common/defaults/system.yml"
tags: always
- name: Include OS-specific variables
ansible.builtin.include_vars: "roles/common/defaults/{{ ansible_os_family }}.yml"
tags: always
- name: System info
ansible.builtin.debug:
msg:
server_name: "{{ hostname | default(ansible_hostname) }}"
server_location: "{{ server_location | default(omit) }}"
ip_address: "{{ inventory_hostname | default('N/A') }}"
os: "{{ ansible_distribution | default('N/A') }} {{ ansible_distribution_version | default('N/A') }}"
kernel: "{{ ansible_kernel | default('N/A') }}"
cpu:
model: "{{ ansible_processor[2] | default('N/A') }}"
count: "{{ ansible_processor_count | default('N/A') }}"
cores: "{{ ansible_processor_cores | default('N/A') }}"
memory: "{{ (ansible_memtotal_mb / 1024) | round(2) if ansible_memtotal_mb is defined else 'N/A' }} GB"
disk_space_total: >-
{{
(ansible_mounts
| map(attribute='size_total')
| map('int')
| sum / 1024 / 1024 / 1024
)
| round(2) if ansible_mounts is defined else 'N/A'
}} GB
architecture: "{{ ansible_architecture | default('N/A') }}"
virtualization_type: "{{ ansible_virtualization_type | default('N/A') }}"
product_name: "{{ ansible_product_name | default('N/A') }}"
tags: always
# if 'cloud_provider' is 'aws', 'gcp', or 'azure'
# set_fact: 'pgbackrest_install' to configure Postgres backups (TODO: Add the ability to configure backups in the UI)
- name: "Set variable: 'pgbackrest_install' to configure Postgres backups"
ansible.builtin.set_fact:
pgbackrest_install: true
when:
- not (pgbackrest_install | bool or wal_g_install | bool)
- cloud_provider | default('') | lower in ['aws', 'gcp', 'azure']
- pgbackrest_auto_conf | default(true) | bool # to be able to disable auto backup settings
tags: always
- name: Clean dnf cache
ansible.builtin.command: dnf clean all
when:
- ansible_os_family == "RedHat"
- name: Update apt cache
ansible.builtin.apt:
update_cache: true
cache_valid_time: 3600
register: apt_status
until: apt_status is success
delay: 5
retries: 3
when: ansible_os_family == "Debian"
- name: Make sure the gnupg and apt-transport-https packages are present
ansible.builtin.apt:
pkg:
- gnupg
- apt-transport-https
state: present
register: apt_status
until: apt_status is success
delay: 5
retries: 3
when: ansible_os_family == "Debian"
# Ansible requires the iproute package for network facts to be populated
- name: Make sure that the iproute is installed
ansible.builtin.package:
name: iproute
state: present
register: package_status
until: package_status is success
delay: 5
retries: 3
when: ansible_os_family == "RedHat"
- name: Make sure that the iproute is installed
ansible.builtin.apt:
name: iproute2
state: present
register: apt_status
until: apt_status is success
delay: 5
retries: 3
when: ansible_os_family == "Debian"
# (optional) Command or script to be executed before the Postgres cluster deployment.
- block:
- name: Print pre-deploy command
ansible.builtin.debug:
var: pre_deploy_command
when: pre_deploy_command_print | default(false) | bool
- name: Run pre-deploy command
ansible.builtin.shell: "{{ pre_deploy_command }} > {{ pre_deploy_command_log | default('/var/tmp/pre_deploy_command.log') }} 2>&1"
args:
executable: /bin/bash
register: pre_deploy_result
delegate_to: "{{ item }}"
loop: "{{ pre_deploy_command_hosts.split(',') | map('extract', groups) | list | flatten }}"
async: "{{ pre_deploy_command_timeout }}" # run the command asynchronously
poll: 0
- name: Wait for the pre-deploy command to complete
ansible.builtin.async_status:
jid: "{{ item.ansible_job_id }}"
register: pre_deploy_job_result
delegate_to: "{{ item.item }}"
loop: "{{ pre_deploy_result.results }}"
loop_control:
label: "{{ item.item }}"
until: pre_deploy_job_result.finished
retries: "{{ (pre_deploy_command_timeout | int) // 10 }}"
delay: 10
ignore_errors: true # allows to collect logs before stopping execution (in case of failure)
when:
- pre_deploy_result.results is defined
- item.ansible_job_id is defined
- name: Get pre-deploy command log
ansible.builtin.command: "cat {{ pre_deploy_command_log | default('/var/tmp/pre_deploy_command.log') }}"
register: pre_deploy_log_content
delegate_to: "{{ item }}"
loop: "{{ pre_deploy_command_hosts.split(',') | map('extract', groups) | list | flatten }}"
changed_when: false
when: pre_deploy_command_print_result | default(false) | bool
- name: Print pre-deploy command result
ansible.builtin.debug:
msg: "{{ item.stdout_lines }}"
loop: "{{ pre_deploy_log_content.results }}"
loop_control:
label: "{{ item.item }}"
when:
- pre_deploy_log_content.results is defined
- item.stdout_lines is defined
- name: Stop if pre-deploy command failed
ansible.builtin.fail:
msg: "Pre-deploy command failed. See log for details."
when: pre_deploy_job_result.results | json_query('[?failed]') | length > 0
run_once: true # noqa run-once
when: pre_deploy_command | default('') | length > 0
tags: pre_deploy, pre_deploy_command
roles:
# (optional) if 'ssh_public_keys' is defined
- role: authorized_keys
tags: ssh_public_keys
- role: pre_checks
vars:
minimal_ansible_version: 2.16.0
timescale_minimal_pg_version: 12 # if enable_timescale is defined
tags: always
- role: hostname
- role: tls_certificate/generate
vars:
tls_group_name: "postgres_cluster"
tls_cert_regenerate: "{{ patroni_tls_cert_regenerate | default(false) }}" # Do not generate new certificates if they already exist.
when: tls_cert_generate | bool
- name: deploy_pgcluster.yml | Deploy etcd cluster
ansible.builtin.import_playbook: etcd_cluster.yml
when: not dcs_exists|bool and dcs_type == "etcd"
tags: etcd
- name: deploy_pgcluster.yml | Deploy Consul
ansible.builtin.import_playbook: consul.yml
when: dcs_type == "consul"
tags: consul
- name: deploy_pgcluster.yml | Postgres Cluster Configuration
hosts: postgres_cluster
become: true
become_method: sudo
gather_facts: true
any_errors_fatal: true
environment: "{{ proxy_env | default({}) }}"
pre_tasks:
- name: Include main variables
ansible.builtin.include_vars: "roles/common/defaults/main.yml"
tags: always
- name: Include system variables
ansible.builtin.include_vars: "roles/common/defaults/system.yml"
tags: always
- name: Include OS-specific variables
ansible.builtin.include_vars: "roles/common/defaults/{{ ansible_os_family }}.yml"
tags: always
- name: Build a firewall_ports_dynamic_var
ansible.builtin.set_fact:
firewall_ports_dynamic_var: "{{ firewall_ports_dynamic_var | default([]) + (firewall_allowed_tcp_ports_for[item] | default([])) }}"
loop: "{{ hostvars[inventory_hostname].group_names }}"
when: firewall_enabled_at_boot | bool
tags: firewall
- name: Build a firewall_rules_dynamic_var
ansible.builtin.set_fact:
firewall_rules_dynamic_var: "{{ firewall_rules_dynamic_var | default([]) + (firewall_additional_rules_for[item] | default([])) }}"
loop: "{{ hostvars[inventory_hostname].group_names }}"
when: firewall_enabled_at_boot | bool
tags: firewall
# if 'dcs_type' is 'consul'
- name: Add a nameserver entry poining to localhost for dnsmasq
ansible.builtin.set_fact:
nameservers: "{{ ['127.0.0.1'] + (nameservers | default([])) }}"
when: dcs_type == "consul" and consul_dnsmasq_enable | bool and ('127.0.0.1' not in (nameservers | default([])))
roles:
- role: firewall
vars:
firewall_allowed_tcp_ports: "{{ firewall_ports_dynamic_var | default([]) | unique }}"
firewall_additional_rules: "{{ firewall_rules_dynamic_var | default([]) | unique }}"
when: firewall_enabled_at_boot|bool
tags: firewall
- role: resolv_conf
- role: etc_hosts
- role: add_repository
- role: packages
- role: sudo
- role: mount
- role: swap
- role: sysctl
- role: transparent_huge_pages
- role: pam_limits
- role: io_scheduler
- role: locales
- role: timezone
- role: ntp
- role: ssh_keys
- role: copy
- name: deploy_pgcluster.yml | Deploy balancers
ansible.builtin.import_playbook: balancers.yml
when: with_haproxy_load_balancing|bool
tags: load_balancing, haproxy
- name: deploy_pgcluster.yml | Install and configure pgBackRest
hosts: pgbackrest:postgres_cluster
become: true
become_method: sudo
gather_facts: true
any_errors_fatal: true
pre_tasks:
- name: Include main variables
ansible.builtin.include_vars: "roles/common/defaults/main.yml"
tags: always
- name: Include OS-specific variables
ansible.builtin.include_vars: "roles/common/defaults/{{ ansible_os_family }}.yml"
tags: always
# if 'cloud_provider' is 'aws', 'gcp', or 'azure'
# set_fact: 'pgbackrest_install' to configure Postgres backups (TODO: Add the ability to configure backups in the UI)
- name: "Set variable: 'pgbackrest_install' to configure Postgres backups"
ansible.builtin.set_fact:
pgbackrest_install: true
when:
- not (pgbackrest_install | bool or wal_g_install | bool)
- cloud_provider | default('') | lower in ['aws', 'gcp', 'azure']
- pgbackrest_auto_conf | default(true) | bool # to be able to disable auto backup settings
tags: always
roles:
- role: pgbackrest
when: pgbackrest_install | bool
- name: deploy_pgcluster.yml | PostgreSQL Cluster Deployment
hosts: postgres_cluster
become: true
become_method: sudo
gather_facts: true
any_errors_fatal: true
handlers:
- ansible.builtin.import_tasks: roles/pgbouncer/handlers/main.yml
pre_tasks:
- name: Include main variables
ansible.builtin.include_vars: "roles/common/defaults/main.yml"
tags: always
- name: Include system variables
ansible.builtin.include_vars: "roles/common/defaults/system.yml"
tags: always
- name: Include OS-specific variables
ansible.builtin.include_vars: "roles/common/defaults/{{ ansible_os_family }}.yml"
tags: always
# if 'cloud_provider' is 'aws', 'gcp', or 'azure'
# set_fact: 'pgbackrest_install' to configure Postgres backups (TODO: Add the ability to configure backups in the UI)
- name: "Set variable: 'pgbackrest_install' to configure Postgres backups"
ansible.builtin.set_fact:
pgbackrest_install: true
when:
- not (pgbackrest_install | bool or wal_g_install | bool)
- cloud_provider | default('') | lower in ['aws', 'gcp', 'azure']
- pgbackrest_auto_conf | default(true) | bool # to be able to disable auto backup settings
tags: always
roles:
- role: wal_g
when: wal_g_install|bool
- role: pg_probackup
when: pg_probackup_install|bool
- role: cron
- role: tls_certificate/copy
when: tls_cert_generate|bool
- role: pgbouncer
when: pgbouncer_install|bool
- role: pgpass
- role: patroni
- role: pgbackrest/stanza-create
when: pgbackrest_install | bool
- role: vip_manager
when: not with_haproxy_load_balancing|bool and
(cluster_vip is defined and cluster_vip | length > 0)
# optional
- role: postgresql_users
when: inventory_hostname == groups['master'][0]
- role: postgresql_databases
when: inventory_hostname == groups['master'][0]
- role: postgresql_schemas
when: inventory_hostname == groups['master'][0]
- role: postgresql_privs
when: inventory_hostname == groups['master'][0]
- role: postgresql_extensions
when: inventory_hostname == groups['master'][0]
- role: pgbouncer/config
when: pgbouncer_install|bool
- role: netdata
# finish (info)
- role: deploy_finish
tasks:
# (optional) Command or script to be executed after the Postgres cluster deployment.
- block:
- name: Print post-deploy command
ansible.builtin.debug:
var: post_deploy_command
when: post_deploy_command_print | default(false) | bool
- name: Run post-deploy command
ansible.builtin.shell: "{{ post_deploy_command }} > {{ post_deploy_command_log | default('/var/tmp/post_deploy_command.log') }} 2>&1"
args:
executable: /bin/bash
register: post_deploy_result
delegate_to: "{{ item }}"
loop: "{{ post_deploy_command_hosts.split(',') | map('extract', groups) | list | flatten }}"
async: "{{ post_deploy_command_timeout }}" # run the command asynchronously
poll: 0
- name: Wait for the post-deploy command to complete
ansible.builtin.async_status:
jid: "{{ item.ansible_job_id }}"
register: post_deploy_job_result
delegate_to: "{{ item.item }}"
loop: "{{ post_deploy_result.results }}"
loop_control:
label: "{{ item.item }}"
until: post_deploy_job_result.finished
retries: "{{ (post_deploy_command_timeout | int) // 10 }}"
delay: 10
ignore_errors: true # allows to collect logs before stopping execution (in case of failure)
when:
- post_deploy_result.results is defined
- item.ansible_job_id is defined
- name: Get post-deploy command log
ansible.builtin.command: "cat {{ post_deploy_command_log | default('/var/tmp/post_deploy_command.log') }}"
register: post_deploy_log_content
delegate_to: "{{ item }}"
loop: "{{ post_deploy_command_hosts.split(',') | map('extract', groups) | list | flatten }}"
changed_when: false
when: post_deploy_command_print_result | default(false) | bool
- name: Print post-deploy command result
ansible.builtin.debug:
msg: "{{ item.stdout_lines }}"
loop: "{{ post_deploy_log_content.results }}"
loop_control:
label: "{{ item.item }}"
when:
- post_deploy_log_content.results is defined
- item.stdout_lines is defined
- name: Stop if post-deploy command failed
ansible.builtin.fail:
msg: "Post-deploy command failed. See log for details."
when: post_deploy_job_result.results | json_query('[?failed]') | length > 0
run_once: true # noqa run-once
when: post_deploy_command | default('') | length > 0
tags: post_deploy, post_deploy_command