-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgres-install.yaml
148 lines (123 loc) · 4.25 KB
/
postgres-install.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
---
- name: Check if docker-compose is installed
ansible.builtin.stat:
path: /usr/local/bin/docker-compose
register: docker_compose_stat
- name: Download docker-compose and make executable
ansible.builtin.get_url:
url: https://github.com/docker/compose/releases/download/v2.29.6/docker-compose-linux-x86_64
dest: /usr/local/bin/docker-compose
mode: '0755'
when: not docker_compose_stat.stat.exists
- name: Check if Docker network exists
ansible.builtin.shell: |
docker network ls --filter name=node_network --format "{{ '{{' }}.Name{{ '}}' }}"
register: docker_network_check
changed_when: false
- name: Create Docker network if it does not exist
ansible.builtin.shell: |
docker network create --driver bridge node_network
when: docker_network_check.stdout != "node_network"
- name: Create docker-compose.yml
ansible.builtin.copy:
dest: ./docker-compose.yml
mode: '0644'
content: |
version: '3.8'
services:
postgres:
build: ./postgres
container_name: obscuronode-postgres
environment:
POSTGRES_PASSWORD: pass
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- node_network
networks:
node_network:
external: true
volumes:
postgres_data:
- name: Stop and remove old Postgres container
ansible.builtin.shell: |
docker stop obscuronode-postgres || true && docker rm obscuronode-postgres || true
- name: Remove old PostgreSQL directory
ansible.builtin.file:
path: ./postgres
state: absent
- name: Create necessary directories for PostgreSQL setup
ansible.builtin.file:
path: "{{ item }}"
state: directory
mode: '0755'
loop:
- ./postgres/certs
- ./postgres/initdb
- name: Install openssl
ansible.builtin.package:
name:
- openssl
- libssl-dev
state: present
- name: Generate SSL private key
community.crypto.openssl_privatekey:
path: ./postgres/certs/server.key
size: 2048
- name: Generate SSL CSR
community.crypto.openssl_csr:
path: ./postgres/certs/server.csr
privatekey_path: ./postgres/certs/server.key
common_name: localhost
- name: Generate SSL certificate
community.crypto.x509_certificate:
path: ./postgres/certs/server.crt
csr_path: ./postgres/certs/server.csr
privatekey_path: ./postgres/certs/server.key
provider: selfsigned
selfsigned_notAfter: "99991231235959Z" # Set to a far future date
- name: Create custom postgresql.conf
ansible.builtin.copy:
dest: ./postgres/postgresql.conf
mode: '0644'
content: |
# Include the default PostgreSQL configuration
include = '/usr/share/postgresql/postgresql.conf.sample'
# SSL configuration
ssl = on
ssl_cert_file = '/var/lib/postgresql/server.crt'
ssl_key_file = '/var/lib/postgresql/server.key'
ssl_prefer_server_ciphers = on
- name: Create Dockerfile for PostgreSQL
ansible.builtin.copy:
dest: ./postgres/Dockerfile
mode: '0644'
content: |
FROM postgres:latest
COPY ./certs/server.crt /var/lib/postgresql/server.crt
COPY ./certs/server.key /var/lib/postgresql/server.key
COPY ./postgresql.conf /etc/postgresql/postgresql.conf
RUN chown postgres:postgres /var/lib/postgresql/server.crt /var/lib/postgresql/server.key \
&& chmod 600 /var/lib/postgresql/server.crt /var/lib/postgresql/server.key
CMD ["postgres", "-c", "config_file=/etc/postgresql/postgresql.conf"]
- name: Run Docker Compose with custom path
ansible.builtin.command: docker-compose up --build -d
become: true
- name: Set postgres_db_host based on docker container and port
ansible.builtin.set_fact:
postgres_db_host: "postgres://postgres:pass@obscuronode-postgres:5432/"
- name: Install psql
ansible.builtin.package:
name:
- postgresql-client
state: present
- name: Wait for Postgres to be ready
ansible.builtin.pause:
seconds: 10
- name: Test postgres with psql and confirm SSL is enabled
ansible.builtin.command: >
psql "postgres://postgres:[email protected]:5432/postgres?sslmode=require" -c "SHOW ssl"
register: psql_ssl_test
failed_when: psql_ssl_test.rc != 0 or 'on' not in psql_ssl_test.stdout