forked from lyw07/dbadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_setup.yml
More file actions
170 lines (140 loc) · 4.85 KB
/
db_setup.yml
File metadata and controls
170 lines (140 loc) · 4.85 KB
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
---
- hosts: master
tasks:
- name: copy postgresql configuration file into the data folder
template:
src: config/master/postgresql.j2
dest: /etc/postgresql/9.6/main/postgresql.conf
- name: copy pg_hba file into the data folder
template:
src: config/master/pg_hba.j2
dest: /etc/postgresql/9.6/main/pg_hba.conf
- name: create repmgr.conf
template:
src: config/master/repmgr.j2
dest: /etc/repmgr.conf
- block:
- name: generate ssh keys
expect:
command: ssh-keygen -t rsa
responses:
'Enter file in which to save the key \(/var/lib/postgresql/.ssh/id_rsa\):': "\n"
'Enter passphrase \(empty for no passphrase\):': "\n"
'Enter same passphrase again:': "\n"
- name: create config for ssh
file:
path: /var/lib/postgresql/.ssh/config
state: touch
- name: add barman server's ip address to config
lineinfile:
dest: /var/lib/postgresql/.ssh/config
line: "Host {{ hostvars[groups['barman'][0]]['hostname'] }}"
- name: ignore ssh host key checking
lineinfile:
dest: /var/lib/postgresql/.ssh/config
line: 'StrictHostKeyChecking no'
- name: create user repmgr
command: createuser -s repmgr
- name: create databse repmgr
command: createdb repmgr -O repmgr
become: true
become_method: sudo
become_user: postgres
- name: fetch the public key to store in the local box
fetch:
src: /var/lib/postgresql/.ssh/id_rsa.pub
dest: tmp
- name: restart server
service:
name: postgresql
state: restarted
- name: reload configuration files
service:
name: postgresql
state: reloaded
- name: register as master
command: repmgr -f /etc/repmgr.conf master register
become: true
become_user: postgres
become_method: sudo
- hosts: standby1
tasks:
- name: copy postgresql configuration file into the data folder
template:
src: config/standby/standby_one/postgresql.j2
dest: /etc/postgresql/9.6/main/postgresql.conf
- name: copy pg_hba file into the data folder
template:
src: config/standby/standby_one/pg_hba.j2
dest: /etc/postgresql/9.6/main/pg_hba.conf
- name: create repmgr.conf
template:
src: config/standby/standby_one/repmgr.j2
dest: /etc/repmgr.conf
- hosts: standby2
tasks:
- name: copy postgresql configuration file into the data folder
template:
src: config/standby/standby_two/postgresql.j2
dest: /etc/postgresql/9.6/main/postgresql.conf
- name: copy pg_hba file into the data folder
template:
src: config/standby/standby_two/pg_hba.j2
dest: /etc/postgresql/9.6/main/pg_hba.conf
- name: create repmgr.conf
template:
src: config/standby/standby_two/repmgr.j2
dest: /etc/repmgr.conf
- hosts: standby
tasks:
- name: add postgres as a sudoer
lineinfile:
dest: /etc/sudoers.d/postgres
line: >
postgres ALL=(root) NOPASSWD: /bin/systemctl start postgresql,
/bin/systemctl stop postgresql,
/bin/systemctl restart postgresql,
/bin/systemctl reload postgresql,
/usr/bin/pg_ctlcluster 9.6 main promote
create: yes
state: present
mode: 0600
- name: install barman-cli
apt:
name: barman-cli
- block:
- name: generate ssh keys
expect:
command: ssh-keygen -t rsa
responses:
'Enter file in which to save the key \(/var/lib/postgresql/.ssh/id_rsa\):': "\n"
'Enter passphrase \(empty for no passphrase\):': "\n"
'Enter same passphrase again:': "\n"
- name: create config for ssh
file:
path: /var/lib/postgresql/.ssh/config
state: touch
- name: add barman server ip address to config
lineinfile:
dest: /var/lib/postgresql/.ssh/config
line: "Host {{ hostvars[groups['barman'][0]]['hostname'] }}"
- name: add user to config
lineinfile:
dest: /var/lib/postgresql/.ssh/config
line: 'User barman'
- name: ignore ssh host key checking
lineinfile:
dest: /var/lib/postgresql/.ssh/config
line: 'StrictHostKeyChecking no'
- name: copy barman public key into authorized_keys
copy:
src: tmp/machine1/var/lib/barman/.ssh/id_rsa.pub
dest: /var/lib/postgresql/.ssh/authorized_keys
mode: 0600
become: true
become_method: sudo
become_user: postgres
- name: fetch the public key to store in the local box
fetch:
src: /var/lib/postgresql/.ssh/id_rsa.pub
dest: tmp