From 9e343a0bf1acc457f0c222421a05c32e2be9b38a Mon Sep 17 00:00:00 2001 From: Andrea Dell'Amico Date: Tue, 26 Mar 2019 18:41:03 +0100 Subject: [PATCH] postgresql: manage the change of the data directory. --- postgresql/tasks/postgresql-config.yml | 35 +++++++++++++++++++------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/postgresql/tasks/postgresql-config.yml b/postgresql/tasks/postgresql-config.yml index b27fcc0..a70ea5a 100644 --- a/postgresql/tasks/postgresql-config.yml +++ b/postgresql/tasks/postgresql-config.yml @@ -1,14 +1,31 @@ --- -- name: Create the postgresql data directory if it is not in the default place - file: dest={{ psql_data_dir }} owner=postgres group=postgres mode=700 recurse=yes state=directory - when: psql_use_alternate_data_dir - tags: [ 'postgresql', 'postgres', 'pg_conf' ] +- block: + - name: Check if the new postgresql data directory exists + stat: path={{ psql_data_dir }} + register: postgresql_data_dir + + - name: Stop the postgresql service while reconfiguring the data directory + service: name=postgresql state=stopped + when: postgresql_data_dir.stat.isdir is not defined + + - name: Create the postgresql data directory if it is not in the default place + file: dest={{ psql_data_dir }} owner=postgres group=postgres mode=700 recurse=yes state=directory + + - name: Set the postgresql data dir if it is different from the default + become: True + become_user: postgres + action: configfile path={{ psql_conf_dir }}/postgresql.conf key=data_directory value="'{{ psql_data_dir }}'" + + - name: Copy the postgresql data directory into the new place + shell: '[ "/var/lib/postgresql/{{ psql_version }}/main" != "{{ psql_data_dir }}" ] && cp -a /var/lib/postgresql/{{ psql_version }}/main/* {{ psql_data_dir }}' + args: + creates: '{{ psql_data_dir }}/main/base' + when: postgresql_data_dir.stat.isdir is not defined + + - name: Start the postgresql service that will use the new data directory + service: name=postgresql state=started + when: postgresql_data_dir.stat.isdir is not defined -- name: Set the postgresql data dir if it is different from the default - become: True - become_user: postgres - action: configfile path={{ psql_conf_dir }}/postgresql.conf key=data_directory value="'{{ psql_data_dir }}'" - notify: Restart postgresql when: psql_use_alternate_data_dir tags: [ 'postgresql', 'postgres', 'pg_conf' ]