This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mariadb-multi-master.yml
188 lines (166 loc) · 5.75 KB
/
mariadb-multi-master.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
- name: MariaDB Multi Master Setup
hosts: master
vars:
- root_password: rootpswd
- db_username: user
- db_password: password
- database: newdb
become: true
tasks:
- name: Run Apt Update
ansible.builtin.apt:
update_cache: true
- name: Install Python and Pip for Ansible Script
ansible.builtin.apt:
name:
- python3
- python3-pip
- name: Install Python Module for Ansible MariaDB Automation
ansible.builtin.pip:
name:
- PyMySQL
- name: MariaDB Installation
ansible.builtin.apt:
name:
- mariadb-server
- mariadb-client
- name: add [mysqld] in my.cnf
ansible.builtin.lineinfile:
path: /etc/mysql/my.cnf
regexp: "\\[mysqld]"
line: "[mysqld]"
state: present
- name: Add configuration in my.cnf
ansible.builtin.lineinfile:
path: /etc/mysql/my.cnf
regexp: "{{item}}"
line: "{{item}}"
state: present
with_items:
- "bind-address = 0.0.0.0"
- "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: Delete Anonymous user
community.mysql.mysql_user:
name: ''
host: localhost
state: absent
login_unix_socket: /run/mysqld/mysqld.sock
- name: Set Root Password with defined variable
community.mysql.mysql_user:
name: "root"
password: "{{root_password}}"
host: "{{item}}"
login_unix_socket: /run/mysqld/mysqld.sock
with_items:
- localhost
- name: Add server-id line in my.cnf[Server 0]
ansible.builtin.lineinfile:
path: /etc/mysql/my.cnf
regexp: "{{item}}"
line: "{{item}}"
state: present
with_items:
- "server-id = 1"
when: inventory_hostname in groups["master"][0]
- name: Add server-id line in my.cnf[Server 1]
ansible.builtin.lineinfile:
path: /etc/mysql/my.cnf
regexp: "{{item}}"
line: "{{item}}"
state: present
with_items:
- "server-id = 2"
when: inventory_hostname in groups["master"][1:]
- name: "Create user dbrep for replication"
community.mysql.mysql_user:
name: "dbrep"
password: "dbrep"
priv: "*.*:REPLICATION SLAVE"
host: "%"
login_user: root
login_password: "{{root_password}}"
state: present
- name: Restart and Enable MariaDB
ansible.builtin.service:
name: mysql
state: restarted
enabled: true
- name: Get Master Status
community.mysql.mysql_info:
login_user: root
login_password: "{{root_password}}"
login_unix_socket: /run/mysqld/mysqld.sock
register: master_status
- name: Get Master Host
command: echo "{{hostvars[inventory_hostname].inventory_hostname}}"
register: master_host
- name: Stop and then Reset Replication
community.mysql.mysql_replication:
mode: "{{item}}"
login_user: root
login_password: "{{root_password}}"
login_host: localhost
when: inventory_hostname in groups["master"]
with_items:
- stopreplica
- resetreplica
- name: Config Replication[Server 0]
community.mysql.mysql_replication:
mode: changeprimary
login_user: root
login_password: "{{root_password}}"
login_host: localhost
primary_host : "{{ hostvars[groups['master'].1].master_host.stdout }}"
# primary_host : "172.17.132.107"
primary_user : "dbrep"
primary_password : "dbrep"
primary_log_file : "{{ hostvars[groups['master'].1].master_status.master_status.File }}"
primary_log_pos : "{{ hostvars[groups['master'].1].master_status.master_status.Position }}"
when: inventory_hostname in groups["master"][0]
- name: Config Replication[Server 1]
community.mysql.mysql_replication:
mode: changeprimary
login_user: root
login_password: "{{root_password}}"
login_host: localhost
primary_host : "{{ hostvars[groups['master'].0].master_host.stdout }}"
# primary_host : "172.17.133.26"
primary_user : "dbrep"
primary_password : "dbrep"
primary_log_file : "{{ hostvars[groups['master'].0].master_status.master_status.File }}"
primary_log_pos : "{{ hostvars[groups['master'].0].master_status.master_status.Position }}"
when: inventory_hostname in groups["master"][1:]
- name: Start Replica on Master 0
community.mysql.mysql_replication:
mode: startreplica
login_user: root
login_password: "{{root_password}}"
login_host: localhost
when: inventory_hostname in groups["master"][0]
- name: Start Replica on Master 1
community.mysql.mysql_replication:
mode: startreplica
login_user: root
login_password: "{{root_password}}"
login_host: localhost
when: inventory_hostname in groups["master"][1]
- name: Create Database "{{database}}"
community.mysql.mysql_db:
name: "{{database}}"
login_user: root
login_password: "{{root_password}}"
login_host: localhost
when: inventory_hostname in groups["master"][0]
- name: "Create user {{db_username}} for database {{database}}"
community.mysql.mysql_user:
name: "{{db_username}}"
password: "{{db_password}}"
priv: "{{database}}.*:ALL,GRANT"
host: "%"
login_user: root
login_password: "{{root_password}}"
state: present
when: inventory_hostname in groups["master"][0]