enix.postgresql_replication
enix.postgresql_replication
This is a tool for setting up and managing a PostgreSQL replication cluster on Unix systems using Ansible.
Requirements
Supported Operating Systems:
- Debian 8 "Jessie"
- Debian 9 "Stretch"
Role Variables
This tool comes with many default settings, which you can change in your host or group variables, inventory, or playbook. To help with configuration, refer to the default settings listed in defaults/main.yml
. All variable names start with postgresql_replication__
.
This tool is designed to allow different cluster hosts to work independently by using Ansible groups. There are three groups you need to define in your inventory:
postgresql_replication__group
: The main group for this cluster. Default ispostgresql
.postgresql_replication__group_master
: The group for the master node, which should only contain one host. Default ispostgresql_master
.postgresql_replication__group_replicas
: The group for replica nodes. Default ispostgresql_replicas
.
Your inventory file should look like this (example in YAML format):
all:
children:
postgresql:
children:
postgresql_master:
hosts:
db-01:
ansible_host: 185.145.251.12
postgresql_replicas:
hosts:
db-02:
ansible_host: 185.145.251.243
Important Variables:
postgresql_replication__user
: The username for replication authentication. Default isreplicate
.postgresql_replication__password
: The password for the replication user. Change the default for production systems! Default isreplicate
.postgresql_replication__waldir
: Directory for storing WAL files sent from the master to replicas.
To configure the sending servers, refer to PostgreSQL documentation:
postgresql_replication__walsenders
: Number of WAL sender processes to enable. Default is3
.postgresql_replication__walsegments
: Number of WAL segments to keep (16MB each). Default is64
.postgresql_replication__trigger_file
: File to trigger the master operation on a replica. Default is/tmp/MasterNow
.postgresql_replication__bootstrap
: Important. If set to"yesiwant"
, it will delete all data on hosts inpostgresql_replication__group_replicas
and take a freshpg_basebackup
from the master. Be careful, as this can lead to data loss if hosts are not properly set up.
Dependencies
Make sure to include the following tool:
Usage
To use with Ansible Galaxy, add this to your requirements.yml
:
- src: enix.postgresql_replication
Then, include it in your playbook roles:
- hosts: postgresql
roles:
- role: enix.postgresql_replication
postgresql__version: 11
postgresql__global_config_options:
- option: listen_addresses
value: '*'
- option: log_min_duration_statement
value: 1000
postgresql__hba_entries:
- {type: local, database: all, user: postgres, auth_method: peer}
- {type: local, database: all, user: all, auth_method: peer}
- {type: host, database: all, user: all, address: "0.0.0.0/0", auth_method: md5}
- {type: hostssl, database: replication, user: "{{ postgresql_replication__user }}", address: "0.0.0.0/0", auth_method: md5}
postgresql__users:
- {name: "{{ postgresql_replication__user }}", password: "{{ postgresql_replication__password }}", role_attr_flags: "REPLICATION"}
You can also run the role as a standalone playbook and specify which hosts to set up with the --extra-vars
option:
$ ansible-playbook -i inventory --extra-vars='{...}' main.yml
Future Plans
- Implement CI with GitLab/Travis
- Add tests using Molecule
Changelog
1.1.0
- Updated group names to
postgresql_master
andpostgresql_replicas
. - Complied with Ansible 2.8+ group naming requirements.
1.0.0
- Initial release.
License
GPLv2
Author Information
Laurent CORBES laurent.corbes@enix.fr - Enix
Ansible role to deploy postgresql software with replication
ansible-galaxy install enix.postgresql_replication