-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy_cluster.yml
More file actions
280 lines (243 loc) · 7.35 KB
/
deploy_cluster.yml
File metadata and controls
280 lines (243 loc) · 7.35 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
---
- name: Deploy MariaDB Replication Cluster
hosts: all
become: true
become_method: sudo
gather_facts: true
any_errors_fatal: true
pre_tasks:
- name: Include main variables
include_vars: "vars/main.yml"
tasks:
- name: Update apt cache
apt:
update_cache: true
cache_valid_time: 3600
register: apt_status
until: apt_status is success
delay: 5
retries: 3
- name: Add entries into /etc/hosts file
lineinfile:
dest: /etc/hosts
regexp: '.*{{ item }}$'
line: "{{ hostvars[item].ansible_host }} {{item}}"
state: present
unsafe_writes: true # to prevent failures in CI
with_items: "{{ groups.all }}"
- name: Update and install some packages
apt:
name:
- apt-transport-https
- python3-pip
- python3-dev
- default-libmysqlclient-dev
- build-essential
update_cache: true
register: apt_status
until: apt_status is success
delay: 5
retries: 3
- name: Make sure pymysql is present
pip:
name:
- pymysql
state: present
register: pip_status
until: pip_status is success
delay: 5
retries: 3
- name: deploy_cluster.yml | Install MariaDB
hosts: mariadb_cluster
become: true
become_method: sudo
gather_facts: true
any_errors_fatal: true
pre_tasks:
- name: Include main variables
include_vars: "vars/main.yml"
tasks:
- name: Import the MariaDB PGP Key
apt_key:
url: "{{mariadb_pgp_key_url}}"
state: present
register: apt_key_status
until: apt_key_status is success
delay: 5
retries: 3
- name: Add MariaDB repository
apt_repository:
repo: "{{mariadb_repo_url}}"
state: present
filename: mariadb
update_cache: yes
register: apt_repo_status
until: apt_repo_status is success
delay: 5
retries: 3
- name: Install mariadb-server and mariadb-client
apt:
name:
- mariadb-server
- mariadb-client
update_cache: yes
register: apt_status
until: apt_status is success
delay: 5
retries: 3
- name: deploy_cluster.yml | Prepare master node
hosts: master
become: true
become_method: sudo
gather_facts: true
any_errors_fatal: true
pre_tasks:
- name: Include main variables
include_vars: "vars/main.yml"
tasks:
- name: Check if mysql_secure_install was already executed
stat:
path: "/tmp/mariadb_secured"
register: mariadb_secured
- name: Set root password
mysql_user:
name: root
password: "{{ mariadb_root_password }}"
login_user: root
login_unix_socket: /var/run/mysqld/mysqld.sock
no_log: true
when:
- set_mariadb_root_password
- not mariadb_secured.stat.exists
- name: Removes all anonymous user accounts
mysql_user:
name: ''
host_all: true
state: absent
login_user: root
login_password: "{{ mariadb_root_password }}"
login_unix_socket: /var/run/mysqld/mysqld.sock
no_log: true
when: not mariadb_secured.stat.exists
- name: Remove test database
mysql_db:
name: test
state: absent
login_user: root
login_password: "{{ mariadb_root_password }}"
login_unix_socket: /var/run/mysqld/mysqld.sock
no_log: true
when: not mariadb_secured.stat.exists
- name: create mariadb_secured
file:
path: "/tmp/mariadb_secured"
state: touch
when: not mariadb_secured.stat.exists
- name: Replace bind address to 0.0.0.0
replace:
path: /etc/mysql/mariadb.conf.d/50-server.cnf
regexp: '127.0.0.1'
replace: 0.0.0.0
- name: Enable binary log
blockinfile:
path: /etc/mysql/mariadb.conf.d/50-server.cnf
block: |
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
max_binlog_size = 100M
relay_log = /var/log/mysql/mysql-relay-bin
relay_log_index = /var/log/mysql/mysql-relay-bin.index
- name: Restart service mariadb
service:
name: mariadb
state: restarted
- name: Create replication user
mysql_user:
name: "{{ mariadb_replication_user }}"
host: "{{ mariadb_replication_host | default('%') }}"
password: "{{ mariadb_replication_password }}"
priv: "*.*:REPLICATION SLAVE"
state: present
login_user: root
login_password: "{{ mariadb_root_password }}"
login_unix_socket: /run/mysqld/mysqld.sock
no_log: true
- name: Create reguler user
mysql_user:
name: "{{ mariadb_user }}"
host: "{{ mariadb_host | default('%') }}"
password: "{{ mariadb_password }}"
priv: "*.*:ALL"
state: present
login_user: root
login_password: "{{ mariadb_root_password }}"
login_unix_socket: /run/mysqld/mysqld.sock
when: create_user
no_log: true
- name: deploy_cluster.yml | prepare replica node
hosts: slave
become: true
become_method: sudo
gather_facts: true
any_errors_fatal: true
pre_tasks:
- name: Include main variables
include_vars: "vars/main.yml"
tasks:
- name: Replace bind address to 0.0.0.0
replace:
path: /etc/mysql/mariadb.conf.d/50-server.cnf
regexp: '127.0.0.1'
replace: 0.0.0.0
- name: Enable binary log
blockinfile:
path: /etc/mysql/mariadb.conf.d/50-server.cnf
block: |
server-id = {{ groups['slave'].index(inventory_hostname) | int + 2 }}
log_bin = /var/log/mysql/mysql-bin.log
max_binlog_size = 100M
relay_log = /var/log/mysql/mysql-relay-bin
relay_log_index = /var/log/mysql/mysql-relay-bin.index
- name: Restart service mariadb
service:
name: mariadb
state: restarted
- name: deploy_cluster.yml | Create replication
hosts: mariadb_cluster
become: true
become_method: sudo
gather_facts: true
any_errors_fatal: true
pre_tasks:
- name: Include main variables
include_vars: "vars/main.yml"
tasks:
- name: Stop slave
command: mariadb -e 'STOP SLAVE;'
when: not is_master
- name: Get replication master status
mysql_replication:
mode: getmaster
login_user: root
login_password: "{{ mariadb_root_password }}"
login_unix_socket: /run/mysqld/mysqld.sock
register: __mariadb_repl_master_status
when: is_master
- name: "Add master status to dummy host"
add_host:
name: "MASTER_STAT_HOLDER"
file: "{{ __mariadb_repl_master_status.File }}"
position: "{{ __mariadb_repl_master_status.Position }}"
- name: Configure replication settings
mysql_replication:
mode: changemaster
master_host: "{{ groups['master'][0] }}"
master_log_file: "{{ hostvars.MASTER_STAT_HOLDER.file }}"
master_log_pos: "{{ hostvars.MASTER_STAT_HOLDER.position }}"
master_user: "{{ mariadb_replication_user }}"
master_password: "{{ mariadb_replication_password }}"
login_unix_socket: /run/mysqld/mysqld.sock
when: not is_master
- name: Start slave
command: mariadb -e 'START SLAVE;'
when: not is_master