216 lines
No EOL
7.8 KiB
YAML
216 lines
No EOL
7.8 KiB
YAML
---
|
|
- name: Create PostgreSQL 17 LXC Container with Debian 13 on lab02
|
|
hosts: lab02
|
|
become: true
|
|
vars:
|
|
ct_id: 300
|
|
ct_name: postgres
|
|
ct_template: "local:vztmpl/debian-13-standard_13.1-1_amd64.tar.zst"
|
|
ct_memory: 8192 # 8GB RAM
|
|
ct_disk_size: 512 # 512GB storage as requested
|
|
ct_cores: 4
|
|
ct_ip: "10.0.19.5/22"
|
|
ct_gateway: "10.0.19.254"
|
|
ct_storage: "ceph" # Use Ceph RBD storage
|
|
aprs_password: "{{ lookup('password', '/dev/null chars=ascii_letters,digits length=16') }}"
|
|
|
|
tasks:
|
|
- name: Check if container already exists
|
|
command: pct status {{ ct_id }}
|
|
register: ct_exists
|
|
failed_when: false
|
|
changed_when: false
|
|
|
|
- name: Stop and destroy existing container if it exists
|
|
shell: |
|
|
pct stop {{ ct_id }} || true
|
|
pct destroy {{ ct_id }} --purge || true
|
|
when: ct_exists.rc == 0
|
|
|
|
- name: List available templates
|
|
command: pveam list local
|
|
register: template_list
|
|
changed_when: false
|
|
|
|
- name: Show available templates
|
|
debug:
|
|
msg: "Available templates: {{ template_list.stdout_lines | select('match', '.*debian.*') | list }}"
|
|
|
|
- name: Create PostgreSQL LXC container
|
|
command: |
|
|
pct create {{ ct_id }} {{ ct_template }} \
|
|
--hostname {{ ct_name }} \
|
|
--memory {{ ct_memory }} \
|
|
--cores {{ ct_cores }} \
|
|
--net0 name=eth0,bridge=vmbr0,ip={{ ct_ip }},gw={{ ct_gateway }} \
|
|
--storage {{ ct_storage }} \
|
|
--rootfs {{ ct_storage }}:{{ ct_disk_size }} \
|
|
--unprivileged 1 \
|
|
--features nesting=1 \
|
|
--onboot 1 \
|
|
--startup order=10
|
|
|
|
- name: Start container
|
|
command: pct start {{ ct_id }}
|
|
|
|
- name: Wait for container to boot
|
|
pause:
|
|
seconds: 30
|
|
|
|
- name: Update container packages
|
|
shell: pct exec {{ ct_id }} -- apt update && pct exec {{ ct_id }} -- apt upgrade -y
|
|
|
|
- name: Install PostgreSQL 17 repository
|
|
shell: |
|
|
pct exec {{ ct_id }} -- bash -c "
|
|
apt-get install -y wget ca-certificates
|
|
sh -c 'echo \"deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main\" > /etc/apt/sources.list.d/pgdg.list'
|
|
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
|
|
apt-get update
|
|
"
|
|
|
|
- name: Install PostgreSQL 17 and PostGIS
|
|
shell: |
|
|
pct exec {{ ct_id }} -- bash -c "
|
|
export DEBIAN_FRONTEND=noninteractive
|
|
apt-get install -y postgresql-17 postgresql-contrib-17 postgresql-17-postgis-3 postgis
|
|
"
|
|
|
|
- name: Start and enable PostgreSQL
|
|
shell: |
|
|
pct exec {{ ct_id }} -- bash -c "
|
|
systemctl enable postgresql
|
|
systemctl start postgresql
|
|
"
|
|
|
|
- name: Configure PostgreSQL for network access
|
|
shell: |
|
|
pct exec {{ ct_id }} -- bash -c "
|
|
# Configure postgresql.conf
|
|
sed -i \"s/#listen_addresses = 'localhost'/listen_addresses = '*'/g\" /etc/postgresql/17/main/postgresql.conf
|
|
|
|
# Configure pg_hba.conf
|
|
echo '# TYPE DATABASE USER ADDRESS METHOD' > /etc/postgresql/17/main/pg_hba.conf
|
|
echo 'local all postgres peer' >> /etc/postgresql/17/main/pg_hba.conf
|
|
echo 'local all all peer' >> /etc/postgresql/17/main/pg_hba.conf
|
|
echo 'host all all 127.0.0.1/32 scram-sha-256' >> /etc/postgresql/17/main/pg_hba.conf
|
|
echo 'host all all ::1/128 scram-sha-256' >> /etc/postgresql/17/main/pg_hba.conf
|
|
echo 'host all all 10.0.19.0/24 scram-sha-256' >> /etc/postgresql/17/main/pg_hba.conf
|
|
echo 'host all all 10.0.16.0/22 scram-sha-256' >> /etc/postgresql/17/main/pg_hba.conf
|
|
echo 'host all all 10.42.0.0/16 scram-sha-256' >> /etc/postgresql/17/main/pg_hba.conf
|
|
echo 'host all all 10.43.0.0/16 scram-sha-256' >> /etc/postgresql/17/main/pg_hba.conf
|
|
|
|
# Restart PostgreSQL
|
|
systemctl restart postgresql
|
|
"
|
|
|
|
- name: Create APRS user and database
|
|
shell: |
|
|
pct exec {{ ct_id }} -- su - postgres -c "psql << 'EOF'
|
|
-- Create APRS user and database
|
|
CREATE USER aprs WITH PASSWORD '{{ aprs_password }}';
|
|
CREATE DATABASE aprs OWNER aprs;
|
|
\c aprs
|
|
CREATE EXTENSION IF NOT EXISTS postgis;
|
|
GRANT ALL PRIVILEGES ON DATABASE aprs TO aprs;
|
|
GRANT ALL ON SCHEMA public TO aprs;
|
|
ALTER DATABASE aprs SET search_path TO public, postgis;
|
|
EOF"
|
|
|
|
- name: Test APRS database connection
|
|
shell: |
|
|
pct exec {{ ct_id }} -- su - postgres -c "psql -d aprs -c 'SELECT version(); SELECT PostGIS_full_version();'"
|
|
register: db_test
|
|
|
|
- name: Save APRS password to file
|
|
copy:
|
|
content: "{{ aprs_password }}"
|
|
dest: "./aprs_db_password.txt"
|
|
mode: '0600'
|
|
delegate_to: localhost
|
|
|
|
- name: Display connection info
|
|
debug:
|
|
msg: |
|
|
PostgreSQL 17 with PostGIS installed successfully on Debian 13!
|
|
|
|
Container details:
|
|
- Container ID: {{ ct_id }}
|
|
- Host: lab02
|
|
- IP: 10.0.19.5
|
|
- Storage: {{ ct_disk_size }}GB on {{ ct_storage }}
|
|
|
|
Database details:
|
|
- PostgreSQL version: 17
|
|
- PostGIS: Installed
|
|
- Database: aprs
|
|
- User: aprs
|
|
- Password: {{ aprs_password }}
|
|
- Password saved to: ./aprs_db_password.txt
|
|
|
|
Connection string:
|
|
postgresql://aprs:{{ aprs_password }}@10.0.19.5:5432/aprs
|
|
|
|
Test output:
|
|
{{ db_test.stdout }}
|
|
|
|
- name: Create K8s secret for APRS database
|
|
hosts: home-k3s-control-1
|
|
become: true
|
|
vars:
|
|
aprs_password: "{{ hostvars['lab02']['aprs_password'] }}"
|
|
tasks:
|
|
- name: Create aprs namespace if it doesn't exist
|
|
command: |
|
|
/usr/local/bin/k3s kubectl create namespace aprs --dry-run=client -o yaml | /usr/local/bin/k3s kubectl apply -f -
|
|
environment:
|
|
KUBECONFIG: /etc/rancher/k3s/k3s.yaml
|
|
|
|
- name: Delete existing database secret if it exists
|
|
command: |
|
|
/usr/local/bin/k3s kubectl delete secret aprs-db-secret -n aprs --ignore-not-found=true
|
|
environment:
|
|
KUBECONFIG: /etc/rancher/k3s/k3s.yaml
|
|
|
|
- name: Create database secret
|
|
command: |
|
|
/usr/local/bin/k3s kubectl create secret generic aprs-db-secret \
|
|
--from-literal=DATABASE_URL="ecto://aprs:{{ aprs_password }}@10.0.19.5:5432/aprs" \
|
|
--from-literal=DB_HOST="10.0.19.5" \
|
|
--from-literal=DB_PORT="5432" \
|
|
--from-literal=DB_NAME="aprs" \
|
|
--from-literal=DB_USER="aprs" \
|
|
--from-literal=DB_PASSWORD="{{ aprs_password }}" \
|
|
-n aprs
|
|
environment:
|
|
KUBECONFIG: /etc/rancher/k3s/k3s.yaml
|
|
|
|
- name: Verify secret was created
|
|
command: |
|
|
/usr/local/bin/k3s kubectl get secret aprs-db-secret -n aprs -o yaml
|
|
register: secret_verify
|
|
environment:
|
|
KUBECONFIG: /etc/rancher/k3s/k3s.yaml
|
|
|
|
- name: Display secret creation result
|
|
debug:
|
|
msg: |
|
|
Kubernetes secret created successfully!
|
|
|
|
Secret name: aprs-db-secret
|
|
Namespace: aprs
|
|
|
|
To use in your deployment, add:
|
|
|
|
envFrom:
|
|
- secretRef:
|
|
name: aprs-db-secret
|
|
|
|
Or individual env vars:
|
|
|
|
env:
|
|
- name: DATABASE_URL
|
|
valueFrom:
|
|
secretKeyRef:
|
|
name: aprs-db-secret
|
|
key: DATABASE_URL |