-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql-playbook.yml
101 lines (101 loc) · 3.65 KB
/
mysql-playbook.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
- hosts: "{{ machine }}"
vars:
# mysql_master_host
# mysql_master_port
# mysql_slave_host
# mysql_port
# mysql_instance_type
mysql_instance_id: "{% if mysql_instance_type == 'slave' and mysql_master_host == mysql_slave_host %}2{% endif %}"
become: no
tasks:
- name: Install Python dependencies
become: yes
apt:
name: python3-pip
state: latest
update_cache: yes
- name: Install PyMySQL module
become: yes
pip:
name: pymysql
- name: Install mysql
become: yes
apt:
name: mysql-server
state: latest
- name: Run instance-dependent tasks
include_tasks: 'mysql-tasks{{ mysql_instance_id }}.yml'
- name: Download mysql init script
get_url:
url: https://raw.githubusercontent.com/spring-petclinic/spring-petclinic-rest/29287912f1976ec01fc364e99a5798d6d8a3d6c7/src/main/resources/db/mysql/initDB.sql
dest: .
force: yes
- name: Remove default user creation from mysql init script
lineinfile:
path: ./initDB.sql
regexp: "^GRANT "
state: absent
- name: Change mysql init script db name
replace:
path: ./initDB.sql
regexp: 'petclinic'
replace: '{{ mysql_name }}'
- name: Execute mysql init script
become: yes
shell: "cat ./initDB.sql | mysql -uroot -p'{{ mysql_root_pass }}' -S'/var/run/mysqld/mysqld{{ mysql_instance_id }}.sock'"
- name: Create db user
become: yes
mysql_user:
name: '{{ mysql_user }}'
host: '%'
password: '{{ mysql_pass }}'
priv: '{{ mysql_name }}.*:ALL/*.*:REPLICATION_SLAVE_ADMIN,REPLICATION SLAVE,REPLICATION CLIENT'
login_user: root
login_password: '{{ mysql_root_pass }}'
login_unix_socket: '/var/run/mysqld/mysqld{{ mysql_instance_id }}.sock'
- name: Download mysql data script
get_url:
url: https://raw.githubusercontent.com/spring-petclinic/spring-petclinic-rest/29287912f1976ec01fc364e99a5798d6d8a3d6c7/src/main/resources/db/mysql/populateDB.sql
dest: .
force: yes
- name: Execute mysql data script
become: yes
shell: "cat ./populateDB.sql | mysql -uroot -p'{{ mysql_root_pass }}' -S'/var/run/mysqld/mysqld{{ mysql_instance_id }}.sock' {{ mysql_name }}"
- name: Get replication master data
when: mysql_instance_type == 'slave'
become: yes
mysql_replication:
mode: getmaster
login_user: '{{ mysql_user }}'
login_password: '{{ mysql_pass }}'
login_host: '{{ mysql_master_host }}'
login_port: '{{ mysql_master_port }}'
register: master_data
- name: Print master data
when: mysql_instance_type == 'slave'
debug:
msg: "Data: {{ master_data }}"
- name: Make instance replication slave
when: mysql_instance_type == 'slave'
ignore_errors: yes
become: yes
mysql_replication:
mode: changemaster
master_host: '{{ mysql_master_host }}'
master_port: '{{ mysql_master_port }}'
master_user: '{{ mysql_user }}'
master_password: '{{ mysql_pass }}'
master_log_file: '{{ master_data.File }}'
master_log_pos: '{{ master_data.Position }}'
login_user: '{{ mysql_user }}'
login_password: '{{ mysql_pass }}'
login_unix_socket: '/var/run/mysqld/mysqld2.sock'
- name: Start replication slave
when: mysql_instance_type == 'slave'
become: yes
ignore_errors: yes
mysql_replication:
mode: startslave
login_user: '{{ mysql_user }}'
login_password: '{{ mysql_pass }}'
login_unix_socket: '/var/run/mysqld/mysqld2.sock'