-
-
Notifications
You must be signed in to change notification settings - Fork 357
/
main.yml
241 lines (220 loc) · 8.62 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
---
- name: Make sure handlers are flushed immediately
ansible.builtin.meta: flush_handlers
# users info
- block:
- name: Get postgresql users list
run_once: true
become: true
become_user: postgres
ansible.builtin.command:
"{{ postgresql_bin_dir }}/psql -p {{ postgresql_port }} -U {{ patroni_superuser_username }} -d postgres -Xc\"\\du\""
register: users_result
delegate_to: "{{ groups.master[0] }}"
changed_when: false
- name: PostgreSQL list of users
run_once: true
ansible.builtin.debug:
var: users_result.stdout_lines
ignore_errors: true
tags: users, users_list, cluster_info, cluster_status, point_in_time_recovery
# databases info
- block:
- name: Get postgresql database list
run_once: true
become: true
become_user: postgres
ansible.builtin.command:
"{{ postgresql_bin_dir }}/psql -p {{ postgresql_port }} -U {{ patroni_superuser_username }} -d postgres -Xc
\"
SELECT
d.datname as name,
pg_get_userbyid(d.datdba) as owner,
pg_encoding_to_char(d.encoding) as encoding,
d.datcollate as collate,
d.datctype as ctype,
CASE
WHEN has_database_privilege(d.datname, 'CONNECT')
THEN pg_size_pretty(pg_database_size(d.datname))
ELSE 'No Access'
END
size,
t.spcname as tablespace
FROM pg_catalog.pg_database d
JOIN pg_catalog.pg_tablespace t
ON d.dattablespace = t.oid
WHERE NOT datistemplate
ORDER BY 1
\""
register: dbs_result
delegate_to: "{{ groups.master[0] }}"
changed_when: false
- name: PostgreSQL list of databases
run_once: true
ansible.builtin.debug:
var: dbs_result.stdout_lines
ignore_errors: true
tags: databases, db_list, cluster_info, cluster_status, point_in_time_recovery
# cluster info
- block:
- name: Check postgresql cluster health
run_once: true
become: true
become_user: postgres
ansible.builtin.command: patronictl -c /etc/patroni/patroni.yml list
register: patronictl_result
environment:
PATH: "{{ ansible_env.PATH }}:/usr/bin:/usr/local/bin"
changed_when: false
- name: PostgreSQL Cluster health
run_once: true
ansible.builtin.debug:
var: patronictl_result.stdout_lines
ignore_errors: true
tags: patroni_status, cluster_info, cluster_status, point_in_time_recovery
# connection info
- block: # if cluster_vip is defined
- name: PostgreSQL Cluster connection info
run_once: true
ansible.builtin.debug:
msg:
- +------------------------------------------------+
- address (VIP) {{ cluster_vip }}
- port {{ haproxy_listen_port.master }} (read/write) master
- port {{ haproxy_listen_port.replicas }} (read only) all replicas
- port {{ haproxy_listen_port.replicas_sync }} (read only) synchronous replica only
- port {{ haproxy_listen_port.replicas_async }} (read only) asynchronous replicas only
- +------------------------------------------------+
when:
- with_haproxy_load_balancing | bool
- synchronous_mode | bool
- name: PostgreSQL Cluster connection info
run_once: true
ansible.builtin.debug:
msg:
- +------------------------------------------------+
- address (VIP) {{ cluster_vip }}
- port {{ haproxy_listen_port.master }} (read/write) master
- port {{ haproxy_listen_port.replicas }} (read only) all replicas
- +------------------------------------------------+
when:
- with_haproxy_load_balancing | bool
- not synchronous_mode | bool
- name: PostgreSQL Cluster connection info
run_once: true
ansible.builtin.debug:
msg:
- +------------------------------------------------+
- address (VIP) {{ cluster_vip }}
- port {% if pgbouncer_install %}{{ pgbouncer_listen_port }} (pgbouncer){% else %}{{ postgresql_port }}{% endif %}
- +------------------------------------------------+
when:
- not with_haproxy_load_balancing | bool
when:
- (cluster_vip is defined and cluster_vip | length > 0)
- dcs_type == "etcd"
ignore_errors: true
tags: conn_info, cluster_info, cluster_status
- block:
- name: Get vip info
ansible.builtin.set_fact:
man_ip: "{{ item }}"
loop: "{{ ansible_all_ipv4_addresses }}"
when: item == cluster_vip
- name: Virtual IP Address (VIP) info
ansible.builtin.debug:
msg:
"Cluster ip address (VIP) {{ cluster_vip }}
is running on server {{ ansible_hostname }}"
when: man_ip is defined and man_ip == cluster_vip
when:
- (cluster_vip is defined and cluster_vip | length > 0)
- dcs_type == "etcd"
ignore_errors: true
tags: vip_owner, vip_status, cluster_info, cluster_status
- block: # if cluster_vip is not defined
- name: Create list of nodes
run_once: true
ansible.builtin.set_fact:
haproxy_nodes: >-
{{
groups['balancers']
| default([])
| map('extract', hostvars, 'inventory_hostname')
| join(',')
}}
postgres_cluster_nodes: >-
{{
groups['postgres_cluster']
| default([])
| map('extract', hostvars, 'inventory_hostname')
| join(',')
}}
- name: PostgreSQL Cluster connection info
run_once: true
ansible.builtin.debug:
msg:
- +------------------------------------------------+
- address {{ haproxy_nodes }}
- port {{ haproxy_listen_port.master }} (read/write) master
- port {{ haproxy_listen_port.replicas }} (read only) all replicas
- port {{ haproxy_listen_port.replicas_sync }} (read only) synchronous replica only
- port {{ haproxy_listen_port.replicas_async }} (read only) asynchronous replicas only
- +------------------------------------------------+
when:
- with_haproxy_load_balancing | bool
- synchronous_mode | bool
- name: PostgreSQL Cluster connection info
run_once: true
ansible.builtin.debug:
msg:
- +------------------------------------------------+
- address {{ haproxy_nodes }}
- port {{ haproxy_listen_port.master }} (read/write) master
- port {{ haproxy_listen_port.replicas }} (read only) all replicas
- +------------------------------------------------+
when:
- with_haproxy_load_balancing | bool
- not synchronous_mode | bool
- name: PostgreSQL Cluster connection info
run_once: true
ansible.builtin.debug:
msg:
- +------------------------------------------------+
- address {{ postgres_cluster_nodes }}
- port {% if pgbouncer_install %}{{ pgbouncer_listen_port }} (pgbouncer){% else %}{{ postgresql_port }}{% endif %}
- +------------------------------------------------+
when:
- not with_haproxy_load_balancing | bool
ignore_errors: true
when:
- (cluster_vip is not defined or cluster_vip | length < 1)
- dcs_type == "etcd"
tags: conn_info, cluster_info, cluster_status
- block: # if dcs_type: "consul"
- name: PostgreSQL Cluster connection info
run_once: true
ansible.builtin.debug:
msg:
- +------------------------------------------------+
- "Client access point (DNS):"
- " master.{{ patroni_cluster_name }}.service.consul "
- " replica.{{ patroni_cluster_name }}.service.consul "
- port {% if pgbouncer_install %}{{ pgbouncer_listen_port }} (pgbouncer){% else %}{{ postgresql_port }}{% endif %}
- +------------------------------------------------+
when: not synchronous_mode | bool
- name: PostgreSQL Cluster connection info
run_once: true
ansible.builtin.debug:
msg:
- +------------------------------------------------+
- "Client access point (DNS):"
- " master.{{ patroni_cluster_name }}.service.consul "
- " replica.{{ patroni_cluster_name }}.service.consul "
- " sync-replica.{{ patroni_cluster_name }}.service.consul "
- " async-replica.{{ patroni_cluster_name }}.service.consul "
- port {% if pgbouncer_install %}{{ pgbouncer_listen_port }} (pgbouncer){% else %}{{ postgresql_port }}{% endif %}
- +------------------------------------------------+
when: synchronous_mode | bool
when: dcs_type == "consul"
...