]> git.immae.eu Git - github/fretlink/ansible-postgresql-role.git/commitdiff
feat(extensions): adds creation of extension on databases if needed
authorPaul B <paul@bonaud.fr>
Fri, 25 Jan 2019 10:55:41 +0000 (11:55 +0100)
committerPaul B <paul@bonaud.fr>
Tue, 29 Jan 2019 10:42:40 +0000 (11:42 +0100)
tasks/main.yml
tasks/postgres-common-postinstall.yml
tasks/postgres-database-extensions.yml [new file with mode: 0644]
tasks/postgres-database.yml

index 2fa78839d1d2f5ce06024dd31851dfd77c041cc5..3d1892c787e98ec9b32c31de3615ca68f8b375c9 100644 (file)
@@ -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 }}"
index 277fafd283e1f5b5e132337ac8f75a1d8519448c..715782c5a2ed5f8ba580314b945dd3272dfc4c45 100644 (file)
     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 (file)
index 0000000..edc4e8d
--- /dev/null
@@ -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
index c7b09a83a7548fa9e8f463ae0c98a24a5f9376e5..c3537eaaeef61c7b30121b348885d1e38bad64cf 100644 (file)
     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