Some code cleanup.
This commit is contained in:
parent
758b309594
commit
b6d5cae17b
|
@ -1,40 +1,56 @@
|
||||||
---
|
---
|
||||||
- name: Data directory for Deb/Ubuntu
|
- name: postgresql-config | Data directory for Deb/Ubuntu
|
||||||
|
when:
|
||||||
|
- psql_use_alternate_data_dir
|
||||||
|
- ansible_distribution_file_variety == "Debian"
|
||||||
|
tags: ['postgresql', 'postgres', 'pg_conf']
|
||||||
block:
|
block:
|
||||||
- name: Check if the new postgresql data directory exists
|
- name: postgresql-config | Check if the new postgresql data directory exists
|
||||||
stat: path={{ psql_data_dir }}
|
ansible.builtin.stat:
|
||||||
|
path: "{{ psql_data_dir }}"
|
||||||
register: postgresql_data_dir
|
register: postgresql_data_dir
|
||||||
|
|
||||||
- name: Stop the postgresql service while reconfiguring the data directory
|
- name: postgresql-config | Stop the postgresql service while reconfiguring the data directory
|
||||||
service: name=postgresql state=stopped
|
ansible.builtin.service:
|
||||||
|
name: postgresql
|
||||||
|
state: stopped
|
||||||
when: postgresql_data_dir.stat.isdir is not defined
|
when: postgresql_data_dir.stat.isdir is not defined
|
||||||
|
|
||||||
- name: Create the postgresql data directory if it is not in the default place
|
- name: postgresql-config | 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
|
ansible.builtin.file:
|
||||||
|
dest: "{{ psql_data_dir }}"
|
||||||
|
owner: postgres
|
||||||
|
group: postgres
|
||||||
|
mode: '700'
|
||||||
|
state: directory
|
||||||
|
recurse: true
|
||||||
|
|
||||||
- name: Set the postgresql data dir if it is different from the default
|
- name: postgresql-config | Set the postgresql data dir if it is different from the default
|
||||||
become: True
|
become: true
|
||||||
become_user: postgres
|
become_user: postgres
|
||||||
action: configfile path={{ psql_conf_dir }}/postgresql.conf key=data_directory value="'{{ psql_data_dir }}'"
|
community.postgresql.postgresql_set:
|
||||||
|
name: data_directory
|
||||||
|
value: "'{{ psql_data_dir }}'"
|
||||||
|
|
||||||
- name: Copy the postgresql data directory into the new place
|
- name: postgresql-config | 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 }}'
|
ansible.builtin.shell:
|
||||||
|
cmd: |
|
||||||
|
if [ "/var/lib/postgresql/{{ psql_version | quote }}/main" != "{{ psql_data_dir | quote }}" ] ; then
|
||||||
|
cp -a /var/lib/postgresql/{{ psql_version | quote }}/main/* {{ psql_data_dir | quote }}
|
||||||
|
fi
|
||||||
args:
|
args:
|
||||||
creates: '{{ psql_data_dir }}/main/base'
|
creates: '{{ psql_data_dir }}/main/base'
|
||||||
when: postgresql_data_dir.stat.isdir is not defined
|
when: postgresql_data_dir.stat.isdir is not defined
|
||||||
|
|
||||||
- name: Start the postgresql service that will use the new data directory
|
- name: postgresql-config | Start the postgresql service that will use the new data directory
|
||||||
service: name=postgresql state=started
|
ansible.builtin.service:
|
||||||
|
name: postgresql
|
||||||
|
state: started
|
||||||
when: postgresql_data_dir.stat.isdir is not defined
|
when: postgresql_data_dir.stat.isdir is not defined
|
||||||
|
|
||||||
when:
|
- name: postgresql-config | Configuration of Deb/Ubuntu systems
|
||||||
- psql_use_alternate_data_dir
|
|
||||||
- ansible_distribution_file_variety == "Debian"
|
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
|
||||||
|
|
||||||
- name: Configuration of Deb/Ubuntu systems
|
|
||||||
block:
|
block:
|
||||||
- name: Set some postgresql configuration parameters that require a db restart
|
- name: postgresql-config | Set some postgresql configuration parameters that require a db restart
|
||||||
become: True
|
become: True
|
||||||
become_user: postgres
|
become_user: postgres
|
||||||
action: configfile path={{ psql_conf_dir }}/postgresql.conf key={{ item.name }} value="{{ item.value }}"
|
action: configfile path={{ psql_conf_dir }}/postgresql.conf key={{ item.name }} value="{{ item.value }}"
|
||||||
|
@ -43,7 +59,7 @@
|
||||||
notify: Restart postgresql
|
notify: Restart postgresql
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
||||||
|
|
||||||
- name: Set the postgresql logging configuration parameters
|
- name: postgresql-config | Set the postgresql logging configuration parameters
|
||||||
become: True
|
become: True
|
||||||
become_user: postgres
|
become_user: postgres
|
||||||
action: configfile path={{ psql_conf_dir }}/postgresql.conf key={{ item.name }} value="{{ item.value }}"
|
action: configfile path={{ psql_conf_dir }}/postgresql.conf key={{ item.name }} value="{{ item.value }}"
|
||||||
|
@ -52,7 +68,7 @@
|
||||||
notify: Reload postgresql
|
notify: Reload postgresql
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf', 'pg_conf_log' ]
|
tags: [ 'postgresql', 'postgres', 'pg_conf', 'pg_conf_log' ]
|
||||||
|
|
||||||
- name: Set the postgresql autovacuum configuration parameters
|
- name: postgresql-config | Set the postgresql autovacuum configuration parameters
|
||||||
become: True
|
become: True
|
||||||
become_user: postgres
|
become_user: postgres
|
||||||
action: configfile path={{ psql_conf_dir }}/postgresql.conf key={{ item.name }} value="{{ item.value }}"
|
action: configfile path={{ psql_conf_dir }}/postgresql.conf key={{ item.name }} value="{{ item.value }}"
|
||||||
|
@ -61,52 +77,52 @@
|
||||||
notify: Reload postgresql
|
notify: Reload postgresql
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf', 'pg_conf_autovacuum' ]
|
tags: [ 'postgresql', 'postgres', 'pg_conf', 'pg_conf_autovacuum' ]
|
||||||
|
|
||||||
- name: Set the postgresql listen port
|
- name: postgresql-config | Set the postgresql listen port
|
||||||
action: configfile path={{ psql_conf_dir }}/postgresql.conf key=port value="{{ psql_db_port }}"
|
action: configfile path={{ psql_conf_dir }}/postgresql.conf key=port value="{{ psql_db_port }}"
|
||||||
register: restart_postgresql
|
register: restart_postgresql
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
||||||
|
|
||||||
- name: We want postgres listen on the public IP
|
- name: postgresql-config | We want postgres listen on the public IP
|
||||||
action: configfile path={{ psql_conf_dir }}/postgresql.conf key=listen_addresses value="'*'"
|
action: configfile path={{ psql_conf_dir }}/postgresql.conf key=listen_addresses value="'*'"
|
||||||
register: restart_postgresql
|
register: restart_postgresql
|
||||||
when:
|
when:
|
||||||
- psql_listen_on_ext_int
|
- psql_listen_on_ext_int
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
||||||
|
|
||||||
- name: If postgresql is only accessed from localhost make it listen only on the localhost interface
|
- name: postgresql-config | If postgresql is only accessed from localhost make it listen only on the localhost interface
|
||||||
action: configfile path={{ psql_conf_dir }}/postgresql.conf key=listen_addresses value="'localhost'"
|
action: configfile path={{ psql_conf_dir }}/postgresql.conf key=listen_addresses value="'localhost'"
|
||||||
register: restart_postgresql
|
register: restart_postgresql
|
||||||
when:
|
when:
|
||||||
- not psql_listen_on_ext_int
|
- not psql_listen_on_ext_int
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
||||||
|
|
||||||
- name: Log the connections
|
- name: postgresql-config | Log the connections
|
||||||
action: configfile path={{ psql_conf_dir }}/postgresql.conf key=log_connections value="on"
|
action: configfile path={{ psql_conf_dir }}/postgresql.conf key=log_connections value="on"
|
||||||
register: restart_postgresql
|
register: restart_postgresql
|
||||||
when: psql_db_data is defined
|
when: psql_db_data is defined
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
||||||
|
|
||||||
- name: Log the disconnections
|
- name: postgresql-config | Log the disconnections
|
||||||
action: configfile path={{ psql_conf_dir }}/postgresql.conf key=log_disconnections value="on"
|
action: configfile path={{ psql_conf_dir }}/postgresql.conf key=log_disconnections value="on"
|
||||||
register: restart_postgresql
|
register: restart_postgresql
|
||||||
when: psql_db_data is defined
|
when: psql_db_data is defined
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
||||||
|
|
||||||
- name: Log the hostnames
|
- name: postgresql-config | Log the hostnames
|
||||||
action: configfile path={{ psql_conf_dir }}/postgresql.conf key=log_hostname value="on"
|
action: configfile path={{ psql_conf_dir }}/postgresql.conf key=log_hostname value="on"
|
||||||
register: restart_postgresql
|
register: restart_postgresql
|
||||||
when:
|
when:
|
||||||
- psql_listen_on_ext_int
|
- psql_listen_on_ext_int
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
||||||
|
|
||||||
- name: Set the correct permissions to the postgresql files
|
- name: postgresql-config | Set the correct permissions to the postgresql files
|
||||||
file: dest={{ psql_conf_dir }}/{{ item }} owner=root group=postgres mode=0640
|
file: dest={{ psql_conf_dir }}/{{ item }} owner=root group=postgres mode=0640
|
||||||
with_items:
|
with_items:
|
||||||
- pg_hba.conf
|
- pg_hba.conf
|
||||||
- postgresql.conf
|
- postgresql.conf
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_hba', 'pg_conf' ]
|
tags: [ 'postgresql', 'postgres', 'pg_hba', 'pg_conf' ]
|
||||||
|
|
||||||
- name: Restart the postgresql server after changing parameters that need a restart
|
- name: postgresql-config | Restart the postgresql server after changing parameters that need a restart
|
||||||
service: name=postgresql state=restarted
|
service: name=postgresql state=restarted
|
||||||
when:
|
when:
|
||||||
- restart_postgresql is defined and restart_postgresql is changed
|
- restart_postgresql is defined and restart_postgresql is changed
|
||||||
|
@ -115,40 +131,40 @@
|
||||||
|
|
||||||
when: ansible_distribution_file_variety == "Debian"
|
when: ansible_distribution_file_variety == "Debian"
|
||||||
|
|
||||||
- name: Data directory for EL
|
- name: postgresql-config | Data directory for EL
|
||||||
block:
|
block:
|
||||||
- name: Check if the new postgresql data directory exists
|
- name: Check if the new postgresql data directory exists
|
||||||
stat: path={{ psql_el_data_dir }}
|
stat: path={{ psql_el_data_dir }}
|
||||||
register: postgresql_data_dir
|
register: postgresql_data_dir
|
||||||
|
|
||||||
- name: Stop the postgresql service while reconfiguring the data directory
|
- name: postgresql-config | Stop the postgresql service while reconfiguring the data directory
|
||||||
service: name='postgresql-{{ psql_version }}' state=stopped
|
service: name='postgresql-{{ psql_version }}' state=stopped
|
||||||
when: postgresql_data_dir.stat.isdir is not defined
|
when: postgresql_data_dir.stat.isdir is not defined
|
||||||
|
|
||||||
- name: Create the postgresql data directory if it is not in the default place
|
- name: postgresql-config | Create the postgresql data directory if it is not in the default place
|
||||||
file: dest={{ psql_el_data_dir }} owner=postgres group=postgres mode=700 recurse=yes state=directory
|
file: dest={{ psql_el_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
|
- name: postgresql-config | Set the postgresql data dir if it is different from the default
|
||||||
become: True
|
become: true
|
||||||
become_user: postgres
|
become_user: postgres
|
||||||
action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key=data_directory value="'{{ psql_el_data_dir }}'"
|
action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key=data_directory value="'{{ psql_el_data_dir }}'"
|
||||||
|
|
||||||
- name: Copy the postgresql data directory into the new place
|
- name: postgresql-config | Copy the postgresql data directory into the new place
|
||||||
shell: '[ "/var/lib/pgsql/{{ psql_version }}/data" != "{{ psql_el_data_dir }}" ] && cp -a /var/lib/pgsql/{{ psql_version }}/data/* {{ psql_el_data_dir }}'
|
shell: '[ "/var/lib/pgsql/{{ psql_version }}/data" != "{{ psql_el_data_dir }}" ] && cp -a /var/lib/pgsql/{{ psql_version }}/data/* {{ psql_el_data_dir }}'
|
||||||
args:
|
args:
|
||||||
creates: '{{ psql_el_data_dir }}/base'
|
creates: '{{ psql_el_data_dir }}/base'
|
||||||
when: postgresql_data_dir.stat.isdir is not defined
|
when: postgresql_data_dir.stat.isdir is not defined
|
||||||
|
|
||||||
- name: Fix the SELinux context for the new data directory
|
- name: postgresql-config | Fix the SELinux context for the new data directory
|
||||||
sefcontext:
|
sefcontext:
|
||||||
target: '{{ psql_el_base_dir }}(/.*)?'
|
target: '{{ psql_el_base_dir }}(/.*)?'
|
||||||
setype: postgresql_db_t
|
setype: postgresql_db_t
|
||||||
state: present
|
state: present
|
||||||
|
|
||||||
- name: Restore the SELinux context
|
- name: postgresql-config | Restore the SELinux context
|
||||||
command: restorecon -vR {{ psql_el_base_dir }}
|
command: restorecon -vR {{ psql_el_base_dir }}
|
||||||
|
|
||||||
- name: Start the postgresql service that will use the new data directory
|
- name: postgresql-config | Start the postgresql service that will use the new data directory
|
||||||
service: name='postgresql-{{ psql_version }}' state=started
|
service: name='postgresql-{{ psql_version }}' state=started
|
||||||
when: postgresql_data_dir.stat.isdir is not defined
|
when: postgresql_data_dir.stat.isdir is not defined
|
||||||
|
|
||||||
|
@ -157,21 +173,21 @@
|
||||||
- ansible_distribution_file_variety == "RedHat"
|
- ansible_distribution_file_variety == "RedHat"
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
||||||
|
|
||||||
- name: Configuration of EL systems
|
- name: postgresql-config | Configuration of EL systems
|
||||||
block:
|
block:
|
||||||
- name: Create the postgresql log directory
|
- name: postgresql-config | Create the postgresql log directory
|
||||||
file: dest={{ psql_log_dir }} state=directory owner=postgres group=postgres mode='0750'
|
file: dest={{ psql_log_dir }} state=directory owner=postgres group=postgres mode='0750'
|
||||||
|
|
||||||
- name: Fix the SELinux context for the postgresql log directory
|
- name: postgresql-config | Fix the SELinux context for the postgresql log directory
|
||||||
sefcontext:
|
sefcontext:
|
||||||
target: '{{ psql_log_dir }}(/.*)?'
|
target: '{{ psql_log_dir }}(/.*)?'
|
||||||
setype: postgresql_db_t
|
setype: postgresql_db_t
|
||||||
state: present
|
state: present
|
||||||
|
|
||||||
- name: Fix the SELinux context for the postgresql log directory
|
- name: postgresql-config | Fix the SELinux context for the postgresql log directory
|
||||||
command: restorecon -vR {{ psql_log_dir }}
|
command: restorecon -vR {{ psql_log_dir }}
|
||||||
|
|
||||||
- name: Set some postgresql configuration parameters that require a db restart
|
- name: postgresql-config | Set some postgresql configuration parameters that require a db restart
|
||||||
become: True
|
become: True
|
||||||
become_user: postgres
|
become_user: postgres
|
||||||
action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key={{ item.name }} value="{{ item.value }}"
|
action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key={{ item.name }} value="{{ item.value }}"
|
||||||
|
@ -180,7 +196,7 @@
|
||||||
notify: Restart postgresql
|
notify: Restart postgresql
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
||||||
|
|
||||||
- name: Set the postgresql logging configuration parameters
|
- name: postgresql-config | Set the postgresql logging configuration parameters
|
||||||
become: True
|
become: True
|
||||||
become_user: postgres
|
become_user: postgres
|
||||||
action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key={{ item.name }} value="{{ item.value }}"
|
action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key={{ item.name }} value="{{ item.value }}"
|
||||||
|
@ -189,7 +205,7 @@
|
||||||
notify: Reload postgresql
|
notify: Reload postgresql
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf', 'pg_conf_log' ]
|
tags: [ 'postgresql', 'postgres', 'pg_conf', 'pg_conf_log' ]
|
||||||
|
|
||||||
- name: Set the postgresql autovacuum configuration parameters
|
- name: postgresql-config | Set the postgresql autovacuum configuration parameters
|
||||||
become: True
|
become: True
|
||||||
become_user: postgres
|
become_user: postgres
|
||||||
action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key={{ item.name }} value="{{ item.value }}"
|
action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key={{ item.name }} value="{{ item.value }}"
|
||||||
|
@ -198,52 +214,52 @@
|
||||||
notify: Reload postgresql
|
notify: Reload postgresql
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf', 'pg_conf_autovacuum' ]
|
tags: [ 'postgresql', 'postgres', 'pg_conf', 'pg_conf_autovacuum' ]
|
||||||
|
|
||||||
- name: Set the postgresql listen port
|
- name: postgresql-config | Set the postgresql listen port
|
||||||
action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key=port value="{{ psql_db_port }}"
|
action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key=port value="{{ psql_db_port }}"
|
||||||
register: restart_postgresql
|
register: restart_postgresql
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
||||||
|
|
||||||
- name: We want postgres listen on the public IP
|
- name: postgresql-config | We want postgres listen on the public IP
|
||||||
action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key=listen_addresses value="'*'"
|
action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key=listen_addresses value="'*'"
|
||||||
register: restart_postgresql
|
register: restart_postgresql
|
||||||
when:
|
when:
|
||||||
- psql_listen_on_ext_int
|
- psql_listen_on_ext_int
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
||||||
|
|
||||||
- name: If postgresql is only accessed from localhost make it listen only on the localhost interface
|
- name: postgresql-config | If postgresql is only accessed from localhost make it listen only on the localhost interface
|
||||||
action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key=listen_addresses value="'localhost'"
|
action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key=listen_addresses value="'localhost'"
|
||||||
register: restart_postgresql
|
register: restart_postgresql
|
||||||
when:
|
when:
|
||||||
- not psql_listen_on_ext_int
|
- not psql_listen_on_ext_int
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
||||||
|
|
||||||
- name: Log the connections
|
- name: postgresql-config | Log the connections
|
||||||
action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key=log_connections value="on"
|
action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key=log_connections value="on"
|
||||||
register: restart_postgresql
|
register: restart_postgresql
|
||||||
when: psql_db_data is defined
|
when: psql_db_data is defined
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
||||||
|
|
||||||
- name: Log the disconnections
|
- name: postgresql-config | Log the disconnections
|
||||||
action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key=log_disconnections value="on"
|
action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key=log_disconnections value="on"
|
||||||
register: restart_postgresql
|
register: restart_postgresql
|
||||||
when: psql_db_data is defined
|
when: psql_db_data is defined
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
||||||
|
|
||||||
- name: Log the hostnames
|
- name: postgresql-config | Log the hostnames
|
||||||
action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key=log_hostname value="on"
|
action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key=log_hostname value="on"
|
||||||
register: restart_postgresql
|
register: restart_postgresql
|
||||||
when:
|
when:
|
||||||
- psql_listen_on_ext_int
|
- psql_listen_on_ext_int
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
tags: [ 'postgresql', 'postgres', 'pg_conf' ]
|
||||||
|
|
||||||
- name: Set the correct permissions to the postgresql files
|
- name: postgresql-config | Set the correct permissions to the postgresql files
|
||||||
file: dest={{ psql_el_conf_dir }}/{{ item }} owner=root group=postgres mode=0640
|
file: dest={{ psql_el_conf_dir }}/{{ item }} owner=root group=postgres mode=0640
|
||||||
with_items:
|
with_items:
|
||||||
- pg_hba.conf
|
- pg_hba.conf
|
||||||
- postgresql.conf
|
- postgresql.conf
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_hba', 'pg_conf' ]
|
tags: [ 'postgresql', 'postgres', 'pg_hba', 'pg_conf' ]
|
||||||
|
|
||||||
- name: Restart the postgresql server after changing parameters that need a restart
|
- name: postgresql-config | Restart the postgresql server after changing parameters that need a restart
|
||||||
service: name='postgresql-{{ psql_version }}' state=restarted
|
service: name='postgresql-{{ psql_version }}' state=restarted
|
||||||
when:
|
when:
|
||||||
- restart_postgresql is defined and restart_postgresql is changed
|
- restart_postgresql is defined and restart_postgresql is changed
|
||||||
|
|
|
@ -1,173 +1,178 @@
|
||||||
---
|
---
|
||||||
- name: Configuration of the streaming replication
|
- name: postgresql-streaming-replication | Configuration of the streaming replication
|
||||||
|
become: true
|
||||||
|
become_user: postgres
|
||||||
|
tags: ['postgresql', 'postgres', 'pg_conf', 'postgresql_replication']
|
||||||
block:
|
block:
|
||||||
- name: Create the replication user
|
- name: postgresql-streaming-replication | Create the replication user
|
||||||
postgresql_user:
|
community.postgresql.postgresql_user:
|
||||||
name: '{{ psql_streaming_replication_user }}'
|
name: '{{ psql_streaming_replication_user }}'
|
||||||
role_attr_flags: "REPLICATION"
|
role_attr_flags: "REPLICATION"
|
||||||
password: '{{ psql_streaming_replication_pwd }}'
|
password: '{{ psql_streaming_replication_pwd }}'
|
||||||
encrypted: yes
|
encrypted: true
|
||||||
state: present
|
state: present
|
||||||
|
|
||||||
- name: Setup the streaming replication on the primary
|
- name: postgresql-streaming-replication | Setup the streaming replication on the primary
|
||||||
postgresql_set:
|
community.postgresql.postgresql_set:
|
||||||
name: '{{ item.name }}'
|
name: '{{ item.name }}'
|
||||||
value: "{{ item.value }}"
|
value: "{{ item.value }}"
|
||||||
loop: '{{ psql_streaming_replication_config }}'
|
loop: '{{ psql_streaming_replication_config }}'
|
||||||
when: postgresql_streaming_replication_primary_node == ansible_fqdn
|
when: postgresql_streaming_replication_primary_node == ansible_fqdn
|
||||||
notify: Restart postgresql
|
notify: Restart postgresql
|
||||||
|
|
||||||
- meta: flush_handlers
|
- name: postgresql-streaming-replication | Flush flush_handlers restart PostgreSQL
|
||||||
|
ansible.builtin.meta: flush_handlers
|
||||||
become: True
|
|
||||||
become_user: postgres
|
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf', 'postgresql_replication' ]
|
|
||||||
|
|
||||||
- name: Configure the streaming replication user on deb systems
|
|
||||||
block:
|
|
||||||
- name: Configure the replication user permissions on deb
|
|
||||||
postgresql_pg_hba:
|
|
||||||
dest: '{{ psql_conf_dir }}/pg_hba.conf'
|
|
||||||
contype: host
|
|
||||||
users: '{{ psql_streaming_replication_user }}'
|
|
||||||
address: '{{ item }}'
|
|
||||||
databases: 'replication'
|
|
||||||
#method: 'scram-sha-256'
|
|
||||||
method: 'md5'
|
|
||||||
state: present
|
|
||||||
loop: '{{ psql_streaming_replication_hosts }}'
|
|
||||||
notify: Reload postgresql
|
|
||||||
when: not psql_enable_ssl
|
|
||||||
|
|
||||||
- name: Configure the replication user permissions on deb
|
|
||||||
postgresql_pg_hba:
|
|
||||||
dest: '{{ psql_conf_dir }}/pg_hba.conf'
|
|
||||||
contype: hostssl
|
|
||||||
users: '{{ psql_streaming_replication_user }}'
|
|
||||||
address: '{{ item }}'
|
|
||||||
databases: 'replication'
|
|
||||||
#method: 'scram-sha-256'
|
|
||||||
method: 'md5'
|
|
||||||
state: present
|
|
||||||
loop: '{{ psql_streaming_replication_hosts }}'
|
|
||||||
notify: Reload postgresql
|
|
||||||
when: psql_enable_ssl
|
|
||||||
|
|
||||||
- meta: flush_handlers
|
|
||||||
|
|
||||||
|
- name: postgresql-streaming-replication | Configure the streaming replication user on deb systems
|
||||||
when: ansible_distribution_file_variety == "Debian"
|
when: ansible_distribution_file_variety == "Debian"
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf', 'pg_hba', 'postgresql_replication' ]
|
tags: ['postgresql', 'postgres', 'pg_conf', 'pg_hba', 'postgresql_replication']
|
||||||
|
|
||||||
- name: Configure the streaming replication user on EL
|
|
||||||
block:
|
block:
|
||||||
- name: Configure the replication user permissions on EL
|
- name: postgresql-streaming-replication | Configure the replication user permissions on deb
|
||||||
postgresql_pg_hba:
|
community.postgresql.postgresql_pg_hba:
|
||||||
dest: '{{ psql_el_conf_dir }}/pg_hba.conf'
|
dest: '{{ psql_conf_dir }}/pg_hba.conf'
|
||||||
contype: host
|
contype: host
|
||||||
users: '{{ psql_streaming_replication_user }}'
|
users: '{{ psql_streaming_replication_user }}'
|
||||||
address: '{{ item }}'
|
address: '{{ item }}'
|
||||||
databases: 'replication'
|
databases: 'replication'
|
||||||
#method: 'scram-sha-256'
|
# method: 'scram-sha-256'
|
||||||
method: 'md5'
|
method: 'md5'
|
||||||
state: present
|
state: present
|
||||||
loop: '{{ psql_streaming_replication_hosts }}'
|
loop: '{{ psql_streaming_replication_hosts }}'
|
||||||
notify: Reload postgresql
|
notify: Reload postgresql
|
||||||
when: not psql_enable_ssl
|
when: not psql_enable_ssl
|
||||||
|
|
||||||
- name: Configure the replication user permissions on EL
|
- name: postgresql-streaming-replication | Configure the replication user permissions on deb
|
||||||
postgresql_pg_hba:
|
community.postgresql.postgresql_pg_hba:
|
||||||
dest: '{{ psql_el_conf_dir }}/pg_hba.conf'
|
dest: '{{ psql_conf_dir }}/pg_hba.conf'
|
||||||
contype: hostssl
|
contype: hostssl
|
||||||
users: '{{ psql_streaming_replication_user }}'
|
users: '{{ psql_streaming_replication_user }}'
|
||||||
address: '{{ item }}'
|
address: '{{ item }}'
|
||||||
databases: 'replication'
|
databases: 'replication'
|
||||||
#method: 'scram-sha-256'
|
# method: 'scram-sha-256'
|
||||||
method: 'md5'
|
method: 'md5'
|
||||||
state: present
|
state: present
|
||||||
loop: '{{ psql_streaming_replication_hosts }}'
|
loop: '{{ psql_streaming_replication_hosts }}'
|
||||||
notify: Reload postgresql
|
notify: Reload postgresql
|
||||||
when: psql_enable_ssl
|
when: psql_enable_ssl
|
||||||
|
|
||||||
- meta: flush_handlers
|
- name: postgresql-streaming-replication | Flush flush_handlers reload PostgreSQL
|
||||||
|
ansible.builtin.meta: flush_handlers
|
||||||
|
|
||||||
|
- name: postgresql-streaming-replication | Configure the streaming replication user on EL
|
||||||
when: ansible_distribution_file_variety == "RedHat"
|
when: ansible_distribution_file_variety == "RedHat"
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf', 'pg_hba', 'postgresql_replication' ]
|
tags: ['postgresql', 'postgres', 'pg_conf', 'pg_hba', 'postgresql_replication']
|
||||||
|
|
||||||
- name: Manage the replica initialization
|
|
||||||
block:
|
block:
|
||||||
- name: Set some paths when it is a deb based system
|
- name: postgresql-streaming-replication | Configure the replication user permissions on EL
|
||||||
set_fact:
|
community.postgresql.postgresql_pg_hba:
|
||||||
|
dest: '{{ psql_el_conf_dir }}/pg_hba.conf'
|
||||||
|
contype: host
|
||||||
|
users: '{{ psql_streaming_replication_user }}'
|
||||||
|
address: '{{ item }}'
|
||||||
|
databases: 'replication'
|
||||||
|
# method: 'scram-sha-256'
|
||||||
|
method: 'md5'
|
||||||
|
state: present
|
||||||
|
loop: '{{ psql_streaming_replication_hosts }}'
|
||||||
|
notify: Reload postgresql
|
||||||
|
when: not psql_enable_ssl
|
||||||
|
|
||||||
|
- name: postgresql-streaming-replication | Configure the replication user permissions on EL
|
||||||
|
community.postgresql.postgresql_pg_hba:
|
||||||
|
dest: '{{ psql_el_conf_dir }}/pg_hba.conf'
|
||||||
|
contype: hostssl
|
||||||
|
users: '{{ psql_streaming_replication_user }}'
|
||||||
|
address: '{{ item }}'
|
||||||
|
databases: 'replication'
|
||||||
|
# method: 'scram-sha-256'
|
||||||
|
method: 'md5'
|
||||||
|
state: present
|
||||||
|
loop: '{{ psql_streaming_replication_hosts }}'
|
||||||
|
notify: Reload postgresql
|
||||||
|
when: psql_enable_ssl
|
||||||
|
|
||||||
|
- name: postgresql-streaming-replication | Flush flush_handlers reload PostgreSQL
|
||||||
|
ansible.builtin.meta: flush_handlers
|
||||||
|
|
||||||
|
- name: postgresql-streaming-replication | Manage the replica initialization
|
||||||
|
when: postgresql_streaming_replication_primary_node != ansible_fqdn
|
||||||
|
tags: ['postgresql', 'postgres', 'pg_conf', 'postgresql_replication']
|
||||||
|
block:
|
||||||
|
- name: postgresql-streaming-replication | Set some paths when it is a deb based system
|
||||||
|
ansible.builtin.set_fact:
|
||||||
postgresql_user_home: '/var/lib/postgresql'
|
postgresql_user_home: '/var/lib/postgresql'
|
||||||
postgresql_active_data_dir: '{{ psql_data_dir }}'
|
postgresql_active_data_dir: '{{ psql_data_dir }}'
|
||||||
when: ansible_distribution_file_variety == "Debian"
|
when: ansible_distribution_file_variety == "Debian"
|
||||||
|
|
||||||
- name: Set some paths it is a EL based system
|
- name: postgresql-streaming-replication | Set some paths it is a EL based system
|
||||||
set_fact:
|
ansible.builtin.set_fact:
|
||||||
postgresql_user_home: '/var/lib/pgsql'
|
postgresql_user_home: '/var/lib/pgsql'
|
||||||
postgresql_active_data_dir: '{{ psql_el_data_dir }}'
|
postgresql_active_data_dir: '{{ psql_el_data_dir }}'
|
||||||
when: ansible_distribution_file_variety == "RedHat"
|
when: ansible_distribution_file_variety == "RedHat"
|
||||||
|
|
||||||
- name: Create the .pgpass file inside the postgresql home {{ postgresql_user_home }}
|
- name: postgresql-streaming-replication | Create the .pgpass file inside the postgresql home {{ postgresql_user_home }}
|
||||||
become: True
|
become: true
|
||||||
become_user: postgres
|
become_user: postgres
|
||||||
copy:
|
ansible.builtin.template:
|
||||||
content: "{{ postgresql_streaming_replication_primary_node }}:{{ psql_db_port }}:replication:{{ psql_streaming_replication_user }}:{{ psql_streaming_replication_pwd }}"
|
src: replica_pgpass.j2
|
||||||
dest: '{{ postgresql_user_home }}/.pgpass'
|
dest: '{{ postgresql_user_home }}/.pgpass'
|
||||||
mode: '0400'
|
mode: '0400'
|
||||||
|
|
||||||
- name: Check if a replica is already enabled
|
- name: postgresql-streaming-replication | Check if a replica is already enabled
|
||||||
stat:
|
ansible.builtin.stat:
|
||||||
path: '{{ postgresql_active_data_dir }}/standby.signal'
|
path: '{{ postgresql_active_data_dir }}/standby.signal'
|
||||||
register: standby_signal_file
|
register: standby_signal_file
|
||||||
|
|
||||||
- debug:
|
- name: postgresql-streaming-replication | Prnt the replica file
|
||||||
|
ansible.builtin.debug:
|
||||||
msg: 'Replica file: {{ postgresql_active_data_dir }}/standby.signal'
|
msg: 'Replica file: {{ postgresql_active_data_dir }}/standby.signal'
|
||||||
|
|
||||||
- name: Stop the postgresql service on deb systems
|
- name: postgresql-streaming-replication | Stop the postgresql service on deb systems
|
||||||
service:
|
ansible.builtin.service:
|
||||||
name: postgresql
|
name: postgresql
|
||||||
state: stopped
|
state: stopped
|
||||||
when:
|
when:
|
||||||
- ansible_distribution_file_variety == "Debian"
|
- ansible_distribution_file_variety == "Debian"
|
||||||
- not standby_signal_file.stat.exists
|
- not standby_signal_file.stat.exists
|
||||||
|
|
||||||
- name: Stop the postgresql service on EL systems
|
- name: postgresql-streaming-replication | Stop the postgresql service on EL systems
|
||||||
service:
|
ansible.builtin.service:
|
||||||
name: 'postgresql-{{ psql_version }}'
|
name: 'postgresql-{{ psql_version }}'
|
||||||
state: stopped
|
state: stopped
|
||||||
when:
|
when:
|
||||||
- ansible_distribution_file_variety == "RedHat"
|
- ansible_distribution_file_variety == "RedHat"
|
||||||
- not standby_signal_file.stat.exists
|
- not standby_signal_file.stat.exists
|
||||||
|
|
||||||
- name: Remove the data directory contents {{ postgresql_active_data_dir }}
|
- name: postgresql-streaming-replication | Remove the data directory contents {{ postgresql_active_data_dir }}
|
||||||
become: True
|
become: true
|
||||||
become_user: postgres
|
become_user: postgres
|
||||||
file:
|
ansible.builtin.file:
|
||||||
dest: '{{ postgresql_active_data_dir }}'
|
dest: '{{ postgresql_active_data_dir }}'
|
||||||
state: absent
|
state: absent
|
||||||
when: not standby_signal_file.stat.exists
|
when: not standby_signal_file.stat.exists
|
||||||
|
|
||||||
- name: Run the pg_basebackup command that starts the replica
|
- name: postgresql-streaming-replication | Run the pg_basebackup command that starts the replica
|
||||||
become: True
|
become: true
|
||||||
become_user: postgres
|
become_user: postgres
|
||||||
shell: /usr/bin/pg_basebackup -h {{ postgresql_streaming_replication_primary_node }} -p {{ psql_db_port }} -U {{ psql_streaming_replication_user }} -D {{ postgresql_active_data_dir }} -Fp -R -Xs -P -w
|
ansible.builtin.shell: >
|
||||||
|
/usr/bin/pg_basebackup -h {{ postgresql_streaming_replication_primary_node }}
|
||||||
|
-p {{ psql_db_port }} -U {{ psql_streaming_replication_user }}
|
||||||
|
-D {{ postgresql_active_data_dir }} -Fp -R -Xs -P -w
|
||||||
|
args:
|
||||||
|
creates: '{{ postgresql_active_data_dir }}'
|
||||||
when: not standby_signal_file.stat.exists
|
when: not standby_signal_file.stat.exists
|
||||||
|
|
||||||
- name: Start the postgresql service
|
- name: postgresql-streaming-replication | Start the postgresql service
|
||||||
service:
|
ansible.builtin.service:
|
||||||
name: postgresql
|
name: postgresql
|
||||||
state: started
|
state: started
|
||||||
when:
|
when:
|
||||||
- ansible_distribution_file_variety == "Debian"
|
- ansible_distribution_file_variety == "Debian"
|
||||||
- not standby_signal_file.stat.exists
|
- not standby_signal_file.stat.exists
|
||||||
|
|
||||||
- name: Start the postgresql service on EL systems
|
- name: postgresql-streaming-replication | Start the postgresql service on EL systems
|
||||||
service:
|
ansible.builtin.service:
|
||||||
name: 'postgresql-{{ psql_version }}'
|
name: 'postgresql-{{ psql_version }}'
|
||||||
state: started
|
state: started
|
||||||
when:
|
when:
|
||||||
- ansible_distribution_file_variety == "RedHat"
|
- ansible_distribution_file_variety == "RedHat"
|
||||||
- not standby_signal_file.stat.exists
|
- not standby_signal_file.stat.exists
|
||||||
|
|
||||||
when: postgresql_streaming_replication_primary_node != ansible_fqdn
|
|
||||||
tags: [ 'postgresql', 'postgres', 'pg_conf', 'postgresql_replication' ]
|
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
{{ postgresql_streaming_replication_primary_node }}:{{ psql_db_port }}:replication:{{ psql_streaming_replication_user }}:{{ psql_streaming_replication_pwd }}
|
Loading…
Reference in New Issue