mysql_replication: add support of resetmaster choice to mode parameter (#63321)

pull/57401/head
Andrey Klychkov 5 years ago committed by René Moser
parent 64a66f566d
commit 3e87429365

@ -0,0 +1,2 @@
minor_changes:
- mysql_replication - add support of ``resetmaster`` choice to ``mode`` parameter (https://github.com/ansible/ansible/issues/42870).

@ -34,6 +34,7 @@ options:
C(getslave) (SHOW SLAVE STATUS),
C(startslave) (START SLAVE),
C(stopslave) (STOP SLAVE),
C(resetmaster) (RESET MASTER) - supported from Ansible 2.10,
C(resetslave) (RESET SLAVE),
C(resetslaveall) (RESET SLAVE ALL).
type: str
@ -43,6 +44,7 @@ options:
- getslave
- startslave
- stopslave
- resetmaster
- resetslave
- resetslaveall
default: getslave
@ -200,6 +202,12 @@ EXAMPLES = r'''
mysql_replication:
mode: stopslave
channel: master-1
- name: >
Run RESET MASTER command which will delete all existing binary log files
and reset the binary log index file on the master
mysql_replication:
mode: resetmaster
'''
RETURN = r'''
@ -295,6 +303,17 @@ def reset_slave_all(cursor, connection_name='', channel=''):
return reset
def reset_master(cursor):
query = 'RESET MASTER'
try:
executed_queries.append(query)
cursor.execute(query)
reset = True
except Exception:
reset = False
return reset
def start_slave(cursor, connection_name='', channel=''):
if connection_name:
query = "START SLAVE '%s'" % connection_name
@ -335,7 +354,8 @@ def main():
login_port=dict(type='int', default=3306),
login_unix_socket=dict(type='str'),
mode=dict(type='str', default='getslave', choices=[
'getmaster', 'getslave', 'changemaster', 'stopslave', 'startslave', 'resetslave', 'resetslaveall']),
'getmaster', 'getslave', 'changemaster', 'stopslave',
'startslave', 'resetmaster', 'resetslave', 'resetslaveall']),
master_auto_position=dict(type='bool', default=False),
master_host=dict(type='str'),
master_user=dict(type='str'),
@ -490,6 +510,12 @@ def main():
module.exit_json(msg="Slave stopped", changed=True, queries=executed_queries)
else:
module.exit_json(msg="Slave already stopped", changed=False, queries=executed_queries)
elif mode in "resetmaster":
reset = reset_master(cursor)
if reset is True:
module.exit_json(msg="Master reset", changed=True, queries=executed_queries)
else:
module.exit_json(msg="Master already reset", changed=False, queries=executed_queries)
elif mode in "resetslave":
reset = reset_slave(cursor, connection_name, channel)
if reset is True:

@ -12,3 +12,7 @@
# Tests of channel parameter:
- import_tasks: mysql_replication_channel.yml
when: ansible_distribution == 'CentOS' and ansible_distribution_major_version >= '7'
# Tests of resetmaster mode:
- import_tasks: mysql_replication_resetmaster_mode.yml
when: ansible_distribution == 'CentOS' and ansible_distribution_major_version >= '7'

@ -0,0 +1,48 @@
# Copyright: (c) 2019, Andrew Klychkov (@Andersson007) <aaklychkov@mail.ru>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# Needs for further tests:
- name: Stop slave
mysql_replication:
login_host: 127.0.0.1
login_port: "{{ standby_port }}"
mode: stopslave
- name: Reset slave all
mysql_replication:
login_host: 127.0.0.1
login_port: "{{ standby_port }}"
mode: resetslaveall
# Get master initial status:
- name: Get master status
mysql_replication:
login_host: 127.0.0.1
login_port: "{{ master_port }}"
mode: getmaster
register: master_initial_status
# Test resetmaster mode:
- name: Reset master
mysql_replication:
login_host: 127.0.0.1
login_port: "{{ master_port }}"
mode: resetmaster
register: result
- assert:
that:
- result is changed
- result.queries == ["RESET MASTER"]
# Get master final status:
- name: Get master status
mysql_replication:
login_host: 127.0.0.1
login_port: "{{ master_port }}"
mode: getmaster
register: master_final_status
- assert:
that:
- master_initial_status.File != master_final_status.File
Loading…
Cancel
Save