From 4d626d5a5c05f4676c0a403929de775404f9e63c Mon Sep 17 00:00:00 2001 From: Paul B Date: Fri, 25 Jan 2019 11:55:41 +0100 Subject: feat(extensions): adds creation of extension on databases if needed --- tasks/main.yml | 1 + tasks/postgres-common-postinstall.yml | 14 +++++++++++++- tasks/postgres-database-extensions.yml | 23 +++++++++++++++++++++++ tasks/postgres-database.yml | 7 +++++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tasks/postgres-database-extensions.yml diff --git a/tasks/main.yml b/tasks/main.yml index 2fa7883..3d1892c 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -46,6 +46,7 @@ - include: postgres-database.yml postgres_dbname={{ item.1.dbname }} postgres_owner={{ item.1.owner }} + postgres_extensions={{ item.1.extensions|default([]) }} postgres_port={{ item.0.port }} with_subelements: - "{{ postgres_clusters }}" diff --git a/tasks/postgres-common-postinstall.yml b/tasks/postgres-common-postinstall.yml index 277fafd..715782c 100644 --- a/tasks/postgres-common-postinstall.yml +++ b/tasks/postgres-common-postinstall.yml @@ -15,13 +15,25 @@ src: /var/lib/postgresql/.ssh/id_rsa.pub register: db_server_pub_key +- name: Check if barman is installed on barman server + getent: + database: passwd + key: "barman" + delegate_to: "{{ postgres_barman_server_public|default(postgres_barman_server) }}" + when: postgres_barman_server is defined + register: barman_user_exists + ignore_errors: true + changed_when: false + - name: Allow SSH access on barman server authorized_key: user: "barman" key: "{{ db_server_pub_key['content']|b64decode }}" state: present delegate_to: "{{ postgres_barman_server_public|default(postgres_barman_server) }}" - when: postgres_barman_server is defined + when: + - postgres_barman_server is defined + - barman_user_exists is succeeded - name: Copy logrotate configuration for postgresql template: src=logrotate-postgresql-common.j2 dest=/etc/logrotate.d/postgresql-common owner=root group=root mode=0644 diff --git a/tasks/postgres-database-extensions.yml b/tasks/postgres-database-extensions.yml new file mode 100644 index 0000000..edc4e8d --- /dev/null +++ b/tasks/postgres-database-extensions.yml @@ -0,0 +1,23 @@ +--- +- name: "Install required packages for 'postgres_ext' ansible module" + apt: + name: + - libpq-dev + - python-psycopg2 + +- name: Install system dependencies of extensions + apt: + name: "{{ postgres_extension.apt_deps | default([]) }}" + cache_valid_time: 86400 + +- name: "CREATE EXTENSION on database {{ postgres_dbname }}" + become: true + become_user: postgres + become_method: su + postgresql_ext: + name: "{{ postgres_extension_name }}" + db: "{{ postgres_dbname }}" + port: "{{ postgres_port }}" + loop: "{{ postgres_extension.names | default([]) }}" + loop_control: + loop_var: postgres_extension_name diff --git a/tasks/postgres-database.yml b/tasks/postgres-database.yml index c7b09a8..c3537ea 100644 --- a/tasks/postgres-database.yml +++ b/tasks/postgres-database.yml @@ -18,3 +18,10 @@ owner: "{{ postgres_owner }}" when: - postgres_up.rc == 0 + +- name: Create database extensions + include_tasks: postgres-database-extensions.yml + loop: "{{ postgres_extensions| default([]) }}" + loop_control: + loop_var: postgres_extension + when: postgres_up.rc == 0 -- cgit v1.2.3