Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optional schema for database extentions #519

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ postgresql_databases:
# List of database extensions to be created (optional)
postgresql_database_extensions:
- db: foobar
schema: foobar # optional (default public)
extensions:
- hstore
- citext
Expand Down
1 change: 1 addition & 0 deletions tasks/databases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
become_user: "{{ postgresql_admin_user }}"
postgresql_ext:
db: "{{ item.0.db }}"
schema: "{{ item.0.schema | default('public') }}"
login_user: "{{ postgresql_service_user }}"
port: "{{ postgresql_port }}"
name: "{{ item.1 }}"
Expand Down
79 changes: 71 additions & 8 deletions tasks/extensions.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,71 @@
# file: postgresql/tasks/extensions.yml

- import_tasks: extensions/contrib.yml
when: postgresql_ext_install_contrib
- import_tasks: extensions/dev_headers.yml
when: postgresql_ext_install_dev_headers
- import_tasks: extensions/postgis.yml
when: postgresql_ext_install_postgis
- name: PostgreSQL | Add extensions to the databases
become: yes
become_user: "{{ postgresql_admin_user }}"
postgresql_ext:
db: "{{ item.0.db }}"
schema: "{{ item.0.schema | default('public') }}"
login_user: "{{ postgresql_service_user }}"
port: "{{ postgresql_port }}"
name: "{{ item.1 }}"
with_subelements:
- "{{ postgresql_database_extensions }}"
- extensions
register: result

- name: PostgreSQL | Add hstore to the databases with the requirement
become: yes
become_user: "{{postgresql_service_user}}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --port={{ postgresql_port | int }} --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}} --port={{ postgresql_port | int }} --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}} --port={{ postgresql_port | int }} --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}} --port={{ postgresql_port | int }} --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}} --port={{ postgresql_port | int }} --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}} --port={{ postgresql_port | int }} --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}} --port={{ postgresql_port | int }} --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
8 changes: 8 additions & 0 deletions tasks/extensions_pkg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# file: postgresql/tasks/extensions.yml

- import_tasks: extensions/contrib.yml
when: postgresql_ext_install_contrib
- import_tasks: extensions/dev_headers.yml
when: postgresql_ext_install_dev_headers
- import_tasks: extensions/postgis.yml
when: postgresql_ext_install_postgis
5 changes: 4 additions & 1 deletion tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
when: ansible_pkg_mgr == "dnf" and ansible_distribution == "Fedora"
tags: [postgresql, postgresql-install]

- import_tasks: extensions.yml
- import_tasks: extensions_pkg.yml
tags: [postgresql, postgresql-extensions]

- import_tasks: fdw.yml
Expand All @@ -46,6 +46,9 @@
- import_tasks: schemas.yml
tags: [postgresql, postgresql-schemas]

- import_tasks: extensions.yml
tags: [postgresql, postgresql-extensions]

- import_tasks: users_privileges.yml
tags: [postgresql, postgresql-users]

Expand Down