[TOC]
postgres is multifunctional role to deploy PostgreSQL in single, replica, HA-clusters (patroni,stolon) mode.
Copyright (C) 2023 Mikhail Shurutov
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
This role requires python v3 because python v2 is out of live and ansible-core>=2.13.9. Role dependencies:
- mshurutov.common
Role has many variables. For details see defaults/main.yml
ANSIBLE_ROOT_DIR
is path for static content: roles,configs,etc, for example: /data/ansibleANSIBLE_ROOT_ROLE_DIR
is path inroles_path
config variable, for example: /data/ansible/roles Content of my ~/.ansible.cfg:
...
# additional paths to search for roles in, colon separated
#roles_path = /etc/ansible/roles
roles_path = /data/ansible/roles
...
user@host ~ $ cd $ANSIBLE_ROOT_ROLE_DIR
user@host roles $ git clone https://git.code.sf.net/p/postgres-ansible-role/code postgres
user@host ~ $ cd $ANSIBLE_ROOT_DIR
user@host ansible $ ansible-galaxy role install mshurutov.postgres -p roles
user@host ~ $ cd $ANSIBLE_ROOT_DIR
user@host ansible $ grep postgres requirements.yml
- name: mshurutov.postgres
user@host ansible $ ansible-galaxy role install -r requirements.yml -p roles
On every case you can set OS-depended variables:
postgres_{{ ansible_pkg_mgr }}_home_dir
is home directory of system user owner of instance;postgres_{{ ansible_pkg_mgr }}_conf_dir
is directory where config files are placed;postgres_{{ ansible_pkg_mgr }}_data_dir
is $PGDATA directory;postgres_{{ ansible_pkg_mgr }}_bin_dir
is directory where executable files is placed by package manager;postgres_{{ ansible_pkg_mgr }}_service_name
is systemd service name of postgres.
Where ansible_pkg_mgr
is one from apt
,dnf
,apt_rpm
(for Alt Linux), portage
.
You may need set postgres_major_version
only in this mode. Default value of this variable is 16
.
Role is installed by ansible galaxy (see above) in this example.
...
- hosts: pg_host
roles:
- role: mshurutov.postgres
tags: postgres
...
ansible-playbook postgres-playbook.yml -t postgres -l pg_host -D
For use this mode you must define follow variables: postgres_install_mode
should be set to "replica
"; postgres_master_host
, postgres_backup_connstring
, postgres_replica_init
need to be set to "from_master"
postgres_recovery_params
as shown below ({{ common_domain_name }}: 'example.com'
is set on group_vars/all
):
user@ansible_host pgdev $ cat host_vars/pg_replica.yml
# Common settings
common_short_hostname: "pg_replica"
common_full_hostname: "{{ common_short_hostname }}.{{ common_domain_name }}"
common_ip4_default: "192.168.1.12"
# postgres settings
postgres_install_mode: "replica"
postgres_master_host: "pg_master.{{ common_domain_name }}"
postgres_backup_connstring: "-h {{ postgres_master_host }} -U {{ postgres_repl_user }}"
postgres_replica_init: "from_master"
postgres_recovery_params:
- { name: "primary_conninfo", value: "'host={{ postgres_master_host }} user={{ postgres_repl_user }}'" }
- { name: "hot_standby", value: "on" }
user@ansible_host pgdev $
Role is installed by git (see above) in this example.
...
- hosts: pg_replica
roles:
- role: postgres
tags: postgres
...
ansible-playbook postgres-playbook.yml -t postgres -l pg_replica -D
mshurutov@hpbook02 pgdev $ cat group_vars/dev.yml
---
# Variables for group dev
...
postgres_install_mode: "patroni"
...
postgres_master_host: "srv01dev.{{ common_domain_name }}"
# Patroni settings
patroni_config_file: '{{ patroni_config_dir }}/config.yml'
patroni_scope_name: 'clusterdev'
patroni_log_params:
- name: "level"
value: "DEBUG"
- name: "dir"
value: "/var/log/postgresql"
patroni_config_file
is distro depened variable;
mshurutov@hpbook02 pgdev $ cat group_vars/dev.yml
---
# Variables for group dev
...
etcd_cluster_group: "dev"
...
postgres_cluster_dcs: "etcd"
...
mshurutov@hpbook02 pgdev $ cat group_vars/dev.yml
---
# Variables for group dev
...
# Consul settings
postgres_consul_hosts_group: "dev"
...
postgres_cluster_dcs: "consul"
...
mshurutov@hpbook02 pgdev $ cat group_vars/dev.yml
---
# Variables for group dev
...
postgres_install_mode: "stolon"
...
# Stolon settings
stolon_apt_repo: "deb [trusted=yes] http://srv01/repos/deb/ bullseye main"
stolon_daemons_common_options_group:
- key: "log_level"
value: "info"
comment: "# debug, info (default by developer), warn or error (default by me)"
- key: "store_backend"
value: "{{ stolon_store_backend }}"
comment: |
# store backend type (etcdv2/etcd, etcdv3, consul or kubernetes)
stolon_daemons_proxy_options_group:
- key: "port"
value: "7432"
comment: "# proxy listening port (default \"5432\")"
...
mshurutov@hpbook02 pgdev $ cat group_vars/dev.yml
---
# Variables for group dev
...
stolon_store_backend: "etcd"
...
mshurutov@hpbook02 pgdev $ cat group_vars/dev.yml
---
# Variables for group dev
...
stolon_store_backend: "consul"
...
...
- hosts: pg_dcs_hosts
roles:
- role: mshurutov.etcd
tags: pg_cluster,pg_dcs,etcd
...
- hosts: pg_hosts
roles:
- role: mshurutov.postgres
tags: pg_cluster,postgres
...
ansible-playbook postgres-playbook.yml -t pg_cluster -D
An optional section for the role authors to include contact information, or a website (HTML is not allowed).