1# Copyright: (c) 2019, Andrew Klychkov (@Andersson007) <aaklychkov@mail.ru>
2# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4# Preparation:
5- name: Create user for replication
6  shell: "echo \"GRANT REPLICATION SLAVE ON *.* TO '{{ replication_user }}'@'localhost' IDENTIFIED BY '{{ replication_pass }}'; FLUSH PRIVILEGES;\" | mysql -P {{ primary_db.port }} -h 127.0.0.1"
7
8- name: Create test database
9  mysql_db:
10    login_host: 127.0.0.1
11    login_port: '{{ primary_db.port }}'
12    state: present
13    name: '{{ test_db }}'
14
15- name: Dump all databases from the master
16  shell: 'mysqldump -P {{ primary_db.port }} -h 127.0.01 --all-databases --master-data=2 > {{ dump_path }}'
17
18- name: Restore the dump to the replica
19  shell: 'mysql -P {{ replica_db.port }} -h 127.0.0.1 < {{ dump_path }}'
20
21# Test getmaster mode:
22- name: Get master status
23  mysql_replication:
24    login_host: 127.0.0.1
25    login_port: "{{ primary_db.port }}"
26    mode: getmaster
27  register: master_status
28
29- assert:
30    that:
31    - master_status.Is_Master == true
32    - master_status.Position != 0
33    - master_status is not changed
34
35# Test changemaster mode:
36- name: Run replication
37  mysql_replication:
38    login_host: 127.0.0.1
39    login_port: "{{ replica_db.port }}"
40    mode: changemaster
41    master_host: 127.0.0.1
42    master_port: "{{ primary_db.port }}"
43    master_user: "{{ replication_user }}"
44    master_password: "{{ replication_pass }}"
45    master_log_file: mysql-bin.000001
46    master_log_pos: '{{ master_status.Position }}'
47  register: result
48
49- assert:
50    that:
51    - result is changed
52    - result.queries[0] is match("CHANGE MASTER ('\S+' )?TO MASTER_HOST='[0-9.]+',MASTER_USER='\w+',MASTER_PASSWORD='[*]{8}',MASTER_PORT=\d+,MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=\d+")
53
54# Test startslave mode:
55- name: Start slave
56  mysql_replication:
57    login_host: 127.0.0.1
58    login_port: "{{ replica_db.port }}"
59    mode: startslave
60  register: result
61
62- assert:
63    that:
64    - result is changed
65    - result.queries == ["START SLAVE"]
66
67# Test getslave mode:
68- name: Get replica status
69  mysql_replication:
70    login_host: 127.0.0.1
71    login_port: "{{ replica_db.port }}"
72    mode: getslave
73  register: slave_status
74
75- assert:
76    that:
77    - slave_status.Is_Slave == true
78    - slave_status.Master_Host == '127.0.0.1'
79    - slave_status.Exec_Master_Log_Pos == master_status.Position
80    - slave_status.Master_Port == {{ primary_db.port }}
81    - slave_status.Last_IO_Errno == 0
82    - slave_status.Last_IO_Error == ''
83    - slave_status is not changed
84
85# Test stopslave mode:
86- name: Stop slave
87  mysql_replication:
88    login_host: 127.0.0.1
89    login_port: "{{ replica_db.port }}"
90    mode: stopslave
91  register: result
92
93- assert:
94    that:
95    - result is changed
96    - result.queries == ["STOP SLAVE"]
97