157 lines
4.8 KiB
YAML
157 lines
4.8 KiB
YAML
---
|
|
- name: Configure PostgreSQL master on Ubuntu 24
|
|
hosts: master
|
|
become: yes
|
|
vars:
|
|
postgres_version: 16
|
|
replication_user: replicator
|
|
replication_password: "your_password"
|
|
tasks:
|
|
- name: Install software-properties-common
|
|
apt:
|
|
name: software-properties-common
|
|
state: present
|
|
update_cache: yes
|
|
|
|
- name: Add Universe repository on master
|
|
command: add-apt-repository universe -y
|
|
args:
|
|
creates: /etc/apt/sources.list.d/universe.list
|
|
register: add_universe_master
|
|
changed_when: add_universe_master.stdout != ""
|
|
|
|
- name: Update apt cache after adding Universe repository on master
|
|
apt:
|
|
update_cache: yes
|
|
when: add_universe_master is changed
|
|
|
|
- name: Install PostgreSQL 16 on master
|
|
apt:
|
|
name: "postgresql-{{ postgres_version }}"
|
|
state: present
|
|
|
|
- name: Install python3-psycopg2 for PostgreSQL modules on master
|
|
apt:
|
|
name: python3-psycopg2
|
|
state: present
|
|
|
|
- name: Ensure listen_addresses is removed (cleanup) in postgresql.conf
|
|
lineinfile:
|
|
path: "/etc/postgresql/{{ postgres_version }}/main/postgresql.conf"
|
|
regexp: '^(#\s*)?listen_addresses'
|
|
state: absent
|
|
notify: Restart PostgreSQL
|
|
|
|
- name: Set listen_addresses to '*' in postgresql.conf
|
|
blockinfile:
|
|
path: "/etc/postgresql/{{ postgres_version }}/main/postgresql.conf"
|
|
marker: "# {mark} ANSIBLE MANAGED LISTEN ADDRESSES"
|
|
block: |
|
|
listen_addresses = '*'
|
|
notify: Restart PostgreSQL
|
|
|
|
- name: Set wal_level to replica
|
|
lineinfile:
|
|
path: "/etc/postgresql/{{ postgres_version }}/main/postgresql.conf"
|
|
regexp: '^(#\s*)?wal_level'
|
|
line: "wal_level = replica"
|
|
notify: Restart PostgreSQL
|
|
|
|
- name: Set max_wal_senders to 3
|
|
lineinfile:
|
|
path: "/etc/postgresql/{{ postgres_version }}/main/postgresql.conf"
|
|
regexp: '^(#\s*)?max_wal_senders'
|
|
line: "max_wal_senders = 3"
|
|
notify: Restart PostgreSQL
|
|
|
|
- name: Allow replication connections in pg_hba.conf on master
|
|
lineinfile:
|
|
path: "/etc/postgresql/{{ postgres_version }}/main/pg_hba.conf"
|
|
line: "host replication {{ replication_user }} 0.0.0.0/0 md5"
|
|
create: yes
|
|
notify: Restart PostgreSQL
|
|
|
|
- name: Create replication user on master
|
|
postgresql_user:
|
|
name: "{{ replication_user }}"
|
|
password: "{{ replication_password }}"
|
|
role_attr_flags: "REPLICATION"
|
|
login_user: postgres
|
|
state: present
|
|
become_user: postgres
|
|
|
|
handlers:
|
|
- name: Restart PostgreSQL
|
|
service:
|
|
name: postgresql
|
|
state: restarted
|
|
|
|
- name: Configure PostgreSQL replica on Ubuntu 24
|
|
hosts: replica
|
|
become: yes
|
|
vars:
|
|
postgres_version: 16
|
|
replication_user: replicator
|
|
replication_password: "your_password"
|
|
master_ip: "{{ hostvars['master_host']['ansible_host'] | default('149.36.48.100') }}"
|
|
tasks:
|
|
- name: Install software-properties-common on replica
|
|
apt:
|
|
name: software-properties-common
|
|
state: present
|
|
update_cache: yes
|
|
|
|
- name: Add Universe repository on replica
|
|
command: add-apt-repository universe -y
|
|
args:
|
|
creates: /etc/apt/sources.list.d/universe.list
|
|
register: add_universe_replica
|
|
changed_when: add_universe_replica.stdout != ""
|
|
|
|
- name: Update apt cache after adding Universe repository on replica
|
|
apt:
|
|
update_cache: yes
|
|
when: add_universe_replica is changed
|
|
|
|
- name: Install PostgreSQL 16 on replica
|
|
apt:
|
|
name: "postgresql-{{ postgres_version }}"
|
|
state: present
|
|
|
|
- name: Stop PostgreSQL service on replica
|
|
service:
|
|
name: postgresql
|
|
state: stopped
|
|
|
|
- name: Remove old PostgreSQL data directory on replica
|
|
file:
|
|
path: "/var/lib/postgresql/{{ postgres_version }}/main"
|
|
state: absent
|
|
|
|
- name: Use pg_basebackup to clone master data
|
|
command: >
|
|
pg_basebackup -h {{ master_ip }}
|
|
-D /var/lib/postgresql/{{ postgres_version }}/main
|
|
-U {{ replication_user }} -v -P --wal-method=stream
|
|
become_user: postgres
|
|
environment:
|
|
PGPASSWORD: "{{ replication_password }}"
|
|
|
|
- name: Create standby.signal file (for PostgreSQL 12+)
|
|
file:
|
|
path: "/var/lib/postgresql/{{ postgres_version }}/main/standby.signal"
|
|
state: touch
|
|
|
|
- name: Set primary connection info for replica
|
|
lineinfile:
|
|
path: "/var/lib/postgresql/{{ postgres_version }}/main/postgresql.auto.conf"
|
|
line: "primary_conninfo = 'host={{ master_ip }} port=5432 user={{ replication_user }} password={{ replication_password }}'"
|
|
notify: Restart PostgreSQL
|
|
|
|
handlers:
|
|
- name: Restart PostgreSQL
|
|
service:
|
|
name: postgresql
|
|
state: started
|
|
|