This repository has been archived by the owner on Sep 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpatroni_server.yml
90 lines (83 loc) · 3.72 KB
/
patroni_server.yml
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
---
- name: Configure PostgreSQL
hosts: meta-role_patroni_server
any_errors_fatal: true
become: true
become_user: root
roles:
- local.pgdg
- local.timescaledb
- kostiantyn-nemchenko.patroni
- bdellegrazie.postgres_exporter
tasks:
- name: Create patronictl configuration directory
file:
state: directory
recurse: true
path: "{{ ansible_env.HOME }}/.config/patroni/"
- name: Create patroni configuration link
file:
state: link
src: "{{ patroni_config_dir }}/{{ patroni_config_file }}"
dest: "{{ ansible_env.HOME }}/.config/patroni/patronictl.yaml"
# Wait until postgresql becomes ready
- name: Check PostgreSQL server availability
postgresql_ping:
become: true
become_user: "{{ postgresql_user }}"
register: pg_is_in_available
retries: "{{ retries|default(10) }}"
delay: "{{ delay|default(30) }}"
until: pg_is_in_available.is_available
# Identify which node is primary
- name: Get the recovery status of postgresql
postgresql_query:
query: select pg_is_in_recovery();
become: true
become_user: "{{ postgresql_user }}"
register: pg_is_in_recovery
retries: "{{ retries|default(10) }}"
delay: "{{ delay|default(30) }}"
- debug:
var: pg_is_in_recovery.query_result[0].pg_is_in_recovery
# We are running a cluster, let's write only where it matter
- name: Create databases associated roles and privs.
block:
# Shameless copy of https://github.com/geerlingguy/ansible-role-postgresql/blob/master/tasks/users.yml
- name: Ensure PostgreSQL users are present.
postgresql_user:
name: "{{ item.name }}"
password: "{{ item.password | default(omit) }}"
encrypted: "{{ item.encrypted | default(omit) }}"
priv: "{{ item.priv | default(omit) }}"
role_attr_flags: "{{ item.role_attr_flags | default(omit) }}"
db: "{{ item.db | default(omit) }}"
login_host: "{{ item.login_host | default('localhost') }}"
login_password: "{{ item.login_password | default(omit) }}"
login_user: "{{ item.login_user | default(postgresql_user) }}"
login_unix_socket: "{{ item.login_unix_socket | default('/var/run/postgresql') }}"
port: "{{ item.port | default(omit) }}"
state: "{{ item.state | default('present') }}"
with_items: "{{ patroni_users|default([]) }}"
- name: Ensure PostgreSQL databases are present.
postgresql_db:
name: "{{ item.name }}"
lc_collate: "{{ item.lc_collate | default('en_US.UTF-8') }}"
lc_ctype: "{{ item.lc_ctype | default('en_US.UTF-8') }}"
encoding: "{{ item.encoding | default('UTF-8') }}"
template: "{{ item.template | default('template0') }}"
login_host: "{{ item.login_host | default('localhost') }}"
login_password: "{{ item.login_password | default(omit) }}"
login_user: "{{ item.login_user | default(postgresql_user) }}"
login_unix_socket: "{{ item.login_unix_socket | default('/var/run/postgresql') }}"
port: "{{ item.port | default(omit) }}"
owner: "{{ item.owner | default(postgresql_user) }}"
state: "{{ item.state | default('present') }}"
with_items: "{{ patroni_databases|default([]) }}"
- include_tasks: tasks/postgresql-database-extension-create.yml
loop: "{{ patroni_databases|default([]) }}"
loop_control:
loop_var: database
become: true
become_user: "{{ postgresql_user|default('postgres') }}"
when: not pg_is_in_recovery.query_result[0].pg_is_in_recovery