forked from ANXS/postgresql
/
databases.yml
90 lines (80 loc) · 4.33 KB
/
databases.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
# file: postgresql/tasks/databases.yml
- name: PostgreSQL | Ensure PostgreSQL is running
service:
name: "{{ postgresql_service_name }}"
state: started
- name: PostgreSQL | Make sure the PostgreSQL databases are present
postgresql_db:
name: "{{item.name}}"
owner: "{{ item.owner | default(postgresql_database_owner) }}"
encoding: "{{postgresql_encoding}}"
lc_collate: "{{postgresql_locale}}"
lc_ctype: "{{postgresql_locale}}"
port: "{{postgresql_port}}"
template: "template0"
state: present
login_user: "{{postgresql_admin_user}}"
become: yes
become_user: "{{postgresql_admin_user}}"
with_items: "{{postgresql_databases}}"
when: postgresql_databases|length > 0
- name: PostgreSQL | Add extensions to the databases
shell: "psql {{item.0.db}} --username {{postgresql_admin_user}} -c 'CREATE EXTENSION IF NOT EXISTS {{ item.1 }};'"
become: yes
become_user: "{{postgresql_service_user}}"
with_subelements:
- "{{postgresql_database_extensions}}"
- extensions
register: result
changed_when: "'NOTICE' not in result.stderr"
- name: PostgreSQL | Add hstore to the databases with the requirement
become: yes
become_user: "{{postgresql_service_user}}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{postgresql_admin_user}} -c 'CREATE EXTENSION IF NOT EXISTS hstore;'"
with_items: "{{postgresql_databases}}"
register: hstore_ext_result
failed_when: hstore_ext_result.rc != 0 and ("already exists, skipping" not in hstore_ext_result.stderr)
changed_when: hstore_ext_result.rc == 0 and ("already exists, skipping" not in hstore_ext_result.stderr)
when: item.hstore is defined and item.hstore
- name: PostgreSQL | Add uuid-ossp to the database with the requirement
become: yes
become_user: "{{postgresql_service_user}}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{postgresql_admin_user}} -c 'CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";'"
with_items: "{{postgresql_databases}}"
register: uuid_ext_result
failed_when: uuid_ext_result.rc != 0 and ("already exists, skipping" not in uuid_ext_result.stderr)
changed_when: uuid_ext_result.rc == 0 and ("already exists, skipping" not in uuid_ext_result.stderr)
when: item.uuid_ossp is defined and item.uuid_ossp
- name: PostgreSQL | Add postgis to the databases with the requirement
become: yes
become_user: "{{postgresql_service_user}}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{postgresql_admin_user}} -c 'CREATE EXTENSION IF NOT EXISTS postgis;'&&psql {{item.name}} -c 'CREATE EXTENSION IF NOT EXISTS postgis_topology;'"
with_items: "{{postgresql_databases}}"
when: item.gis is defined and item.gis
- name: PostgreSQL | add cube to the database with the requirement
become: yes
become_user: "{{postgresql_service_user}}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{ postgresql_admin_user }} -c 'create extension if not exists cube;'"
with_items: "{{postgresql_databases}}"
when: item.cube is defined and item.cube
- name: PostgreSQL | Add plpgsql to the database with the requirement
become: yes
become_user: "{{postgresql_service_user}}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{ postgresql_admin_user }} -c 'CREATE EXTENSION IF NOT EXISTS plpgsql;'"
with_items: "{{postgresql_databases}}"
when: item.plpgsql is defined and item.plpgsql
- name: PostgreSQL | add earthdistance to the database with the requirement
become: yes
become_user: "{{postgresql_service_user}}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{ postgresql_admin_user }} -c 'create extension if not exists earthdistance;'"
with_items: "{{postgresql_databases}}"
when: item.earthdistance is defined and item.earthdistance
- name: PostgreSQL | Add citext to the database with the requirement
become: yes
become_user: "{{postgresql_service_user}}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{postgresql_admin_user}} -c 'CREATE EXTENSION IF NOT EXISTS citext;'"
with_items: "{{postgresql_databases}}"
register: citext_ext_result
failed_when: citext_ext_result.rc != 0 and ("already exists, skipping" not in citext_ext_result.stderr)
changed_when: citext_ext_result.rc == 0 and ("already exists, skipping" not in citext_ext_result.stderr)
when: item.citext is defined and item.citext