97 lines
2.6 KiB
YAML
97 lines
2.6 KiB
YAML
---
|
|
- name: Configure PostgreSQL Primary
|
|
hosts: postgres_primary
|
|
become: true
|
|
tasks:
|
|
- name: Install PostgreSQL
|
|
apt:
|
|
name: postgresql
|
|
state: present
|
|
update_cache: yes
|
|
|
|
- name: Ensure PostgreSQL is running
|
|
service:
|
|
name: postgresql
|
|
state: started
|
|
enabled: yes
|
|
|
|
- name: Create test database
|
|
become: true
|
|
# Replace the direct become_user with a shell command that uses sudo
|
|
shell: |
|
|
sudo -u postgres psql -c "CREATE DATABASE testdb WITH OWNER = postgres ENCODING = 'UTF8' LC_COLLATE = 'en_US.UTF-8' LC_CTYPE = 'en_US.UTF-8' TEMPLATE = template0;"
|
|
# Only run this if the database doesn't already exist
|
|
register: db_creation
|
|
changed_when: db_creation.rc == 0
|
|
failed_when: db_creation.rc != 0 and "already exists" not in db_creation.stderr
|
|
# The following approach handles error gracefully if DB already exists
|
|
args:
|
|
executable: /bin/bash
|
|
|
|
# Rest of the playbook remains the same
|
|
- name: Configure PostgreSQL Replicas
|
|
hosts: postgres_replicas
|
|
become: true
|
|
tasks:
|
|
- name: Install PostgreSQL
|
|
apt:
|
|
name: postgresql
|
|
state: present
|
|
update_cache: yes
|
|
|
|
- name: Ensure PostgreSQL is running
|
|
service:
|
|
name: postgresql
|
|
state: started
|
|
enabled: yes
|
|
|
|
- name: Configure Load Balancer
|
|
hosts: postgres_lb
|
|
become: true
|
|
tasks:
|
|
- name: Install HAProxy
|
|
apt:
|
|
name: haproxy
|
|
state: present
|
|
update_cache: yes
|
|
|
|
- name: Create simple HAProxy configuration
|
|
copy:
|
|
dest: /etc/haproxy/haproxy.cfg
|
|
content: |
|
|
global
|
|
log /dev/log local0
|
|
log /dev/log local1 notice
|
|
chroot /var/lib/haproxy
|
|
stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
|
|
stats timeout 30s
|
|
user haproxy
|
|
group haproxy
|
|
daemon
|
|
|
|
defaults
|
|
log global
|
|
mode tcp
|
|
option tcplog
|
|
option dontlognull
|
|
timeout connect 5000
|
|
timeout client 50000
|
|
timeout server 50000
|
|
|
|
frontend postgresql
|
|
bind *:5432
|
|
mode tcp
|
|
default_backend postgresql_backends
|
|
|
|
backend postgresql_backends
|
|
mode tcp
|
|
balance roundrobin
|
|
server primary {{ hostvars[groups['postgres_primary'][0]]['ansible_host'] }}:5432 check
|
|
backup: yes
|
|
|
|
- name: Restart HAProxy
|
|
service:
|
|
name: haproxy
|
|
state: restarted
|
|
enabled: yes
|