This repository has been archived by the owner on Mar 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.yml
105 lines (103 loc) · 3.07 KB
/
main.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
---
- hosts: "{{ master }}"
tasks:
- name: create temporary backup directory on master
tempfile:
state: directory
prefix: ansible-
suffix: .mysqlslave
register: backupdir
- name: dump database from master
shell: >-
innobackupex --no-timestamp {{ backupdir.path }}/dump 2>&1 | tail -n 1
register: innobackupex
failed_when: '"completed OK!" not in innobackupex.stdout'
- name: apply log to database dump
shell: >-
innobackupex --apply-log {{ backupdir.path }}/dump 2>&1 | tail -n 1
register: apply_log
failed_when: '"completed OK!" not in apply_log.stdout'
- name: compress database dump
archive:
path: "{{ backupdir.path }}/dump/"
dest: "{{ backupdir.path }}/dump.tar.gz"
format: gz
owner: root
group: root
mode: 0600
remove: true
- name: create temporary directory on localhost
delegate_to: localhost
tempfile:
state: directory
prefix: ansible-
suffix: .mysqlslave
register: local_tmp
- name: download database dump from master
fetch:
src: "{{ backupdir.path }}/dump.tar.gz"
dest: "{{ local_tmp.path }}/"
flat: true
- name: remove database dump from master
file:
path: "{{ backupdir.path }}"
state: absent
- hosts: "{{ slave }}"
tasks:
- name: stop MySQL on slave
service:
name: mysql
state: stopped
- name: delete slave data
file:
path: /var/lib/mysql
state: absent
- name: create /var/lib/mysql
file:
path: /var/lib/mysql
state: directory
owner: mysql
group: mysql
mode: 0750
- name: uncompress database dump
unarchive:
src: "{{ hostvars[master].local_tmp.path }}/dump.tar.gz"
dest: /var/lib/mysql
owner: mysql
group: mysql
- name: start MySQL on slave
service:
name: mysql
state: started
- name: get binlog file
command: >-
sed -r 's/^(.*)\s[0-9]+$/\1/'
/var/lib/mysql/xtrabackup_binlog_pos_innodb
args:
warn: false
register: binlog_file
- name: get binlog position
command: >-
sed -r 's/^.*\s([0-9]+)$/\1/'
/var/lib/mysql/xtrabackup_binlog_pos_innodb
args:
warn: false
register: binlog_position
- name: configure MySQL slave process
mysql_replication:
master_host: "{{ mysql_replication_master | default(master) }}"
master_user: "{{ mysql_replication_user }}"
master_password: "{{ mysql_replication_password }}"
master_log_file: "{{ binlog_file.stdout }}"
master_log_pos: "{{ binlog_position.stdout }}"
master_ssl: 1
master_ssl_capath: /etc/ssl/certs
mode: changemaster
- name: start MySQL slave process
mysql_replication:
mode: startslave
- name: remove database dump from localhost
delegate_to: localhost
file:
path: "{{ hostvars[master].local_tmp.path }}"
state: absent