-
Notifications
You must be signed in to change notification settings - Fork 571
/
configure.yml
244 lines (218 loc) · 9.56 KB
/
configure.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
# file: postgresql/tasks/configure.yml
- name: PostgreSQL | Drop the data directory | RedHat
file:
path: "{{ postgresql_data_directory }}"
state: absent
register: pgdata_dir_remove
when: ansible_os_family == "RedHat" and postgresql_cluster_reset
- name: PostgreSQL | Make sure the postgres data directory exists
file:
path: "{{postgresql_data_directory}}"
owner: "{{ postgresql_service_user }}"
group: "{{ postgresql_service_group }}"
state: directory
mode: 0700
register: pgdata_dir_exist
- name: PostgreSQL | Make sure postgres tablespaces directories exist
file:
path: "{{ item }}"
owner: "{{ postgresql_service_user }}"
group: "{{ postgresql_service_group }}"
state: directory
mode: 0700
with_items: "{{ postgresql_tablespaces_dirs }}"
- name: PostgreSQL | Make sure the postgres WAL directory exists
file:
path: "{{ postgresql_wal_directory }}"
owner: "{{ postgresql_service_user }}"
group: "{{ postgresql_service_group }}"
state: directory
mode: 0700
register: pgwal_dir_exist
when: postgresql_wal_directory != ""
- name: PostgreSQL | Make sure the postgres log directory exists
file:
path: "{{ postgresql_log_directory }}"
owner: "{{ postgresql_service_user }}"
group: "{{ postgresql_service_group }}"
state: directory
mode: 0700
register: pglog_dir_exist
when: postgresql_log_directory != "pg_log" or postgresql_log_directory != "log"
- name: PostgreSQL | Ensure the locale for lc_collate and lc_ctype is generated | Debian
become: yes
locale_gen: name="{{ item }}" state=present
with_items:
- "{{ postgresql_locale }}"
- "{{ postgresql_ctype }}"
when: ansible_os_family == "Debian" and item != "C.UTF-8"
- name: PostgreSQL | Ensure the locale is generated | RedHat
become: yes
command: localedef -c -i {{ item.parts[0] }} -f {{ item.parts[1] }} {{ item.locale_name }}
changed_when: false
with_items:
- { parts: "{{ postgresql_locale_parts }}", locale_name: "{{ postgresql_locale }}" }
- { parts: "{{ postgresql_ctype_parts }}", locale_name: "{{ postgresql_ctype }}" }
when: ansible_os_family == "RedHat"
ignore_errors: yes
- name: PostgreSQL | Stop PostgreSQL | Debian
service:
name: "{{ postgresql_service_name }}"
state: stopped
become: yes
when: ansible_os_family == "Debian" and postgresql_cluster_reset and pgdata_dir_exist.changed
- name: PostgreSQL | Reset the cluster - drop the existing one | Debian
shell: pg_dropcluster {{ postgresql_version }} {{ postgresql_cluster_name }}
become: yes
become_user: "{{ postgresql_service_user }}"
when: ansible_os_family == "Debian" and postgresql_cluster_reset and pgdata_dir_exist.changed
- name: PostgreSQL | Reset the cluster - create a new one (with specified encoding and locale) | Debian
shell: >
pg_createcluster --locale {{ postgresql_locale }}
-e {{ postgresql_encoding }} -d {{ postgresql_data_directory }}
{{ postgresql_version }} {{ postgresql_cluster_name }}
--
{% if postgresql_data_checksums and postgresql_version is version_compare('9.3', '>=') %}--data-checksums{% endif %}
{% if postgresql_pwfile != "" %}--pwfile={{ postgresql_pwfile }} {% endif %}
{% if postgresql_wal_directory != "" and postgresql_version is version_compare('10', '<') %}--xlogdir={{ postgresql_wal_directory }} {% endif %}
{% if postgresql_wal_directory != "" and postgresql_version is version_compare('10', '>=') %}--waldir={{ postgresql_wal_directory }} {% endif %}
become: yes
become_user: "{{ postgresql_service_user }}"
when: ansible_os_family == "Debian" and postgresql_cluster_reset and pgdata_dir_exist.changed
- name: PostgreSQL | Update systemd | Debian
command: systemctl daemon-reload
become: yes
when: ansible_os_family == "Debian" and postgresql_cluster_reset and pgdata_dir_exist.changed
- name: PostgreSQL | Start PostgreSQL | Debian
service:
name: "{{ postgresql_service_name }}"
state: started
become: yes
when: ansible_os_family == "Debian" and postgresql_cluster_reset and pgdata_dir_exist.changed
- name: PostgreSQL | Check whether the postgres data directory is initialized | RedHat
stat:
path: "{{ postgresql_data_directory }}/PG_VERSION"
when: ansible_os_family == "RedHat" and not postgresql_cluster_reset
register: pgdata_dir_initialized
- name: PostgreSQL | Initialize the database | RedHat
command: >
{{ postgresql_bin_directory }}/initdb -D {{ postgresql_data_directory }}
--locale={{ postgresql_locale }} --encoding={{ postgresql_encoding }}
{% if postgresql_data_checksums and postgresql_version is version_compare('9.3', '>=') %}--data-checksums{% endif %}
{% if postgresql_pwfile != "" %}--pwfile={{ postgresql_pwfile }} {% endif %}
{% if postgresql_wal_directory != "" and postgresql_version is version_compare('10', '<') %}--xlogdir={{ postgresql_wal_directory }} {% endif %}
{% if postgresql_wal_directory != "" and postgresql_version is version_compare('10', '>=') %}--waldir={{ postgresql_wal_directory }} {% endif %}
{% if postgresql_wal_segsize != "" and postgresql_version is version_compare('11', '>=') %}--wal-segsize={{ postgresql_wal_segsize }} {% endif %}
become: yes
become_user: "{{ postgresql_service_user }}"
when: ansible_os_family == "RedHat" and
(postgresql_cluster_reset or
pgdata_dir_exist.changed or
not pgdata_dir_initialized.stat.exists)
- name: PostgreSQL | Verify postgresql cluster version
command: grep ^{{ postgresql_version }}$ {{ postgresql_data_directory }}/PG_VERSION
changed_when: false
- name: PostgreSQL | Ensure configuration directory exists
file:
path: "{{ postgresql_conf_directory }}"
owner: "{{ postgresql_service_user }}"
group: "{{ postgresql_service_group }}"
mode: 0700
state: directory
- name: PostgreSQL | Update configuration - pt. 1 (pg_hba.conf)
template:
src: pg_hba.conf.j2
dest: "{{postgresql_conf_directory}}/pg_hba.conf"
owner: "{{ postgresql_service_user }}"
group: "{{ postgresql_service_group }}"
mode: 0640
no_log: "{{ postgresql_hide_passwords }}"
register: postgresql_configuration_pt1
- name: PostgreSQL | Update configuration - pt. 2 (postgresql.conf)
template:
src: "postgresql.conf-{{ postgresql_version }}.j2"
# if using pgtune, save the template to ".untuned"
dest: "{{postgresql_conf_directory}}/postgresql.conf{% if postgresql_pgtune %}.untuned{% endif %}"
owner: "{{ postgresql_service_user }}"
group: "{{ postgresql_service_group }}"
mode: 0640
register: postgresql_configuration_pt2
- name: PostgreSQL | Update configuration - pt. 3 (pgtune)
become: true
become_user: "{{ postgresql_service_user }}"
shell: |
set -e
TMPDIR=$(mktemp -d)
pgtune --input-config=postgresql.conf.untuned\
--output-config=$TMPDIR/postgresql.conf\
{% if postgresql_pgtune_memory %}--memory {{postgresql_pgtune_memory|int}}{% endif %}\
--type {{postgresql_pgtune_type|quote}}\
{% if postgresql_pgtune_connections %}--connections {{postgresql_pgtune_connections|int}}{% endif %}\
# Compare the current config with the one procuded by pgtune (ignoring comments/blanks)
if diff --ignore-blank-lines \
<(sed -e 's/#.*//' postgresql.conf) \
<(sed -e 's/#.*//' $TMPDIR/postgresql.conf)
then
echo '_OK_'
else
cp $TMPDIR/postgresql.conf postgresql.conf
fi
# Cleanup
rm $TMPDIR/postgresql.conf
rmdir $TMPDIR
args:
chdir: "{{postgresql_conf_directory}}"
executable: "/bin/bash"
when: postgresql_pgtune
register: postgresql_configuration_pt3
changed_when: "'_OK_' not in postgresql_configuration_pt3.stdout"
- name: PostgreSQL | Update configuration - pt. 4 (pg_ident.conf)
template:
src: pg_ident.conf.j2
dest: "{{ postgresql_ident_file }}"
owner: "{{ postgresql_service_user }}"
group: "{{ postgresql_service_group }}"
mode: 0640
register: postgresql_configuration_pt4
notify: restart postgresql
- name: PostgreSQL | Create folder for additional configuration files
file:
name: "{{postgresql_conf_directory}}/conf.d"
state: directory
owner: "{{ postgresql_service_user }}"
group: "{{ postgresql_service_group }}"
mode: 0755
- name: PostgreSQL | Ensure the systemd directory for PostgreSQL exists | RedHat
file:
name: "/etc/systemd/system/postgresql-{{ postgresql_version }}.service.d"
state: directory
mode: 0755
when: ansible_os_family == "RedHat"
notify: restart postgresql
- name: PostgreSQL | Use the conf directory when starting the Postgres service | RedHat
template:
src: etc_systemd_system_postgresql.service.d_custom.conf.j2
dest: "/etc/systemd/system/postgresql-{{ postgresql_version }}.service.d/custom.conf"
when: ansible_os_family == "RedHat"
register: postgresql_systemd_custom_conf
- name: PostgreSQL | Ensure the pid directory for PostgreSQL exists
file:
name: "{{ postgresql_pid_directory }}"
state: directory
owner: "{{ postgresql_service_user }}"
group: "{{ postgresql_service_group }}"
mode: u=rwX,g=rwXs,o=rx
notify: restart postgresql
- block:
- name: PostgreSQL | Ensure PostgreSQL is running
service:
name: "{{ postgresql_service_name }}"
state: started
register: postgresql_cold_started
- name: PostgreSQL | Reload PostgreSQL configuration
service:
name: "{{ postgresql_service_name }}"
state: reloaded
register: postgresql_reloaded
when: not postgresql_cold_started.changed
when: postgresql_configuration_pt1.changed or postgresql_configuration_pt2.changed or postgresql_configuration_pt3.changed or postgresql_systemd_custom_conf.changed