-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgresql-replicated.yml
208 lines (179 loc) · 7.77 KB
/
postgresql-replicated.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# A replicated postgres cluster installer that works on single node too
---
- hosts: all
sudo: yes
remote_user: root
vars:
main_nodes_ips: "{% set IP_ARR=[] %}{% for host in groups['all'] %}{% if IP_ARR.insert(loop.index,hostvars[host]['ansible_default_ipv4']['address']) %}{% endif %}{% endfor %}{{IP_ARR|join(' ')}}"
main_nodes_ipc: "{% set IP_ARR=[] %}{% for host in groups['all'] %}{% if IP_ARR.insert(loop.index,hostvars[host]['ansible_default_ipv4']['address']) %}{% endif %}{% endfor %}{{IP_ARR|join(',')}}"
master_name: "{{ hostvars[groups['all'][0]]['ansible_hostname'] }}"
master_ip: "{{ hostvars[groups['all'][0]]['ansible_default_ipv4'] }}"
slave_name: "{{ hostvars[groups['all'][1]]['ansible_hostname'] }}"
slave_ip: "{{ hostvars[groups['all'][1]]['ansible_default_ipv4'] }}"
host_count: "{{ groups['all'] | length }}"
service_name: postgresql
vars_files:
- "{{ var_file }}"
tasks:
####
# Start task: basics
# Add packages & stop service for now
- name: Postgresql ensure packages installed
apt: pkg={{ item }} state=latest
with_items:
- "postgresql-9.6"
- "postgresql-client-9.6"
# Get information about user
- name: Postgresql replication user info
sudo: yes
sudo_user: postgres
command: psql -c "SELECT 999 FROM pg_roles WHERE rolname='repuser'"
register: repuser
# Create replication user
- name: Postgresql create replication user
sudo: yes
sudo_user: postgres
command: "psql -c \"CREATE ROLE repuser PASSWORD '{{ postgres.replication.user }}' REPLICATION LOGIN; \""
when: "{{ repuser.stdout.find('999') == -1 }}"
# Stop service
- name: Postgresql Stop service
command: service {{ service_name }} stop
# Make sure all required directories are available
- name: postgresql create infra dirs
command: mkdir -p {{ item }}
with_items:
- /infra/etc/{{ cluster_name }}/postgresql/9.6/main
- /infra/etc/{{ cluster_name }}/network
- /infra/etc/{{ cluster_name }}/network/iptables-specific.d
- /etc/network/iptables-specific.d
- /var/lib/postgresql/9.6/wals
# Make sure all postgresql directories have right ownership
- name: postgresql chown postgres dirs
command: chown postgres:postgres /var/lib/postgresql/9.6/wals
with_items:
- /var/lib/postgresql/9.6/wals
# end task: basics
####
####
# start task: postgresql service configuration
- name: postgresql check for postgresql conf files info
stat: path=/infra/etc/{{ cluster_name }}/postgresql/9.6/main/{{item.infra}}
register: stat_result
with_items:
- { dest: 'postgresql.conf', infra: "postgresql.master.conf.{{ ansible_hostname }}" , template: 'postgresql.master.conf' }
- { dest: 'postgresql.conf', infra: "postgresql.slave.conf.{{ ansible_hostname }}" , template: 'postgresql.slave.conf' }
- { dest: 'pg_hba.conf', infra: 'pg_hba.conf', template: 'pg_hba.conf' }
- { dest: 'pg_ident.conf', infra: 'pg_ident.conf', template: 'pg_ident.conf' }
- { dest: 'start.conf', infra: 'start.conf', template: 'start.conf' }
- { dest: 'pg_ctl.conf', infra: 'pg_ctl.conf', template: 'pg_ctl.conf' }
- { dest: 'environment', infra: 'environment', template: 'environment' }
# ensure the package conf is absent
- name : remove package source conf
command : rm -f /etc/postgresql/9.6/main/{{item.item.dest}}
when: not item.stat.exists
with_items: "{{ stat_result.results }}"
# templates
- name: postgresql generate postgresql template conf files
template:
force: yes
src: templates/postgresql/9.6/main/{{item.item.template}}
dest: /infra/etc/{{ cluster_name }}/postgresql/9.6/main/{{item.item.infra}}
owner: postgres
group: postgres
mode: 0660
when: not item.stat.exists
with_items: "{{ stat_result.results }}"
# deploy conf on master
- name: postgresql symlink master conf file
command: ln -s /infra/etc/{{ cluster_name }}/postgresql/9.6/main/{{item.item.infra}} /etc/postgresql/9.6/main/{{item.item.dest}}
when:
- "'{{ ansible_hostname }}' == '{{ master_name }}'"
- not item.stat.exists
- item.item.template != "postgresql.slave.conf"
with_items: "{{ stat_result.results }}"
# deploy conf on slave
- name: postgresql symlink slave conf file
command: ln -s /infra/etc/{{ cluster_name }}/postgresql/9.6/main/{{item.item.infra}} /etc/postgresql/9.6/main/{{item.item.dest}}
when:
- "'{{ ansible_hostname }}' != '{{ master_name }}'"
- not item.stat.exists
- item.item.template != "postgresql.master.conf"
with_items: "{{ stat_result.results }}"
# end task: postgresql service configuration
####
####
# start task: postgresql recovery configuration
- name: postgresql check for postgresql recovery file info
stat: path=/infra/etc/{{ cluster_name }}/postgresql/9.6/main/{{item.infra}}
register: stat_result
with_items:
- { dest: 'recovery.conf', infra: "recovery.conf.{{ ansible_hostname }}" }
when:
- "{{ host_count == 2 }}"
# master template
- name: postgresql generate master postgresql template recovery files
template:
force: yes
src: templates/postgresql/9.6/main/recovery.master.conf
dest: /infra/etc/{{ cluster_name }}/postgresql/9.6/main/{{item.item.infra}}
owner: root
group: root
mode: 0600
when:
- "{{ host_count == 2 }}"
- "'{{ ansible_hostname }}' == '{{ master_name }}'"
- not item.stat.exists
with_items: "{{ stat_result.results }}"
# slave template
- name: postgresql generate slave postgresql template recovery files
template:
force: yes
src: templates/postgresql/9.6/main/recovery.slave.conf
dest: /infra/etc/{{ cluster_name }}/postgresql/9.6/main/{{item.item.infra}}
owner: root
group: root
mode: 0600
when:
- "{{ host_count == 2 }}"
- "'{{ ansible_hostname }}' != '{{ master_name }}'"
- not item.stat.exists
with_items: "{{ stat_result.results }}"
# deploy recovery on slave
- name: postgresql symlink slave recovery file
command: cp /infra/etc/{{ cluster_name }}/postgresql/9.6/main/{{item.item.infra}} /var/lib/postgresql/9.6/main/{{item.item.dest}}
when:
- "{{ host_count == 2 }}"
- "'{{ ansible_hostname }}' != '{{ master_name }}'"
- not item.stat.exists
with_items: "{{ stat_result.results }}"
# end task: postgresql recovery configuration
####
####
# start task: iptables configuration
- name: postgresql check for iptables files info
stat: path={{item.src}}
register: stat_result
with_items:
- { name: 'iptables_specific', tmpl: 'files/iptables-specific',src: '/infra/etc/{{ cluster_name }}/network/iptables-specific', dest: '/etc/network/iptables-specific' }
- { name: 'iptables_postgresql', tmpl: 'templates/iptables.postgresql.conf',src: '/infra/etc/{{ cluster_name }}/network/iptables-specific.d/postgresql.conf', dest: '/etc/network/iptables-specific.d/postgresql.conf' }
- name: postgresql create firewall file from template
template:
src: "{{ item.item.tmpl }}"
dest: "{{ item.item.src }}"
owner: root
group: root
mode: 0755
when: not item.stat.exists
with_items: "{{ stat_result.results }}"
- name: Postgresql symlink iptables-specific files
command: ln -s {{ item.item.src }} {{ item.item.dest }}
when: not item.stat.exists
with_items: "{{ stat_result.results }}"
- name: Postgresql Reload Firewall
command: /etc/network/iptables_on.sh
# End task: Iptables configuration
####
####
# Restart service
- name: Postgresql Start services
command: service {{ service_name }} start