|
|
@ -146,6 +146,12 @@ options:
|
|
|
|
- For more information see U(https://dev.mysql.com/doc/refman/8.0/en/replication-multi-source.html).
|
|
|
|
- For more information see U(https://dev.mysql.com/doc/refman/8.0/en/replication-multi-source.html).
|
|
|
|
type: str
|
|
|
|
type: str
|
|
|
|
version_added: '2.10'
|
|
|
|
version_added: '2.10'
|
|
|
|
|
|
|
|
fail_on_error:
|
|
|
|
|
|
|
|
description:
|
|
|
|
|
|
|
|
- Fails on error when calling mysql.
|
|
|
|
|
|
|
|
type: bool
|
|
|
|
|
|
|
|
default: False
|
|
|
|
|
|
|
|
version_added: '2.10'
|
|
|
|
|
|
|
|
|
|
|
|
notes:
|
|
|
|
notes:
|
|
|
|
- If an empty value for the parameter of string type is needed, use an empty string.
|
|
|
|
- If an empty value for the parameter of string type is needed, use an empty string.
|
|
|
@ -211,6 +217,18 @@ EXAMPLES = r'''
|
|
|
|
and reset the binary log index file on the master
|
|
|
|
and reset the binary log index file on the master
|
|
|
|
mysql_replication:
|
|
|
|
mysql_replication:
|
|
|
|
mode: resetmaster
|
|
|
|
mode: resetmaster
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- name: Run start slave and fail the task on errors
|
|
|
|
|
|
|
|
mysql_replication:
|
|
|
|
|
|
|
|
mode: startslave
|
|
|
|
|
|
|
|
connection_name: master-1
|
|
|
|
|
|
|
|
fail_on_error: yes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- name: Change master and fail on error (like when slave thread is running)
|
|
|
|
|
|
|
|
mysql_replication:
|
|
|
|
|
|
|
|
mode: changemaster
|
|
|
|
|
|
|
|
fail_on_error: yes
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
RETURN = r'''
|
|
|
|
RETURN = r'''
|
|
|
@ -252,7 +270,7 @@ def get_slave_status(cursor, connection_name='', channel=''):
|
|
|
|
return slavestatus
|
|
|
|
return slavestatus
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def stop_slave(cursor, connection_name='', channel=''):
|
|
|
|
def stop_slave(module, cursor, connection_name='', channel='', fail_on_error=False):
|
|
|
|
if connection_name:
|
|
|
|
if connection_name:
|
|
|
|
query = "STOP SLAVE '%s'" % connection_name
|
|
|
|
query = "STOP SLAVE '%s'" % connection_name
|
|
|
|
else:
|
|
|
|
else:
|
|
|
@ -265,12 +283,16 @@ def stop_slave(cursor, connection_name='', channel=''):
|
|
|
|
executed_queries.append(query)
|
|
|
|
executed_queries.append(query)
|
|
|
|
cursor.execute(query)
|
|
|
|
cursor.execute(query)
|
|
|
|
stopped = True
|
|
|
|
stopped = True
|
|
|
|
except Exception:
|
|
|
|
except mysql_driver.Warning as e:
|
|
|
|
|
|
|
|
stopped = False
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
if fail_on_error:
|
|
|
|
|
|
|
|
module.fail_json(msg="STOP SLAVE failed: %s" % to_native(e))
|
|
|
|
stopped = False
|
|
|
|
stopped = False
|
|
|
|
return stopped
|
|
|
|
return stopped
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def reset_slave(cursor, connection_name='', channel=''):
|
|
|
|
def reset_slave(module, cursor, connection_name='', channel='', fail_on_error=False):
|
|
|
|
if connection_name:
|
|
|
|
if connection_name:
|
|
|
|
query = "RESET SLAVE '%s'" % connection_name
|
|
|
|
query = "RESET SLAVE '%s'" % connection_name
|
|
|
|
else:
|
|
|
|
else:
|
|
|
@ -283,12 +305,16 @@ def reset_slave(cursor, connection_name='', channel=''):
|
|
|
|
executed_queries.append(query)
|
|
|
|
executed_queries.append(query)
|
|
|
|
cursor.execute(query)
|
|
|
|
cursor.execute(query)
|
|
|
|
reset = True
|
|
|
|
reset = True
|
|
|
|
except Exception:
|
|
|
|
except mysql_driver.Warning as e:
|
|
|
|
|
|
|
|
reset = False
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
if fail_on_error:
|
|
|
|
|
|
|
|
module.fail_json(msg="RESET SLAVE failed: %s" % to_native(e))
|
|
|
|
reset = False
|
|
|
|
reset = False
|
|
|
|
return reset
|
|
|
|
return reset
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def reset_slave_all(cursor, connection_name='', channel=''):
|
|
|
|
def reset_slave_all(module, cursor, connection_name='', channel='', fail_on_error=False):
|
|
|
|
if connection_name:
|
|
|
|
if connection_name:
|
|
|
|
query = "RESET SLAVE '%s' ALL" % connection_name
|
|
|
|
query = "RESET SLAVE '%s' ALL" % connection_name
|
|
|
|
else:
|
|
|
|
else:
|
|
|
@ -301,23 +327,31 @@ def reset_slave_all(cursor, connection_name='', channel=''):
|
|
|
|
executed_queries.append(query)
|
|
|
|
executed_queries.append(query)
|
|
|
|
cursor.execute(query)
|
|
|
|
cursor.execute(query)
|
|
|
|
reset = True
|
|
|
|
reset = True
|
|
|
|
except Exception:
|
|
|
|
except mysql_driver.Warning as e:
|
|
|
|
|
|
|
|
reset = False
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
if fail_on_error:
|
|
|
|
|
|
|
|
module.fail_json(msg="RESET SLAVE ALL failed: %s" % to_native(e))
|
|
|
|
reset = False
|
|
|
|
reset = False
|
|
|
|
return reset
|
|
|
|
return reset
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def reset_master(cursor):
|
|
|
|
def reset_master(module, cursor, fail_on_error=False):
|
|
|
|
query = 'RESET MASTER'
|
|
|
|
query = 'RESET MASTER'
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
executed_queries.append(query)
|
|
|
|
executed_queries.append(query)
|
|
|
|
cursor.execute(query)
|
|
|
|
cursor.execute(query)
|
|
|
|
reset = True
|
|
|
|
reset = True
|
|
|
|
except Exception:
|
|
|
|
except mysql_driver.Warning as e:
|
|
|
|
|
|
|
|
reset = False
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
if fail_on_error:
|
|
|
|
|
|
|
|
module.fail_json(msg="RESET MASTER failed: %s" % to_native(e))
|
|
|
|
reset = False
|
|
|
|
reset = False
|
|
|
|
return reset
|
|
|
|
return reset
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def start_slave(cursor, connection_name='', channel=''):
|
|
|
|
def start_slave(module, cursor, connection_name='', channel='', fail_on_error=False):
|
|
|
|
if connection_name:
|
|
|
|
if connection_name:
|
|
|
|
query = "START SLAVE '%s'" % connection_name
|
|
|
|
query = "START SLAVE '%s'" % connection_name
|
|
|
|
else:
|
|
|
|
else:
|
|
|
@ -330,7 +364,11 @@ def start_slave(cursor, connection_name='', channel=''):
|
|
|
|
executed_queries.append(query)
|
|
|
|
executed_queries.append(query)
|
|
|
|
cursor.execute(query)
|
|
|
|
cursor.execute(query)
|
|
|
|
started = True
|
|
|
|
started = True
|
|
|
|
except Exception:
|
|
|
|
except mysql_driver.Warning as e:
|
|
|
|
|
|
|
|
started = False
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
if fail_on_error:
|
|
|
|
|
|
|
|
module.fail_json(msg="START SLAVE failed: %s" % to_native(e))
|
|
|
|
started = False
|
|
|
|
started = False
|
|
|
|
return started
|
|
|
|
return started
|
|
|
|
|
|
|
|
|
|
|
@ -384,6 +422,7 @@ def main():
|
|
|
|
master_delay=dict(type='int'),
|
|
|
|
master_delay=dict(type='int'),
|
|
|
|
connection_name=dict(type='str'),
|
|
|
|
connection_name=dict(type='str'),
|
|
|
|
channel=dict(type='str'),
|
|
|
|
channel=dict(type='str'),
|
|
|
|
|
|
|
|
fail_on_error=dict(type='bool', default=False),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
mutually_exclusive=[
|
|
|
|
mutually_exclusive=[
|
|
|
|
['connection_name', 'channel']
|
|
|
|
['connection_name', 'channel']
|
|
|
@ -418,6 +457,7 @@ def main():
|
|
|
|
master_use_gtid = module.params["master_use_gtid"]
|
|
|
|
master_use_gtid = module.params["master_use_gtid"]
|
|
|
|
connection_name = module.params["connection_name"]
|
|
|
|
connection_name = module.params["connection_name"]
|
|
|
|
channel = module.params['channel']
|
|
|
|
channel = module.params['channel']
|
|
|
|
|
|
|
|
fail_on_error = module.params['fail_on_error']
|
|
|
|
|
|
|
|
|
|
|
|
if mysql_driver is None:
|
|
|
|
if mysql_driver is None:
|
|
|
|
module.fail_json(msg=mysql_driver_fail_msg)
|
|
|
|
module.fail_json(msg=mysql_driver_fail_msg)
|
|
|
@ -502,31 +542,31 @@ def main():
|
|
|
|
result['changed'] = True
|
|
|
|
result['changed'] = True
|
|
|
|
module.exit_json(queries=executed_queries, **result)
|
|
|
|
module.exit_json(queries=executed_queries, **result)
|
|
|
|
elif mode in "startslave":
|
|
|
|
elif mode in "startslave":
|
|
|
|
started = start_slave(cursor, connection_name, channel)
|
|
|
|
started = start_slave(module, cursor, connection_name, channel, fail_on_error)
|
|
|
|
if started is True:
|
|
|
|
if started is True:
|
|
|
|
module.exit_json(msg="Slave started ", changed=True, queries=executed_queries)
|
|
|
|
module.exit_json(msg="Slave started ", changed=True, queries=executed_queries)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
module.exit_json(msg="Slave already started (Or cannot be started)", changed=False, queries=executed_queries)
|
|
|
|
module.exit_json(msg="Slave already started (Or cannot be started)", changed=False, queries=executed_queries)
|
|
|
|
elif mode in "stopslave":
|
|
|
|
elif mode in "stopslave":
|
|
|
|
stopped = stop_slave(cursor, connection_name, channel)
|
|
|
|
stopped = stop_slave(module, cursor, connection_name, channel, fail_on_error)
|
|
|
|
if stopped is True:
|
|
|
|
if stopped is True:
|
|
|
|
module.exit_json(msg="Slave stopped", changed=True, queries=executed_queries)
|
|
|
|
module.exit_json(msg="Slave stopped", changed=True, queries=executed_queries)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
module.exit_json(msg="Slave already stopped", changed=False, queries=executed_queries)
|
|
|
|
module.exit_json(msg="Slave already stopped", changed=False, queries=executed_queries)
|
|
|
|
elif mode in "resetmaster":
|
|
|
|
elif mode in "resetmaster":
|
|
|
|
reset = reset_master(cursor)
|
|
|
|
reset = reset_master(module, cursor, fail_on_error)
|
|
|
|
if reset is True:
|
|
|
|
if reset is True:
|
|
|
|
module.exit_json(msg="Master reset", changed=True, queries=executed_queries)
|
|
|
|
module.exit_json(msg="Master reset", changed=True, queries=executed_queries)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
module.exit_json(msg="Master already reset", changed=False, queries=executed_queries)
|
|
|
|
module.exit_json(msg="Master already reset", changed=False, queries=executed_queries)
|
|
|
|
elif mode in "resetslave":
|
|
|
|
elif mode in "resetslave":
|
|
|
|
reset = reset_slave(cursor, connection_name, channel)
|
|
|
|
reset = reset_slave(module, cursor, connection_name, channel, fail_on_error)
|
|
|
|
if reset is True:
|
|
|
|
if reset is True:
|
|
|
|
module.exit_json(msg="Slave reset", changed=True, queries=executed_queries)
|
|
|
|
module.exit_json(msg="Slave reset", changed=True, queries=executed_queries)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
module.exit_json(msg="Slave already reset", changed=False, queries=executed_queries)
|
|
|
|
module.exit_json(msg="Slave already reset", changed=False, queries=executed_queries)
|
|
|
|
elif mode in "resetslaveall":
|
|
|
|
elif mode in "resetslaveall":
|
|
|
|
reset = reset_slave_all(cursor, connection_name, channel)
|
|
|
|
reset = reset_slave_all(module, cursor, connection_name, channel, fail_on_error)
|
|
|
|
if reset is True:
|
|
|
|
if reset is True:
|
|
|
|
module.exit_json(msg="Slave reset", changed=True, queries=executed_queries)
|
|
|
|
module.exit_json(msg="Slave reset", changed=True, queries=executed_queries)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|