From 682cfa62593a5fa640091586bec2836547cf9e5a Mon Sep 17 00:00:00 2001 From: nd Date: Thu, 13 Sep 2018 20:18:19 +0200 Subject: [PATCH] added support to create users and dbs --- defaults/main.yml | 4 +++- tasks/main.yml | 22 +++++++++++++++++++++- templates/pg_hba.conf.j2 | 7 ++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/defaults/main.yml b/defaults/main.yml index 3cb7c70..c03add9 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,2 +1,4 @@ postgresql: - allowed_hosts: [] + allowed_hosts: {} + dbs: {} + users: {} diff --git a/tasks/main.yml b/tasks/main.yml index 358e156..db92b84 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,6 +1,11 @@ - name: install postgres apt: - pkg: postgresql + pkg: "{{ item }}" + state: latest + update_cache: yes + with_items: + - postgresql + - python-psycopg2 - name: generate pg_hba template: @@ -11,3 +16,18 @@ mode: 0640 notify: - restart postgres + +- name: add postgresql dbs + postgresql_db: + name: "{{ item.key }}" + become_user: postgres + become: true + with_dict: "{{ postgresql.dbs }}" + +- name: add postgresql user(s) + postgresql_user: + name: "{{ item.key }}" + password: "{{ item.value }}" + become_user: postgres + become: true + with_dict: "{{ postgresql.users }}" diff --git a/templates/pg_hba.conf.j2 b/templates/pg_hba.conf.j2 index 777d57d..a1f9ddf 100644 --- a/templates/pg_hba.conf.j2 +++ b/templates/pg_hba.conf.j2 @@ -1,7 +1,12 @@ -{% for item in postgresql.allowed_hosts %} +{% for group in postgresql.allowed_hosts %} +# {{ group }} +{% for item in postgresql.allowed_hosts[group] %} {{ item.type | default('host') }} {{ item.database | default('all') }} {{ item.user | default('all') }} {{ item.auth | default('md5') }} {% endfor %} + +{% endfor %} + local all postgres peer local all all peer host all all 127.0.0.1/32 md5