From dc34456d0cf0ef40f22205f3b88f76d90948a225 Mon Sep 17 00:00:00 2001 From: Pascal Armand Date: Mon, 4 Oct 2021 16:55:43 +0200 Subject: [PATCH 1/2] add optional schema for database extentions --- README.md | 1 + tasks/databases.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/README.md b/README.md index 5ad10a15..9c243967 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/tasks/databases.yml b/tasks/databases.yml index 72d0f3ea..06ac0049 100644 --- a/tasks/databases.yml +++ b/tasks/databases.yml @@ -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 }}" From 6af8570bd1948e5e3304e48e49bd605e7f95e7f3 Mon Sep 17 00:00:00 2001 From: Pascal Armand Date: Mon, 4 Oct 2021 17:31:52 +0200 Subject: [PATCH 2/2] enable extension after create database and schemas --- tasks/extensions.yml | 79 ++++++++++++++++++++++++++++++++++++---- tasks/extensions_pkg.yml | 8 ++++ tasks/main.yml | 5 ++- 3 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 tasks/extensions_pkg.yml diff --git a/tasks/extensions.yml b/tasks/extensions.yml index e7baac77..741c704d 100644 --- a/tasks/extensions.yml +++ b/tasks/extensions.yml @@ -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 diff --git a/tasks/extensions_pkg.yml b/tasks/extensions_pkg.yml new file mode 100644 index 00000000..e7baac77 --- /dev/null +++ b/tasks/extensions_pkg.yml @@ -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 diff --git a/tasks/main.yml b/tasks/main.yml index 9eedb061..ec779c90 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -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 @@ -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]