mysql_replication: add master_use_gtid parameter (#62648)

* mysql_replication: add master_use_gtid parameter

* mysql_replication: add master_use_gtid parameter, improve tests
This commit is contained in:
Andrey Klychkov 2019-10-03 15:32:41 +03:00 committed by Abhijit Menon-Sen
parent 6ad6fde424
commit ea86b2c2f7
4 changed files with 211 additions and 8 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- mysql_replication - add ``master_use_gtid`` parameter (https://github.com/ansible/ansible/pull/62648).

View file

@ -103,31 +103,47 @@ options:
- Whether the host uses GTID based replication or not.
type: bool
version_added: "2.0"
master_use_gtid:
description:
- Configures the slave to use the MariaDB Global Transaction ID.
- C(disabled) equals MASTER_USE_GTID=no command.
- To find information about available values see
U(https://mariadb.com/kb/en/library/change-master-to/#master_use_gtid).
- Available since MariaDB 10.0.2.
choices: [current_pos, slave_pos, disabled]
type: str
version_added: "2.10"
extends_documentation_fragment:
- mysql
'''
EXAMPLES = r'''
# Stop mysql slave thread
- mysql_replication:
- name: Stop mysql slave thread
mysql_replication:
mode: stopslave
# Get master binlog file name and binlog position
- mysql_replication:
- name: Get master binlog file name and binlog position
mysql_replication:
mode: getmaster
# Change master to master server 192.0.2.1 and use binary log 'mysql-bin.000009' with position 4578
- mysql_replication:
- name: Change master to master server 192.0.2.1 and use binary log 'mysql-bin.000009' with position 4578
mysql_replication:
mode: changemaster
master_host: 192.0.2.1
master_log_file: mysql-bin.000009
master_log_pos: 4578
# Check slave status using port 3308
- mysql_replication:
- name: Check slave status using port 3308
mysql_replication:
mode: getslave
login_host: ansible.example.com
login_port: 3308
- name: On MariaDB change master to use GTID current_pos
mysql_replication:
mode: changemaster
master_use_gtid: current_pos
'''
RETURN = r'''
@ -242,6 +258,7 @@ def main():
client_cert=dict(type='path', aliases=['ssl_cert']),
client_key=dict(type='path', aliases=['ssl_key']),
ca_cert=dict(type='path', aliases=['ssl_ca']),
master_use_gtid=dict(type='str', choices=['current_pos', 'slave_pos', 'disabled']),
)
)
mode = module.params["mode"]
@ -266,6 +283,10 @@ def main():
ssl_ca = module.params["ca_cert"]
connect_timeout = module.params['connect_timeout']
config_file = module.params['config_file']
if module.params.get("master_use_gtid") == 'disabled':
master_use_gtid = 'no'
else:
master_use_gtid = module.params["master_use_gtid"]
if mysql_driver is None:
module.fail_json(msg=mysql_driver_fail_msg)
@ -337,6 +358,8 @@ def main():
chm.append("MASTER_SSL_CIPHER='%s'" % master_ssl_cipher)
if master_auto_position:
chm.append("MASTER_AUTO_POSITION=1")
if master_use_gtid is not None:
chm.append("MASTER_USE_GTID=%s" % master_use_gtid)
try:
changemaster(cursor, chm)
except mysql_driver.Warning as e:

View file

@ -4,3 +4,8 @@
# Initial CI tests of mysql_replication module
- import_tasks: mariadb_replication_initial.yml
when: ansible_distribution == 'CentOS' and ansible_distribution_major_version >= '7'
# Tests of master_use_gtid parameter
# https://github.com/ansible/ansible/pull/62648
- import_tasks: mariadb_master_use_gtid.yml
when: ansible_distribution == 'CentOS' and ansible_distribution_major_version >= '7'

View file

@ -0,0 +1,173 @@
# 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)
# Tests for master_use_gtid parameter.
# https://github.com/ansible/ansible/pull/62648
#############################
# master_use_gtid: "disabled"
#############################
# Auxiliary step:
- name: Get master status
mysql_replication:
login_host: 127.0.0.1
login_port: "{{ master_port }}"
mode: getmaster
register: master_status
# Set master_use_gtid disabled:
- name: Run replication
mysql_replication:
login_host: 127.0.0.1
login_port: "{{ standby_port }}"
mode: changemaster
master_host: 127.0.0.1
master_port: "{{ master_port }}"
master_user: "{{ replication_user }}"
master_password: "{{ replication_pass }}"
master_log_file: mysql-bin.000001
master_log_pos: '{{ master_status.Position }}'
master_use_gtid: disabled
register: result
- assert:
that:
- result is changed
# Start standby for further tests:
- name: Start standby
mysql_replication:
login_host: 127.0.0.1
login_port: "{{ master_port }}"
mode: startslave
- name: Get standby status
mysql_replication:
login_host: 127.0.0.1
login_port: "{{ standby_port }}"
mode: getslave
register: slave_status
- assert:
that:
- slave_status.Using_Gtid == 'No'
# Stop standby for further tests:
- name: Stop standby
mysql_replication:
login_host: 127.0.0.1
login_port: "{{ standby_port }}"
mode: stopslave
################################
# master_use_gtid: "current_pos"
################################
# Auxiliary step:
- name: Get master status
mysql_replication:
login_host: 127.0.0.1
login_port: "{{ master_port }}"
mode: getmaster
register: master_status
# Set master_use_gtid current_pos:
- name: Run replication
mysql_replication:
login_host: 127.0.0.1
login_port: "{{ standby_port }}"
mode: changemaster
master_host: 127.0.0.1
master_port: "{{ master_port }}"
master_user: "{{ replication_user }}"
master_password: "{{ replication_pass }}"
master_log_file: mysql-bin.000001
master_log_pos: '{{ master_status.Position }}'
master_use_gtid: current_pos
register: result
- assert:
that:
- result is changed
# Start standby for further tests:
- name: Start standby
mysql_replication:
login_host: 127.0.0.1
login_port: "{{ master_port }}"
mode: startslave
- name: Get standby status
mysql_replication:
login_host: 127.0.0.1
login_port: "{{ standby_port }}"
mode: getslave
register: slave_status
- assert:
that:
- slave_status.Using_Gtid == 'Current_Pos'
# Stop standby for further tests:
- name: Stop standby
mysql_replication:
login_host: 127.0.0.1
login_port: "{{ standby_port }}"
mode: stopslave
##############################
# master_use_gtid: "slave_pos"
##############################
# Auxiliary step:
- name: Get master status
mysql_replication:
login_host: 127.0.0.1
login_port: "{{ master_port }}"
mode: getmaster
register: master_status
# Set master_use_gtid slave_pos:
- name: Run replication
mysql_replication:
login_host: 127.0.0.1
login_port: "{{ standby_port }}"
mode: changemaster
master_host: 127.0.0.1
master_port: "{{ master_port }}"
master_user: "{{ replication_user }}"
master_password: "{{ replication_pass }}"
master_log_file: mysql-bin.000001
master_log_pos: '{{ master_status.Position }}'
master_use_gtid: slave_pos
register: result
- assert:
that:
- result is changed
# Start standby for further tests:
- name: Start standby
mysql_replication:
login_host: 127.0.0.1
login_port: "{{ master_port }}"
mode: startslave
- name: Get standby status
mysql_replication:
login_host: 127.0.0.1
login_port: "{{ standby_port }}"
mode: getslave
register: slave_status
- assert:
that:
- slave_status.Using_Gtid == 'Slave_Pos'
# Stop standby for further tests:
- name: Stop standby
mysql_replication:
login_host: 127.0.0.1
login_port: "{{ standby_port }}"
mode: stopslave