infra/home/ansible/setup-postgres-lxc.yml
2025-09-06 10:55:13 -05:00

175 lines
No EOL
6.2 KiB
YAML

---
- name: Setup PostgreSQL with PostGIS in LXC
hosts: proxmox[0]
become: true
vars:
ct_id: 300
postgres_version: 16
tasks:
- name: Check if container is running
command: pct status {{ ct_id }}
register: ct_status
failed_when: false
changed_when: false
- name: Start container if stopped
command: pct start {{ ct_id }}
when: ct_status.rc == 0 and 'stopped' in ct_status.stdout
- name: Wait for container to be ready
wait_for:
host: 10.0.19.5
port: 22
timeout: 60
delay: 5
when: ct_status.rc == 0
- name: Check if PostgreSQL is already installed
shell: pct exec {{ ct_id }} -- which psql || echo "not installed"
register: psql_check
changed_when: false
when: ct_status.rc == 0
- name: Update package list
shell: pct exec {{ ct_id }} -- apt-get update -y
when: ct_status.rc == 0 and "'not installed' in psql_check.stdout"
- name: Install PostgreSQL
shell: pct exec {{ ct_id }} -- bash -c "export DEBIAN_FRONTEND=noninteractive && apt-get install -y postgresql postgresql-contrib"
when: ct_status.rc == 0 and "'not installed' in psql_check.stdout"
- name: Start PostgreSQL service
shell: |
pct exec {{ ct_id }} -- bash -c "
systemctl enable postgresql
systemctl start postgresql
systemctl status postgresql
"
register: pg_status
when: ct_status.rc == 0
- name: Show PostgreSQL status
debug:
var: pg_status.stdout_lines
when: pg_status is defined
- name: Get PostgreSQL version
shell: pct exec {{ ct_id }} -- ls /etc/postgresql/
register: pg_dir
when: ct_status.rc == 0
- name: Configure PostgreSQL for network access
shell: |
pct exec {{ ct_id }} -- bash -c "
# Get actual PostgreSQL version
PG_VERSION=$(ls /etc/postgresql/ 2>/dev/null | head -1)
# If no PostgreSQL directory found, exit
if [ -z \"\$PG_VERSION\" ]; then
echo 'PostgreSQL not found'
exit 1
fi
# Configure postgresql.conf
sed -i \"s/#listen_addresses = 'localhost'/listen_addresses = '*'/g\" /etc/postgresql/\$PG_VERSION/main/postgresql.conf
# Configure pg_hba.conf properly
echo '# TYPE DATABASE USER ADDRESS METHOD' > /etc/postgresql/\$PG_VERSION/main/pg_hba.conf
echo 'local all postgres peer' >> /etc/postgresql/\$PG_VERSION/main/pg_hba.conf
echo 'local all all peer' >> /etc/postgresql/\$PG_VERSION/main/pg_hba.conf
echo 'host all all 127.0.0.1/32 scram-sha-256' >> /etc/postgresql/\$PG_VERSION/main/pg_hba.conf
echo 'host all all ::1/128 scram-sha-256' >> /etc/postgresql/\$PG_VERSION/main/pg_hba.conf
echo 'host all all 10.0.19.0/24 md5' >> /etc/postgresql/\$PG_VERSION/main/pg_hba.conf
echo 'host all all 10.0.16.0/22 md5' >> /etc/postgresql/\$PG_VERSION/main/pg_hba.conf
echo 'host all all 10.42.0.0/16 md5' >> /etc/postgresql/\$PG_VERSION/main/pg_hba.conf
# Restart PostgreSQL
systemctl restart postgresql
"
register: pg_config_result
- name: Verify pg_hba.conf was updated
shell: |
pct exec {{ ct_id }} -- bash -c "
PG_VERSION=$(ls /etc/postgresql/ 2>/dev/null | head -1)
if [ -n \"\$PG_VERSION\" ]; then
echo '=== PostgreSQL pg_hba.conf ==='
cat /etc/postgresql/\$PG_VERSION/main/pg_hba.conf | grep -E '10.0.19|10.42|10.0.16'
else
echo 'PostgreSQL not found'
fi
"
register: pg_hba_verify
- name: Display pg_hba.conf entries
debug:
var: pg_hba_verify.stdout_lines
when: pg_hba_verify is defined
- name: Install sudo if missing
shell: pct exec {{ ct_id }} -- apt-get install -y sudo
when: ct_status.rc == 0
- name: Create databases and users
shell: |
pct exec {{ ct_id }} -- su - postgres -c "psql << 'EOF'
-- Drop existing if needed and recreate
DROP DATABASE IF EXISTS forgejo;
DROP USER IF EXISTS forgejo;
CREATE USER forgejo WITH PASSWORD 'fj8K3n2Qp9Lx5mW7';
CREATE DATABASE forgejo OWNER forgejo;
\c forgejo
CREATE EXTENSION IF NOT EXISTS postgis;
GRANT ALL PRIVILEGES ON DATABASE forgejo TO forgejo;
-- Create other databases as needed
DROP DATABASE IF EXISTS apps;
DROP USER IF EXISTS appuser;
CREATE USER appuser WITH PASSWORD 'AppPass2024!';
CREATE DATABASE apps OWNER appuser;
\c apps
CREATE EXTENSION IF NOT EXISTS postgis;
GRANT ALL PRIVILEGES ON DATABASE apps TO appuser;
EOF"
when: ct_status.rc == 0
- name: Configure firewall
shell: |
pct exec {{ ct_id }} -- bash -c "
apt-get install -y ufw
ufw allow 22/tcp
ufw allow 5432/tcp
ufw --force enable
"
when: ct_status.rc == 0
- name: Wait for PostgreSQL to be ready
wait_for:
host: 10.0.19.5
port: 5432
timeout: 30
delay: 2
- name: Test PostgreSQL connectivity
shell: |
pct exec {{ ct_id }} -- sudo -u postgres psql -c "SELECT version();" || echo "PostgreSQL query failed"
register: pg_test
when: ct_status.rc == 0
- name: Display connection info
debug:
msg: |
PostgreSQL with PostGIS installed successfully!
Connection details:
- Host: 10.0.19.5
- Port: 5432
- Version: PostgreSQL {{ postgres_version }} with PostGIS 3
Databases:
- forgejo (user: forgejo, pass: fj8K3n2Qp9Lx5mW7)
- apps (user: appuser, pass: AppPass2024!)
From Kubernetes pods, connect using:
postgresql://forgejo:fj8K3n2Qp9Lx5mW7@10.0.19.5:5432/forgejo