From 225e70a6933e41ce0d9e204a77e70668f5142f46 Mon Sep 17 00:00:00 2001 From: Andrea Dell'Amico Date: Tue, 13 Sep 2016 16:34:08 +0200 Subject: [PATCH] library/roles/postgresql: Fix the management of variables that are used to decide to create ad DB or not. --- postgresql/defaults/main.yml | 6 ++++-- postgresql/tasks/manage_pg_db.yml | 8 ++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/postgresql/defaults/main.yml b/postgresql/defaults/main.yml index edb77e3..602f12e 100644 --- a/postgresql/defaults/main.yml +++ b/postgresql/defaults/main.yml @@ -65,9 +65,11 @@ postgresql_pgpool_pkgs: #psql_db_data: # Example of line needed to create a db, create the user that owns the db, manage the db accesses (used by iptables too). All the fields are mandatory. - #- { name: '{{ psql_db_name }}', encoding: 'UTF8', user: '{{ psql_db_user }}', pwd: '{{ psql_db_pwd }}', roles: 'NOCREATEDB,NOSUPERUSER', extensions: [ 'postgis', 'pgpool_regclass', 'pgpool_recovery' ], allowed_hosts: [ 'xxx.xxx.xxx.xxx/32', 'yyy.yyy.yyy.yyy/32' ] } + #- { name: '{{ psql_db_name }}', encoding: 'UTF8', user: '{{ psql_db_user }}', pwd: '{{ psql_db_pwd }}', roles: 'NOCREATEDB,NOSUPERUSER', extensions: [ 'postgis', 'pgpool_regclass', 'pgpool_recovery' ], allowed_hosts: [ 'xxx.xxx.xxx.xxx/32', 'yyy.yyy.yyy.yyy/32' ], managedb: True } # Example of line needed to manage the db accesses (used by iptables too), without creating the db and the user. Useful, for example, to give someone access to the postgresql db - #- { name: '{{ psql_db_name }}', user: '{{ psql_db_user }}', allowed_hosts: [ 'xxx.xxx.xxx.xxx/32', 'yyy.yyy.yyy.yyy/32' ] } + #- { name: '{{ psql_db_name }}', user: '{{ psql_db_user }}', allowed_hosts: [ 'xxx.xxx.xxx.xxx/32', 'yyy.yyy.yyy.yyy/32' ], managedb: False } + # Example of line needed to remove a db, create the user that owns the db, manage the db accesses (used by iptables too). All the fields are mandatory. + #- { name: '{{ psql_db_name }}', encoding: 'UTF8', user: '{{ psql_db_user }}', pwd: '{{ psql_db_pwd }}', managedb: True, roles: 'NOCREATEDB,NOSUPERUSER', extensions: [ 'postgis', 'pgpool_regclass', 'pgpool_recovery' ], allowed_hosts: [ 'xxx.xxx.xxx.xxx/32', 'yyy.yyy.yyy.yyy/32' ], state=absent } # pgpool-II diff --git a/postgresql/tasks/manage_pg_db.yml b/postgresql/tasks/manage_pg_db.yml index e4b3232..ff6554d 100644 --- a/postgresql/tasks/manage_pg_db.yml +++ b/postgresql/tasks/manage_pg_db.yml @@ -2,17 +2,17 @@ - name: Add a user for the postgresql DBs become: True become_user: postgres - postgresql_user: user={{ item.user }} password={{ item.pwd }} role_attr_flags={{ item.roles }} port={{ psql_db_port }} + postgresql_user: user={{ item.user }} password={{ item.pwd }} role_attr_flags={{ item.roles }} port={{ psql_db_port }} state={{ item.userstate | default('present') }} with_items: '{{ psql_db_data }}' when: item.roles is defined tags: [ 'postgresql', 'postgres', 'pg_db' ] -- name: Add the databases with the correct owner +- name: Add the databases with the correct owner. Or remove them, if not used anymore become: True become_user: postgres - postgresql_db: db={{ item.name }} port={{ psql_db_port }} encoding={{ item.encoding }} owner={{ item.user }} template=template0 state=present + postgresql_db: db={{ item.name }} port={{ psql_db_port }} encoding={{ item.encoding }} owner={{ item.user }} template=template0 state={{ item.state | default('present') }} with_items: '{{ psql_db_data }}' - when: ( item.createdb is not defined or item.createdb ) + when: item.managedb tags: [ 'postgresql', 'postgres', 'pg_db' ] - name: Only set a db user password. Mostly for the postgresql user