This repository has been archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.yml
381 lines (331 loc) · 14 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
---
- hosts: all
become: true
vars:
postgresql_version: 13
tomcat_install_method: source
pre_tasks:
- name: Download latest versions of roles baosystems.postgresql and baosystems.tomcat
command:
argv:
- ansible-galaxy
- role
- install
- --roles-path=/etc/ansible/roles
- --force
- --force-with-deps
- "https://bitbucket.org/baosystems/{{ item }}/downloads/master.tar.gz,master,{{ item }}"
loop:
- baosystems.postgresql
- baosystems.tomcat
- name: Query additional requirements for baosystems.postgresql
uri:
url: "https://bitbucket.org/baosystems/baosystems.postgresql/raw/master/molecule/shared/common/requirements.yml"
return_content: true
register: pg_reqs
no_log: true
- name: Download additional requirements for baosystems.postgresql not in its meta/main.yml
command:
argv:
- ansible-galaxy
- role
- install
- --roles-path=/etc/ansible/roles
- --force
- --force-with-deps
- "{{ item.src }},{{ item.version }}"
loop: "{{ pg_reqs.content | from_yaml }}"
tasks:
- name: "Tomcat: install with role baosystems.tomcat"
include_role:
name: baosystems.tomcat
- name: PostgreSQL
tags: postgresql
block:
- name: "PostgreSQL: install with role baosystems.postgresql"
include_role:
name: baosystems.postgresql
- name: "PostgreSQL: Set up conf.d/50-dhis2.conf"
copy:
src: files/postgresql/50-dhis2.conf
dest: "/var/lib/pgsql/{{ postgresql_version }}/data/conf.d/50-dhis2.conf"
owner: postgres
group: postgres
backup: false
notify: restart postgresql
- name: "PostgreSQL: Set up pg_ident.conf"
copy:
src: files/postgresql/pg_ident.conf
dest: "/var/lib/pgsql/{{ postgresql_version }}/data/pg_ident.conf"
owner: postgres
group: postgres
backup: false
notify: restart postgresql
- name: "PostgreSQL: Install PostGIS"
package:
name: "postgis30_{{ postgresql_version }}"
notify: restart postgresql
- name: "PostgreSQL: flush_handlers before proceeding so the postgres user can connect over the socket."
meta: flush_handlers
- name: "PostgreSQL: Wait for service to start"
wait_for:
port: 5432
- name: "PostgreSQL: 'dhis' user"
postgresql_user:
name: dhis
encrypted: true
password: "{{ lookup('password', '/tmp/pg_dhis_passwd.txt chars=ascii_letters,digits') }}"
- name: "PostgreSQL: 'dhis2' database"
postgresql_db:
name: dhis2
owner: dhis
- name: "PostgreSQL: 'dhis2' database, 'public' schema"
postgresql_schema:
database: dhis2
name: public
owner: dhis
- name: "PostgreSQL: PostGIS schema setup"
block:
- name: "PostgreSQL: 'dhis2' database, 'postgis' schema"
postgresql_schema:
database: dhis2
name: postgis
owner: postgres
- name: "PostgreSQL: Check database 'dhis2' value 'search_path'"
shell: |
psql \
--username="postgres" \
--tuples-only \
--no-align \
--command="SELECT rs.setconfig FROM pg_db_role_setting rs LEFT JOIN pg_roles r ON r.oid = rs.setrole LEFT JOIN pg_database d ON d.oid = rs.setdatabase WHERE r.rolname = 'dhis' OR d.datname = 'dhis2';" \
| grep -q -E '(=|,\s*)postgis("|,)'
args:
warn: false
ignore_errors: true
changed_when: false
register: pgdg_check_search_path
- name: "PostgreSQL: 'ALTER DATABASE dhis2 SET search_path TO public,postgis;'"
shell: |
psql \
--username="postgres" \
--command="ALTER DATABASE dhis2 SET search_path TO public,postgis;"
when: pgdg_check_search_path.rc == 1
- name: "PostgreSQL: Check default privs 'dhis2' schema 'postgis' select all tables"
shell: |
psql \
--username="postgres" \
--tuples-only \
--no-align \
--dbname="dhis2" \
--command="SELECT defaclacl FROM pg_default_acl a JOIN pg_namespace b ON a.defaclnamespace=b.oid WHERE nspname = 'postgis' AND defaclobjtype = 'r';" \
| grep -q 'dhis=r/postgres'
args:
warn: false
ignore_errors: true
changed_when: false
register: pgdg_check_defaults_tables
- name: "PostgreSQL: 'ALTER DEFAULT PRIVILEGES IN SCHEMA postgis GRANT SELECT ON TABLES TO dhis;'"
shell: |
psql \
--username="postgres" \
--dbname="dhis2" \
--command="ALTER DEFAULT PRIVILEGES IN SCHEMA postgis GRANT SELECT ON TABLES TO dhis;"
when: pgdg_check_defaults_tables.rc == 1
- name: "PostgreSQL: Check default privs 'dhis2' schema 'postgis' usage, select all sequences"
shell: |
psql \
--username="postgres" \
--tuples-only \
--no-align \
--dbname="dhis2" \
--command="SELECT defaclacl FROM pg_default_acl a JOIN pg_namespace b ON a.defaclnamespace=b.oid WHERE nspname = 'postgis' AND defaclobjtype = 'S';" \
| grep -q 'dhis=rU/postgres'
args:
warn: false
ignore_errors: true
changed_when: false
register: pgdg_check_defaults_sequences
- name: "PostgreSQL: 'ALTER DEFAULT PRIVILEGES IN SCHEMA postgis GRANT USAGE, SELECT ON SEQUENCES TO dhis;'"
shell: |
psql \
--username="postgres" \
--dbname="dhis2" \
--command="ALTER DEFAULT PRIVILEGES IN SCHEMA postgis GRANT USAGE, SELECT ON SEQUENCES TO dhis;"
when: pgdg_check_defaults_sequences.rc == 1
- name: "PostgreSQL: Check default privs 'dhis2' schema 'postgis' execute all functions"
shell: |
psql \
--username="postgres" \
--tuples-only \
--no-align \
--dbname="dhis2" \
--command="SELECT defaclacl FROM pg_default_acl a JOIN pg_namespace b ON a.defaclnamespace=b.oid WHERE nspname = 'postgis' AND defaclobjtype = 'f';" \
| grep -q 'dhis=X/postgres'
args:
warn: false
ignore_errors: true
changed_when: false
register: pgdg_check_defaults_functions
- name: "PostgreSQL: 'ALTER DEFAULT PRIVILEGES IN SCHEMA postgis GRANT EXECUTE ON FUNCTIONS TO dhis;'"
shell: |
psql \
--username="postgres" \
--dbname="dhis2" \
--command="ALTER DEFAULT PRIVILEGES IN SCHEMA postgis GRANT EXECUTE ON FUNCTIONS TO dhis;"
when: pgdg_check_defaults_functions.rc == 1
- name: "PostgreSQL: Check for extension 'postgis' in schema 'postgis'"
shell: |
psql \
--username="postgres" \
--tuples-only \
--no-align \
--field-separator='|' \
--dbname="dhis2" \
--command="\dx" \
| grep -q -E '^postgis\|[0-9.]+[^\|]*\|postgis\|'
args:
warn: false
ignore_errors: true
changed_when: false
register: pgdg_check_extension_postgis
- name: "PostgreSQL: 'CREATE EXTENSION postgis WITH SCHEMA postgis;'"
shell: |
psql \
--username="postgres" \
--dbname="dhis2" \
--command="CREATE EXTENSION postgis WITH SCHEMA postgis;"
when: pgdg_check_extension_postgis.rc == 1
- name: "PostgreSQL: 'GRANT USAGE ON SCHEMA postgis TO dhis;'"
postgresql_privs:
database: dhis2
privs: USAGE
type: schema
objs: postgis
roles: dhis
- name: "PostgreSQL: 'GRANT SELECT ON ALL TABLES IN SCHEMA postgis TO dhis;'"
postgresql_privs:
database: dhis2
privs: SELECT
type: table
objs: ALL_IN_SCHEMA
schema: postgis
roles: dhis
- name: "PostgreSQL: 'GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA postgis TO dhis;'"
postgresql_privs:
database: dhis2
privs: USAGE,SELECT
type: sequence
objs: ALL_IN_SCHEMA
schema: postgis
roles: dhis
- name: "PostgreSQL: Check privs 'dhis2' schema 'postgis' execute functions"
# Count the number of functions in the 'postgis' schema with the permission 'dhis=X/postgres'
shell: |
psql \
--username="postgres" \
--tuples-only \
--no-align \
--field-separator='|' \
--dbname="dhis2" \
--command="SELECT p.proname, p.proacl FROM pg_catalog.pg_proc p LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace WHERE n.nspname OPERATOR(pg_catalog.~) '^(postgis)$';" \
| sort -u \
| grep -v 'dhis=X/postgres' \
| wc -l
args:
warn: false
ignore_errors: true
changed_when: false
register: pgdg_check_function_permissions
- name: "PostgreSQL: 'GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA postgis TO dhis;'"
shell: |
psql \
--username="postgres" \
--dbname="dhis2" \
--command="GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA postgis TO dhis;"
when: pgdg_check_function_permissions.stdout != "0"
- name: DHIS2
tags: dhis2
block:
- name: "DHIS2: Create DHIS2_HOME directory /opt/dhis2"
file:
path: /opt/dhis2
state: directory
owner: tomcat
group: tomcat
- name: "DHIS2: Configuration file /opt/dhis2/dhis.conf with database credentials"
copy:
dest: /opt/dhis2/dhis.conf
owner: tomcat
group: tomcat
backup: false
content: |
connection.dialect = org.hibernate.dialect.PostgreSQLDialect
connection.driver_class = org.postgresql.Driver
connection.url = jdbc:postgresql:dhis2
connection.username = dhis
connection.password = {{ lookup('password', '/tmp/pg_dhis_passwd.txt chars=ascii_letters,digits') }}
connection.schema = update
- name: "DHIS2: Download DHIS2 {{dhis2_version}} war file to Tomcat webapps"
get_url:
url: "https://s3.amazonaws.com/dhis2-builds/{{ dhis2_version }}/latest/dhis.war"
checksum: "sha256:https://s3.amazonaws.com/dhis2-builds/{{ dhis2_version }}/latest/dhis.war.sha256"
dest: /var/lib/tomcat/webapps/ROOT.war
owner: tomcat
group: tomcat
- name: "DHIS2: Create directory to unarchive dhis.war into"
file:
path: /var/lib/tomcat/webapps/ROOT
state: directory
owner: tomcat
group: tomcat
- name: "DHIS2: Unarchive dhis.war"
unarchive:
src: /var/lib/tomcat/webapps/ROOT.war
dest: /var/lib/tomcat/webapps/ROOT/
creates: /var/lib/tomcat/webapps/ROOT/WEB-INF/web.xml
owner: tomcat
group: tomcat
- name: Nginx
tags: nginx
block:
- name: "Nginx: install package"
package:
name: nginx
state: present
- name: "Nginx: Configure proxy to Tomcat"
block:
- name: "Nginx: client_max_body_size 32m"
lineinfile:
path: /etc/nginx/nginx.conf
insertafter: '^[^#]\s+access_log\s+/var/log/nginx/access.log\s+main;\s+'
line: ' client_max_body_size 32m;'
validate: nginx -c %s -t
- name: "Nginx: proxy_pass"
lineinfile:
path: /etc/nginx/nginx.conf
insertafter: '^[^#]\s+location\s+/\s+{'
line: ' proxy_pass http://localhost:8080;'
validate: nginx -c %s -t
- name: "Nginx: proxy_redirect"
lineinfile:
path: /etc/nginx/nginx.conf
insertafter: '^[^#]\s+proxy_pass\s+'
line: ' proxy_redirect off;'
validate: nginx -c %s -t
- name: "Nginx: proxy_set_header Host"
lineinfile:
path: /etc/nginx/nginx.conf
insertafter: '^[^#]\s+proxy_redirect\s+'
line: ' proxy_set_header Host $http_host;'
validate: nginx -c %s -t
- name: "Nginx: proxy_set_header X-Forwarded-Proto"
lineinfile:
path: /etc/nginx/nginx.conf
insertafter: '^[^#]\s+proxy_set_header\s+Host\s+'
line: ' proxy_set_header X-Forwarded-Proto $scheme;'
validate: nginx -c %s -t
- name: "Nginx: Start service and enable to start on boot"
service:
name: nginx
state: started
enabled: true