-
-
Notifications
You must be signed in to change notification settings - Fork 434
/
Copy pathmain.yml
1093 lines (992 loc) · 43.2 KB
/
main.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
- name: Make sure handlers are flushed immediately
ansible.builtin.meta: flush_handlers
# pip install
- ansible.builtin.import_tasks: pip.yml
when:
- patroni_installation_method == "pip"
- pip_package not in system_packages
vars:
pip_package: "python{{ python_version | default('3') }}-pip"
tags: patroni, patroni_install, pip
# Patroni install
- block: # installation_method: "repo" and patroni_installation_method: "pip"
- name: Copy patroni requirements.txt file
ansible.builtin.copy:
src: requirements.txt
dest: /tmp/requirements.txt
when: patroni_pip_requirements_repo | length < 1
- name: Install setuptools
ansible.builtin.pip:
name: setuptools<66.0.0 # https://github.com/pypa/setuptools/issues/3772
state: latest
executable: pip3
extra_args: "--trusted-host=pypi.python.org --trusted-host=pypi.org --trusted-host=files.pythonhosted.org"
umask: "0022"
environment:
PATH: "{{ ansible_env.PATH }}:/usr/local/bin:/usr/bin"
PIP_BREAK_SYSTEM_PACKAGES: "1"
when: patroni_pip_requirements_repo | length < 1
- name: Install requirements
ansible.builtin.pip:
requirements: /tmp/requirements.txt
executable: pip3
extra_args: "--trusted-host=pypi.python.org --trusted-host=pypi.org --trusted-host=files.pythonhosted.org"
umask: "0022"
environment:
PATH: "{{ ansible_env.PATH }}:{{ postgresql_bin_dir }}:/usr/local/bin:/usr/bin"
PIP_BREAK_SYSTEM_PACKAGES: "1"
when: patroni_pip_requirements_repo | length < 1
- name: Install patroni
ansible.builtin.pip:
name: patroni
state: latest
executable: pip3
extra_args: "--trusted-host=pypi.python.org --trusted-host=pypi.org --trusted-host=files.pythonhosted.org"
umask: "0022"
environment:
PATH: "{{ ansible_env.PATH }}:/usr/local/bin:/usr/bin"
PIP_BREAK_SYSTEM_PACKAGES: "1"
when: patroni_pip_package_repo | length < 1 and patroni_install_version == "latest"
- name: "Install patroni {{ patroni_install_version }}"
ansible.builtin.pip:
name: "patroni=={{ patroni_install_version }}"
executable: pip3
extra_args: "--trusted-host=pypi.python.org --trusted-host=pypi.org --trusted-host=files.pythonhosted.org"
umask: "0022"
environment:
PATH: "{{ ansible_env.PATH }}:/usr/local/bin:/usr/bin"
PIP_BREAK_SYSTEM_PACKAGES: "1"
when: patroni_pip_package_repo | length < 1 and patroni_install_version != "latest"
when: installation_method == "repo" and patroni_installation_method == "pip"
environment: "{{ proxy_env | default({}) }}"
tags: patroni, patroni_install
- block: # when "patroni_pip_requirements_repo" and "patroni_pip_package_repo" is defined
- name: Download patroni requirements
ansible.builtin.get_url:
url: "{{ item }}"
dest: /tmp/
timeout: 120
validate_certs: false
loop: "{{ patroni_pip_requirements_repo }}"
when: patroni_pip_requirements_repo | length > 0
- name: Download patroni package
ansible.builtin.get_url:
url: "{{ item }}"
dest: /tmp/
timeout: 60
validate_certs: false
loop: "{{ patroni_pip_package_repo | list }}"
when: patroni_pip_package_repo | length > 0
- name: Install requirements
ansible.builtin.pip:
name: "file:///tmp/{{ item }}"
executable: pip3
extra_args: "--no-index --find-links=file:///tmp --ignore-installed"
umask: "0022"
loop: "{{ patroni_pip_requirements_repo | map('basename') | list }}"
environment:
PATH: "{{ ansible_env.PATH }}:/usr/local/bin:/usr/bin"
PIP_BREAK_SYSTEM_PACKAGES: "1"
when: patroni_pip_requirements_repo | length > 0
- name: Install patroni
ansible.builtin.pip:
name: "file:///tmp/{{ item }}"
executable: pip3
extra_args: "--no-index --find-links=file:///tmp --ignore-installed"
umask: "0022"
loop: "{{ patroni_pip_package_repo | map('basename') | list }}"
environment:
PATH: "{{ ansible_env.PATH }}:/usr/local/bin:/usr/bin"
PIP_BREAK_SYSTEM_PACKAGES: "1"
when: patroni_pip_package_repo | length > 0
when: installation_method == "repo" and patroni_installation_method == "pip"
tags: patroni, patroni_install
- block: # installation_method: "file" and patroni_installation_method: "pip"
- name: Copy patroni requirements
ansible.builtin.copy:
src: "{{ item }}"
dest: /tmp/
loop: "{{ patroni_pip_requirements_file }}"
when: patroni_pip_requirements_file | length > 0
- name: Copy patroni package
ansible.builtin.copy:
src: "{{ item }}"
dest: /tmp/
loop: "{{ patroni_pip_package_file }}"
when: patroni_pip_package_file | length > 0
- name: Install requirements
ansible.builtin.pip:
name: "file:///tmp/{{ item }}"
executable: pip3
extra_args: "--no-index --find-links=file:///tmp --ignore-installed"
umask: "0022"
loop: "{{ patroni_pip_requirements_file | map('basename') | list }}"
environment:
PATH: "{{ ansible_env.PATH }}:/usr/local/bin:/usr/bin"
PIP_BREAK_SYSTEM_PACKAGES: "1"
when: patroni_pip_requirements_file | length > 0
- name: Install patroni
ansible.builtin.pip:
name: "file:///tmp/{{ item }}"
executable: pip3
extra_args: "--no-index --find-links=file:///tmp --ignore-installed"
umask: "0022"
loop: "{{ patroni_pip_package_file | map('basename') | list }}"
environment:
PATH: "{{ ansible_env.PATH }}:/usr/local/bin:/usr/bin"
PIP_BREAK_SYSTEM_PACKAGES: "1"
when: patroni_pip_package_file | length > 0
when: installation_method == "file" and patroni_installation_method == "pip"
tags: patroni, patroni_install
- block: # installation_method: "repo" and patroni_installation_method: "rpm/deb"
# Debian
- name: Install patroni package
ansible.builtin.package:
name: "{{ patroni_packages | default('patroni') }}"
state: present
register: package_status
until: package_status is success
delay: 5
retries: 3
when: ansible_os_family == "Debian" and patroni_deb_package_repo | length < 1
# RedHat
- name: Install patroni package
ansible.builtin.package:
name: "{{ patroni_packages | default('patroni') }}"
state: present
register: package_status
until: package_status is success
delay: 5
retries: 3
when: ansible_os_family == "RedHat" and patroni_rpm_package_repo | length < 1
# when patroni_deb_package_repo or patroni_rpm_package_repo URL is defined
# Debian
- name: Download patroni deb package
ansible.builtin.get_url:
url: "{{ item }}"
dest: /tmp/
timeout: 60
validate_certs: false
loop: "{{ patroni_deb_package_repo | list }}"
when: ansible_os_family == "Debian" and patroni_deb_package_repo | length > 0
- name: Install patroni from deb package
ansible.builtin.apt:
force_apt_get: true
deb: "/tmp/{{ item }}"
state: present
loop: "{{ patroni_deb_package_repo | map('basename') | list }}"
register: apt_status
until: apt_status is success
delay: 5
retries: 3
when: ansible_os_family == "Debian" and patroni_deb_package_repo | length > 0
# RedHat
- name: Download patroni rpm package
ansible.builtin.get_url:
url: "{{ item }}"
dest: /tmp/
timeout: 60
validate_certs: false
loop: "{{ patroni_rpm_package_repo | list }}"
when: ansible_os_family == "RedHat" and patroni_rpm_package_repo | length > 0
- name: Install patroni from rpm package
ansible.builtin.package:
name: "/tmp/{{ item }}"
state: present
loop: "{{ patroni_rpm_package_repo | map('basename') | list }}"
register: package_status
until: package_status is success
delay: 5
retries: 3
when: ansible_os_family == "RedHat" and patroni_rpm_package_repo | length > 0
environment: "{{ proxy_env | default({}) }}"
when: installation_method == "repo" and (patroni_installation_method == "rpm" or patroni_installation_method == "deb")
tags: patroni, patroni_install
# installation_method: "file" and patroni_installation_method: "rpm/deb"
- block:
# Debian
- name: Copy patroni deb package into /tmp
ansible.builtin.copy:
src: "{{ patroni_deb_package_file }}"
dest: /tmp/
when: ansible_os_family == "Debian"
- name: Install patroni from deb package
ansible.builtin.apt:
force_apt_get: true
deb: "/tmp/{{ patroni_deb_package_file | basename }}"
state: present
register: apt_status
until: apt_status is success
delay: 5
retries: 3
when: ansible_os_family == "Debian"
# RedHat
- name: Copy patroni rpm package into /tmp
ansible.builtin.copy:
src: "{{ patroni_rpm_package_file }}"
dest: /tmp/
when: ansible_os_family == "RedHat"
- name: Install patroni from rpm package
ansible.builtin.package:
name: "/tmp/{{ patroni_rpm_package_file | basename }}"
state: present
register: package_status
until: package_status is success
delay: 5
retries: 3
when: ansible_os_family == "RedHat"
when: installation_method == "file" and (patroni_installation_method == "rpm" or patroni_installation_method == "deb")
tags: patroni, patroni_install
# Patroni configure
- name: Create conf directory
ansible.builtin.file:
path: /etc/patroni
state: directory
owner: postgres
group: postgres
mode: "0750"
tags: patroni, patroni_conf
- name: Generate conf file "/etc/patroni/patroni.yml"
ansible.builtin.template:
src: templates/patroni.yml.j2
dest: /etc/patroni/patroni.yml
owner: postgres
group: postgres
mode: "0640"
when: existing_pgcluster is not defined or not existing_pgcluster|bool
tags: patroni, patroni_conf
- name: Create patroni log directory
ansible.builtin.file:
path: "{{ patroni_log_dir }}"
owner: postgres
group: postgres
state: directory
mode: "0750"
when: patroni_log_destination == 'logfile'
tags: patroni, patroni_conf
- block: # for add_pgnode.yml
- name: Fetch patroni.yml conf file from master
run_once: true
ansible.builtin.fetch:
src: /etc/patroni/patroni.yml
dest: files/patroni.yml
validate_checksum: true
flat: true
delegate_to: "{{ groups.master[0] }}"
- name: Copy patroni.yml conf file to replica
ansible.builtin.copy:
src: files/patroni.yml
dest: /etc/patroni/patroni.yml
owner: postgres
group: postgres
mode: "0640"
- name: Remove patroni.yml conf files from localhost
become: false
run_once: true
ansible.builtin.file:
path: files/patroni.yml
state: absent
delegate_to: localhost
- name: Prepare patroni.yml conf file (replace "name","listen","connect_address")
ansible.builtin.lineinfile:
path: /etc/patroni/patroni.yml
regexp: "{{ patroni_config_without_cluster_vip_item.regexp }}"
line: "{{ patroni_config_without_cluster_vip_item.line }}"
backrefs: true
loop:
- regexp: '^name:'
line: 'name: {{ ansible_hostname }}'
- regexp: '^ listen: .*:{{ patroni_restapi_port }}$'
line: ' listen: {{ hostvars[inventory_hostname].inventory_hostname }}:{{ patroni_restapi_port }}'
- regexp: '^ connect_address: .*:{{ patroni_restapi_port }}$'
line: ' connect_address: {{ hostvars[inventory_hostname].inventory_hostname }}:{{ patroni_restapi_port }}'
- regexp: '^ listen: ((?!{{ patroni_restapi_port }}).)*$'
line: ' listen: {{ hostvars[inventory_hostname].inventory_hostname }},127.0.0.1:{{ postgresql_port }}'
- regexp: '^ connect_address: ((?!{{ patroni_restapi_port }}).)*$'
line: ' connect_address: {{ hostvars[inventory_hostname].inventory_hostname }}:{{ postgresql_port }}'
loop_control:
loop_var: patroni_config_without_cluster_vip_item
label: "{{ patroni_config_without_cluster_vip_item.line }}"
when: with_haproxy_load_balancing|bool or pgbouncer_install|bool or (cluster_vip is not defined or cluster_vip | length < 1)
- name: Prepare patroni.yml conf file (replace "name","listen","connect_address")
ansible.builtin.lineinfile:
path: /etc/patroni/patroni.yml
regexp: "{{ patroni_config_with_cluster_vip_item.regexp }}"
line: "{{ patroni_config_with_cluster_vip_item.line }}"
backrefs: true
loop:
- regexp: '^name:'
line: 'name: {{ ansible_hostname }}'
- regexp: '^ listen: .*:{{ patroni_restapi_port }}$'
line: ' listen: {{ hostvars[inventory_hostname].inventory_hostname }}:{{ patroni_restapi_port }}'
- regexp: '^ connect_address: .*:{{ patroni_restapi_port }}$'
line: ' connect_address: {{ hostvars[inventory_hostname].inventory_hostname }}:{{ patroni_restapi_port }}'
- regexp: '^ listen: ((?!{{ patroni_restapi_port }}).)*$'
line: ' listen: {{ hostvars[inventory_hostname].inventory_hostname }},{{ cluster_vip }},127.0.0.1:{{ postgresql_port }}'
- regexp: '^ connect_address: ((?!{{ patroni_restapi_port }}).)*$'
line: ' connect_address: {{ hostvars[inventory_hostname].inventory_hostname }}:{{ postgresql_port }}'
loop_control:
loop_var: patroni_config_with_cluster_vip_item
label: "{{ patroni_config_with_cluster_vip_item.line }}"
when: not with_haproxy_load_balancing|bool and not pgbouncer_install|bool and (cluster_vip is defined and cluster_vip | length > 0)
when: existing_pgcluster is defined and existing_pgcluster|bool
tags: patroni, patroni_conf
- name: Copy systemd service file "/etc/systemd/system/patroni.service"
ansible.builtin.template:
src: templates/patroni.service.j2
dest: /etc/systemd/system/patroni.service
owner: postgres
group: postgres
mode: "0644"
tags: patroni, patroni_conf, patroni_service
- name: Prepare PostgreSQL | create statistics directory (if not already exists)
ansible.builtin.file:
path: "{{ postgresql_stats_temp_directory_path }}"
state: directory
mode: "01777"
when:
- postgresql_stats_temp_directory_path is defined
- postgresql_stats_temp_directory_path != 'none'
- postgresql_version | int <= 14
tags: patroni, pgsql_stats_tmp
- name: Prepare PostgreSQL | mount the statistics directory in memory (tmpfs)
ansible.posix.mount:
path: "{{ postgresql_stats_temp_directory_path }}"
src: tmpfs
fstype: tmpfs
opts: "size={{ postgresql_stats_temp_directory_size }},uid=postgres,gid=postgres"
state: mounted
when:
- postgresql_stats_temp_directory_path is defined
- postgresql_stats_temp_directory_path != 'none'
- postgresql_version | int <= 14
tags: patroni, pgsql_stats_tmp
- name: Prepare PostgreSQL | make sure the postgresql log directory "{{ postgresql_log_dir }}" exists
ansible.builtin.file:
path: "{{ postgresql_log_dir }}"
owner: postgres
group: postgres
state: directory
mode: "0700"
tags: patroni
- name: Prepare PostgreSQL | make sure the custom WAL directory "{{ postgresql_wal_dir }}" exists
ansible.builtin.file:
path: "{{ postgresql_wal_dir }}"
owner: postgres
group: postgres
state: directory
mode: "0700"
when: postgresql_wal_dir is defined and postgresql_wal_dir | length > 0
tags: patroni, custom_wal_dir
- block: # when postgresql NOT exists or PITR
- name: Prepare PostgreSQL | make sure PostgreSQL data directory "{{ postgresql_data_dir }}" exists
ansible.builtin.file:
path: "{{ postgresql_data_dir }}"
owner: postgres
group: postgres
state: directory
mode: "0700"
# for Debian based distros only
# patroni bootstrap failure is possible if the PostgreSQL config files are missing
- name: Prepare PostgreSQL | make sure PostgreSQL config directory exists
ansible.builtin.file:
path: /etc/postgresql
state: directory
owner: postgres
group: postgres
recurse: true
when: ansible_os_family == "Debian" and
postgresql_packages|join(" ") is not search("postgrespro")
- name: Prepare PostgreSQL | make sure PostgreSQL config files exists
ansible.builtin.stat:
path: "{{ postgresql_conf_dir }}/postgresql.conf"
register: postgresql_conf_file
when: ansible_os_family == "Debian" and
postgresql_packages|join(" ") is not search("postgrespro")
- name: Prepare PostgreSQL | generate default PostgreSQL config files
become: true
become_user: postgres
ansible.builtin.command: >
/usr/bin/pg_createcluster {{ postgresql_version }} {{ postgresql_cluster_name }}
-d {{ postgresql_data_dir }}
-p {{ postgresql_port }}
-e {{ postgresql_encoding }}
--locale {{ postgresql_locale }}
register: pg_createcluster_result
failed_when: pg_createcluster_result.rc != 0
when: (ansible_os_family == "Debian" and
postgresql_packages|join(" ") is not search("postgrespro")) and
not postgresql_conf_file.stat.exists
# When performing PITR, we do not clear the directory if pgbackrest is used
# to be able to use the '--delta restore' option.
- name: Prepare PostgreSQL | make sure the data directory "{{ postgresql_data_dir }}" is empty
ansible.builtin.file:
path: "{{ postgresql_data_dir }}"
state: "{{ item }}"
owner: postgres
group: postgres
mode: "0700"
loop:
- absent
- directory
when: (is_master | bool and patroni_cluster_bootstrap_method != "pgbackrest")
or (not is_master | bool and ('pgbackrest' not in patroni_create_replica_methods or new_node | default(false) | bool))
- name: Prepare PostgreSQL | make sure the custom WAL directory "{{ postgresql_wal_dir }}" is empty
ansible.builtin.file:
path: "{{ postgresql_wal_dir }}"
state: "{{ item }}"
owner: postgres
group: postgres
mode: "0700"
loop:
- absent
- directory
when: (postgresql_wal_dir is defined and postgresql_wal_dir | length > 0) and
((is_master | bool and patroni_cluster_bootstrap_method != "pgbackrest")
or (not is_master | bool and ('pgbackrest' not in patroni_create_replica_methods or new_node | default(false) | bool)))
when: not postgresql_exists | bool or patroni_cluster_bootstrap_method != "initdb"
tags: patroni, point_in_time_recovery
- block: # when postgresql exists
- block: # for master only
- name: Prepare PostgreSQL | check PostgreSQL is started on Master
become: true
become_user: postgres
ansible.builtin.command: "{{ postgresql_bin_dir }}/pg_ctl status -D {{ postgresql_data_dir }}"
register: pg_ctl_status_result
changed_when: false
failed_when:
- pg_ctl_status_result.rc != 0
- pg_ctl_status_result.rc != 3
- name: Prepare PostgreSQL | start PostgreSQL on Master
become: true
become_user: postgres
ansible.builtin.command: "{{ postgresql_bin_dir }}/pg_ctl start -D {{ postgresql_data_dir }} -w -t {{ pg_ctl_timeout | default(3600) }}"
when: pg_ctl_status_result.rc == 3
- name: Prepare PostgreSQL | check PostgreSQL is accepting connections
become: true
become_user: postgres
ansible.builtin.command: "{{ postgresql_bin_dir }}/pg_isready -p {{ postgresql_port }}"
register: pg_isready_result
until: pg_isready_result.rc == 0
retries: 30
delay: 10
changed_when: false
- name: Prepare PostgreSQL | generate pg_hba.conf on Master
ansible.builtin.template:
src: templates/pg_hba.conf.j2
dest: "{{ postgresql_conf_dir }}/pg_hba.conf"
owner: postgres
group: postgres
mode: "0640"
- name: Prepare PostgreSQL | reload for apply the pg_hba.conf
become: true
become_user: postgres
ansible.builtin.command: "{{ postgresql_bin_dir }}/pg_ctl reload -D {{ postgresql_data_dir }}"
- name: Prepare PostgreSQL | make sure the user "{{ patroni_superuser_username }}" are present, and password does not differ from the specified
community.postgresql.postgresql_user:
db: postgres
name: "{{ patroni_superuser_username }}"
password: "{{ patroni_superuser_password }}"
encrypted: true
role_attr_flags: "SUPERUSER"
login_unix_socket: "{{ postgresql_unix_socket_dir }}"
port: "{{ postgresql_port }}"
state: present
become: true
become_user: postgres
- name: Prepare PostgreSQL | make sure the user "{{ patroni_replication_username }}" are present, and password does not differ from the specified
community.postgresql.postgresql_user:
db: postgres
name: "{{ patroni_replication_username }}"
password: "{{ patroni_replication_password }}"
encrypted: true
role_attr_flags: "LOGIN,REPLICATION"
login_unix_socket: "{{ postgresql_unix_socket_dir }}"
port: "{{ postgresql_port }}"
state: present
become: true
become_user: postgres
when: is_master | bool
- name: Prepare PostgreSQL | check PostgreSQL is started
become: true
become_user: postgres
ansible.builtin.command: "{{ postgresql_bin_dir }}/pg_ctl status -D {{ postgresql_data_dir }}"
register: pg_ctl_status_result
changed_when: false
failed_when:
- pg_ctl_status_result.rc != 0
- pg_ctl_status_result.rc != 3
- name: Prepare PostgreSQL | waiting for CHECKPOINT to complete before stopping postgresql
become: true
become_user: postgres
ansible.builtin.command: >
{{ postgresql_bin_dir }}/psql -p {{ postgresql_port }} -U {{ patroni_superuser_username }} -d postgres -tAXc "CHECKPOINT"
register: checkpoint_result
until: checkpoint_result.rc == 0
retries: 300
delay: 10
when: pg_ctl_status_result.rc == 0
- name: Prepare PostgreSQL | stop PostgreSQL (will be managed by patroni)
become: true
become_user: postgres
ansible.builtin.command: "{{ postgresql_bin_dir }}/pg_ctl stop -D {{ postgresql_data_dir }} -m fast -w -t {{ pg_ctl_timeout | default(3600) }}"
when: checkpoint_result.rc is defined and checkpoint_result.rc == 0
- name: Prepare PostgreSQL | check PostgreSQL is stopped
become: true
become_user: postgres
ansible.builtin.command: "{{ postgresql_bin_dir }}/pg_ctl status -D {{ postgresql_data_dir }}"
register: pg_ctl_stop_result
failed_when: pg_ctl_stop_result.rc != 3
changed_when: false
when: postgresql_exists | bool and patroni_cluster_bootstrap_method == "initdb"
tags: patroni, patroni_start_master
- block: # PITR (custom bootstrap)
# Prepare (install pexpect, ruamel.yaml)
- name: Prepare | Make sure the ansible required python library is exist
ansible.builtin.pip:
name: "{{ item }}"
state: present
executable: pip3
extra_args: "--trusted-host=pypi.python.org --trusted-host=pypi.org --trusted-host=files.pythonhosted.org"
umask: "0022"
loop:
- pexpect==4.9.0
- ruamel.yaml==0.17.40
environment:
PATH: "{{ ansible_env.PATH }}:/usr/local/bin:/usr/bin"
PIP_BREAK_SYSTEM_PACKAGES: "1"
# Run PITR
- name: Stop patroni service on the Replica servers (if running)
ansible.builtin.systemd:
name: patroni
state: stopped
when: not is_master | bool
- name: Stop patroni service on the Master server (if running)
ansible.builtin.systemd:
name: patroni
state: stopped
when: is_master | bool
- name: Check that PostgreSQL is stopped
become: true
become_user: postgres
ansible.builtin.command: "{{ postgresql_bin_dir }}/pg_ctl status -D {{ postgresql_data_dir }}"
register: pg_ctl_status_result
changed_when: false
failed_when: false
- name: Stop PostgreSQL
become: true
become_user: postgres
ansible.builtin.command: >-
{{ postgresql_bin_dir }}/pg_ctl stop -D {{ postgresql_data_dir }} -m fast -w -t {{ pg_ctl_timeout | default(3600) }}
when: pg_ctl_status_result.rc is defined and (pg_ctl_status_result.rc != 3 and pg_ctl_status_result.rc != 4)
- name: Remove patroni cluster "{{ patroni_cluster_name }}" from DCS (if exist)
become: true
become_user: postgres
ansible.builtin.expect:
command: "patronictl -c /etc/patroni/patroni.yml remove {{ patroni_cluster_name }}"
responses:
'Please confirm the cluster name to remove': '{{ patroni_cluster_name }}'
'You are about to remove all information in DCS': 'Yes I am aware'
register: patronictl_remove_result
changed_when:
patronictl_remove_result.rc == 0 and
patronictl_remove_result.stdout|lower is not search("key not found")
failed_when:
patronictl_remove_result.rc != 0
environment:
PATH: "{{ ansible_env.PATH }}:/usr/bin:/usr/local/bin"
when: is_master | bool
- block: # for pgbackrest only (for use --delta restore)
- name: Run "{{ pgbackrest_patroni_cluster_restore_command }}" on Master
ansible.builtin.command: >
{{ pgbackrest_patroni_cluster_restore_command }}
{{ '--target-action=promote' if pgbackrest_patroni_cluster_restore_command is search('--type=') else '' }}
async: "{{ cluster_restore_timeout | default(86400) }}" # timeout 24 hours
poll: 0
register: pgbackrest_restore_master
when: is_master | bool
# if patroni_create_replica_methods: "pgbackrest"
- name: Run "{{ pgbackrest_patroni_cluster_restore_command }}" on Replica
ansible.builtin.command: >
{{ pgbackrest_patroni_cluster_restore_command }}
{{ '--target-action=pause' if pgbackrest_patroni_cluster_restore_command is search('--type=') else '' }}
async: "{{ cluster_restore_timeout | default(86400) }}" # timeout 24 hours
poll: 0
register: pgbackrest_restore_replica
when: not is_master | bool and 'pgbackrest' in patroni_create_replica_methods
- name: Waiting for restore from backup
ansible.builtin.async_status:
jid: "{{ item.ansible_job_id }}"
loop:
- "{{ pgbackrest_restore_master }}"
- "{{ pgbackrest_restore_replica }}"
loop_control:
label: "{{ item.changed }}"
register: pgbackrest_restore_jobs_result
until: pgbackrest_restore_jobs_result.finished
retries: "{{ (cluster_restore_timeout | default(86400)) | int // 30 }}" # timeout 24 hours
delay: 30
when: item.ansible_job_id is defined
- name: Remove patroni.dynamic.json file
ansible.builtin.file:
path: "{{ postgresql_data_dir }}/patroni.dynamic.json"
state: absent
failed_when: false
when: not keep_patroni_dynamic_json|bool
- name: Start PostgreSQL for Recovery
ansible.builtin.command: >-
{{ postgresql_bin_dir }}/pg_ctl start -D {{ postgresql_data_dir }} -w -t {{ pg_ctl_timeout | default(3600) }}
-o '--config-file={{ postgresql_conf_dir }}/postgresql.conf'
-o '-c hot_standby=off'
{% if postgresql_version | int >= 12 %}
-o '-c restore_command="pgbackrest --stanza={{ pgbackrest_stanza }} archive-get %f %p"'
{% endif %}
-o '-c archive_command=/bin/true'
-l /tmp/pg_recovery_{{ ansible_date_time.date }}.log
async: "{{ pg_ctl_timeout | default(3600) }}" # run the command asynchronously
poll: 0
register: pg_ctl_start_result
when: is_master | bool or (not is_master | bool and 'pgbackrest' in patroni_create_replica_methods)
- name: Wait for the PostgreSQL start command to complete
ansible.builtin.async_status:
jid: "{{ pg_ctl_start_result.ansible_job_id }}"
register: pg_ctl_start_job_result
until: pg_ctl_start_job_result.finished
retries: "{{ (pg_ctl_timeout | default(3600) | int) // 10 }}"
delay: 10
when: is_master | bool or (not is_master | bool and 'pgbackrest' in patroni_create_replica_methods)
- name: Wait for PostgreSQL recovery to complete (WAL apply)
ansible.builtin.command: >-
{{ postgresql_bin_dir }}/psql -p {{ postgresql_port }} -U {{ patroni_superuser_username }} -d postgres
-tAXc "select pg_is_in_recovery()"
register: pg_is_in_recovery
until: pg_is_in_recovery.stdout == "f"
retries: "{{ (cluster_restore_timeout | default(86400)) | int // 30 }}" # timeout 24 hours
delay: 30
changed_when: false
failed_when: false
when: is_master | bool
- name: Check PostgreSQL recovery log
ansible.builtin.command: "grep -A2 'recovery stopping' /tmp/pg_recovery_{{ ansible_date_time.date }}.log"
register: pg_recovery_result
changed_when: false
failed_when: false
when: is_master | bool
- name: PostgreSQL recovery details
ansible.builtin.debug:
msg: '{{ pg_recovery_result.stdout_lines }}'
when: pg_recovery_result.stdout_lines is defined
- name: Check that PostgreSQL is stopped
ansible.builtin.command: "{{ postgresql_bin_dir }}/pg_ctl status -D {{ postgresql_data_dir }}"
register: pg_ctl_status_result
changed_when: false
failed_when: false
- name: Stop PostgreSQL
ansible.builtin.command: >-
{{ postgresql_bin_dir }}/pg_ctl stop -D {{ postgresql_data_dir }} -m fast -w -t {{ pg_ctl_timeout | default(3600) }}
when: pg_ctl_status_result.rc is defined and (pg_ctl_status_result.rc != 3 and pg_ctl_status_result.rc != 4)
when: patroni_cluster_bootstrap_method == "pgbackrest"
become: true
become_user: postgres
environment: "{{ proxy_env | default({}) }}"
when: patroni_cluster_bootstrap_method != "initdb" and
(pgbackrest_install|bool or wal_g_install|bool)
tags: patroni, point_in_time_recovery
- block: # PITR (custom bootstrap) - disable archive_command
- name: Check if patroni.dynamic.json exists
ansible.builtin.stat:
path: "{{ postgresql_data_dir }}/patroni.dynamic.json"
register: patroni_dynamic_json
when: not keep_patroni_dynamic_json | bool
- name: Remove patroni.dynamic.json file
ansible.builtin.file:
path: "{{ postgresql_data_dir }}/patroni.dynamic.json"
state: absent
when:
- patroni_dynamic_json is defined
- patroni_dynamic_json.stat is defined
- patroni_dynamic_json.stat.exists
- name: Edit patroni.dynamic.json | disable archive_command (if enabled)
yedit:
src: "{{ postgresql_data_dir }}/patroni.dynamic.json"
key: postgresql.parameters.archive_command
value: "cd ." # not doing anything yet with WAL-s
content_type: json
when: disable_archive_command | bool
- name: Edit patroni.yml | disable archive_command (if enabled)
yedit:
src: /etc/patroni/patroni.yml
key: bootstrap.dcs.postgresql.parameters.archive_command
value: "cd ." # not doing anything yet with WAL-s
when: disable_archive_command | bool
when: patroni_cluster_bootstrap_method != "initdb" and
(pgbackrest_install | bool or wal_g_install | bool) and
(existing_pgcluster is not defined or not existing_pgcluster | bool)
become: true
become_user: postgres
tags: patroni, point_in_time_recovery
- block: # PITR (custom bootstrap) - generate pg_hba.conf
- name: Generate pg_hba.conf (before start patroni)
ansible.builtin.template:
src: templates/pg_hba.conf.j2
dest: "{{ postgresql_conf_dir }}/pg_hba.conf"
owner: postgres
group: postgres
mode: "0640"
when: patroni_cluster_bootstrap_method != "initdb" and
postgresql_conf_dir != postgresql_data_dir and
(existing_pgcluster is not defined or not existing_pgcluster|bool)
tags: patroni
- block: # start patroni on master
- name: Start patroni service on the Master server
ansible.builtin.systemd:
daemon_reload: true
name: patroni
state: restarted
enabled: true
- name: "Wait for port {{ patroni_restapi_port }} to become open on the host"
ansible.builtin.wait_for:
port: "{{ patroni_restapi_port }}"
host: "{{ inventory_hostname }}"
state: started
timeout: 120
delay: 10
ignore_errors: false
# for WAL-G (custom bootstrap)
- name: Wait for PostgreSQL Recovery to complete (WAL apply)
become: true
become_user: postgres
ansible.builtin.command: >-
{{ postgresql_bin_dir }}/psql -p {{ postgresql_port }} -U {{ patroni_superuser_username }} -d postgres -tAXc
"select pg_is_in_recovery()"
register: pg_is_in_recovery
until: pg_is_in_recovery.stdout == "f"
retries: "{{ (cluster_restore_timeout | default(86400)) | int // 30 }}" # timeout 24 hours
delay: 30
changed_when: false
failed_when: false
when: patroni_cluster_bootstrap_method == "wal-g"
- name: Wait for the Standby cluster initialization to complete
ansible.builtin.uri:
url: "http://{{ inventory_hostname }}:{{ patroni_restapi_port }}/standby-leader"
status_code: 200
register: standby_leader_result
until: standby_leader_result.status == 200
retries: "{{ (cluster_restore_timeout | default(86400)) | int // 30 }}" # timeout 24 hours
delay: 30
environment:
no_proxy: "{{ inventory_hostname }}"
when:
- (patroni_standby_cluster.host is defined and patroni_standby_cluster.host | length > 0)
- not ansible_check_mode
- name: Check PostgreSQL is started and accepting connections
become: true
become_user: postgres
ansible.builtin.command: "{{ postgresql_bin_dir }}/pg_isready -p {{ postgresql_port }}"
register: pg_isready_result
until: pg_isready_result.rc == 0
retries: 1000
delay: 30
changed_when: false
- name: Wait for the cluster to initialize (master is the leader with the lock)
ansible.builtin.uri:
url: "http://{{ inventory_hostname }}:{{ patroni_restapi_port }}/leader"
status_code: 200
register: result
until: result.status == 200
retries: 10
delay: 2
environment:
no_proxy: "{{ inventory_hostname }}"
when:
- (patroni_standby_cluster.host is not defined or patroni_standby_cluster.host | length < 1)
- not ansible_check_mode
when: is_master | bool
tags: patroni, patroni_start_master, point_in_time_recovery
- block: # pg_hba (using a templates/pg_hba.conf.j2)
- name: Prepare PostgreSQL | generate pg_hba.conf
ansible.builtin.template:
src: templates/pg_hba.conf.j2
dest: "{{ postgresql_conf_dir }}/pg_hba.conf"
owner: postgres
group: postgres
mode: "0640"
register: generate_pg_hba
when: is_master | bool or
((not is_master | bool and postgresql_conf_dir != postgresql_data_dir)
or postgresql_exists | bool)
- name: Prepare PostgreSQL | reload for apply the pg_hba.conf
become: true
become_user: postgres
ansible.builtin.command: "{{ postgresql_bin_dir }}/pg_ctl reload -D {{ postgresql_data_dir }}"
register: pg_ctl_reload_result
changed_when: pg_ctl_reload_result.rc == 0
failed_when: false # exec 'reload' on all running postgres (to re-run with --tag pg_hba).
when: generate_pg_hba is changed
when: existing_pgcluster is not defined or not existing_pgcluster|bool
tags: patroni, pg_hba, pg_hba_generate
- block: # PITR (custom bootstrap) - password reset for PostgreSQL users
- name: Make sure the Master is not in recovery mode
ansible.builtin.command: >-
{{ postgresql_bin_dir }}/psql -p {{ postgresql_port }} -U {{ patroni_superuser_username }} -d postgres -tAXc
"select pg_is_in_recovery()"
register: pg_is_in_recovery
until: pg_is_in_recovery.stdout == "f"
retries: "{{ (cluster_restore_timeout | default(86400)) | int // 30 }}" # timeout 24 hours
delay: 30
changed_when: false
when: is_master | bool
- name: Make sure the superuser and replication users are present, and password does not differ from the specified
community.postgresql.postgresql_user:
db: postgres
name: "{{ item.name }}"
password: "{{ item.password }}"
role_attr_flags: "{{ item.flags }}"
login_unix_socket: "{{ postgresql_unix_socket_dir }}"
port: "{{ postgresql_port }}"
register: postgresql_user_result
loop:
- { name: '{{ patroni_superuser_username }}', password: '{{ patroni_superuser_password }}', flags: 'SUPERUSER' }
- { name: '{{ patroni_replication_username }}', password: '{{ patroni_replication_password }}', flags: 'LOGIN,REPLICATION' }
loop_control:
label: "{{ item.name }}"
when:
- is_master | bool
- (patroni_superuser_username and patroni_superuser_password) is defined
- (patroni_superuser_username and patroni_superuser_password) | length > 0
- (patroni_replication_username and patroni_replication_password) is defined
- (patroni_replication_username and patroni_replication_password) | length > 0
- name: Make sure the postgresql users are present, and password does not differ from the specified
community.postgresql.postgresql_user:
db: postgres
name: "{{ item.name }}"
password: "{{ item.password }}"
role_attr_flags: "{{ item.flags }}"
login_unix_socket: "{{ postgresql_unix_socket_dir }}"
port: "{{ postgresql_port }}"
loop: "{{ postgresql_users }}"
loop_control:
label: "{{ item.name }}"
ignore_errors: true
when:
- is_master | bool
- (postgresql_users is defined and postgresql_users | length > 0)
- name: Update postgresql authentication in patroni.yml
yedit:
src: /etc/patroni/patroni.yml
edits:
- key: postgresql.authentication.replication.username
value: "{{ patroni_replication_username }}"
- key: postgresql.authentication.replication.password
value: "{{ patroni_replication_password }}"
- key: postgresql.authentication.superuser.username
value: "{{ patroni_superuser_username }}"
- key: postgresql.authentication.superuser.password
value: "{{ patroni_superuser_password }}"
state: present
when: hostvars[groups['master'][0]]['postgresql_user_result'] is changed
when: patroni_cluster_bootstrap_method != "initdb" and
(pgbackrest_install|bool or wal_g_install|bool) and
(existing_pgcluster is not defined or not existing_pgcluster|bool)
become: true
become_user: postgres
tags: patroni, point_in_time_recovery
- block: # for add_pgnode.yml
- name: Prepare PostgreSQL | fetch pg_hba.conf file from master
run_once: true
ansible.builtin.fetch:
src: "{{ postgresql_conf_dir }}/pg_hba.conf"
dest: files/pg_hba.conf
validate_checksum: true
flat: true
delegate_to: "{{ groups.master[0] }}"
- name: Prepare PostgreSQL | copy pg_hba.conf file to replica
ansible.builtin.copy:
src: files/pg_hba.conf
dest: "{{ postgresql_conf_dir }}/pg_hba.conf"
owner: postgres
group: postgres