mirror of
https://github.com/HanXHX/ansible-mysql.git
synced 2026-02-24 10:13:30 +07:00
Replication tasks in seperated directory
This commit is contained in:
20
tasks/replication/main.yml
Normal file
20
tasks/replication/main.yml
Normal file
@@ -0,0 +1,20 @@
|
||||
---
|
||||
|
||||
- name: MYSQL_VARIABLES | Get MySQL vendor
|
||||
mysql_variables: variable=version_comment
|
||||
register: vc
|
||||
|
||||
- name: SET_FACT | Server can use GTID
|
||||
set_fact: mysql_gtid=true
|
||||
when: >
|
||||
( mysql_vendor == 'mariadb' and vc.msg[0][1] | version_compare('10.0.2', 'ge') ) or
|
||||
( vc.msg[0][1] | version_compare('5.6.5', 'ge') )
|
||||
|
||||
- name: INCLUDE | Replication Master
|
||||
include: master.yml
|
||||
when: mysql_replication_master
|
||||
|
||||
- name: INCLUDE | Replication slave
|
||||
include: slave.yml
|
||||
when: mysql_replication_slave
|
||||
|
||||
5
tasks/replication/master.yml
Normal file
5
tasks/replication/master.yml
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
|
||||
- name: TEMPLATE | Deploy master configuration
|
||||
template: src=../templates/etc/mysql/conf.d/50-master.cnf.j2 dest=/etc/mysql/conf.d/50-master.cnf
|
||||
notify: restart mysql
|
||||
38
tasks/replication/slave.yml
Normal file
38
tasks/replication/slave.yml
Normal file
@@ -0,0 +1,38 @@
|
||||
---
|
||||
|
||||
- name: MYSQL_VARIABLES | Set read only
|
||||
mysql_variables: variable=read_only value=ON
|
||||
when: mysql_slave_readonly
|
||||
|
||||
- name: TEMPLATE | Deploy slave configuration
|
||||
template: src=../templates/etc/mysql/conf.d/51-slave.cnf.j2 dest=/etc/mysql/conf.d/51-slave.cnf
|
||||
notify: restart mysql
|
||||
|
||||
- name: MYSQL_REPLICATION | Get slave status
|
||||
mysql_replication: mode=getslave
|
||||
ignore_errors: yes
|
||||
register: slave_status
|
||||
|
||||
- name: INCLUDE | Import data
|
||||
include: slave/import_data.yml
|
||||
when: slave_status.failed is defined and mysql_slave_import_data
|
||||
|
||||
- name: INCLUDE | Configure replication
|
||||
include: slave/replication.yml
|
||||
when: slave_status.failed is defined or mysql_slave_force_setup
|
||||
|
||||
- name: INCLUDE | Transfert /etc/mysql/debian.cnf from master
|
||||
include: slave/debiancnf.yml
|
||||
when: mysql_slave_replicate_mysqldb
|
||||
|
||||
- name: MYSQL_REPLICATION | Get slave status
|
||||
mysql_replication: mode=getslave
|
||||
ignore_errors: yes
|
||||
register: slave_status
|
||||
|
||||
- name: Configure MariaDB GTID
|
||||
include: slave/mariadb_gtid.yml
|
||||
when: >
|
||||
mysql_vendor == 'mariadb' and
|
||||
slave_status.Using_Gtid == 'No' and
|
||||
mysql_gtid
|
||||
21
tasks/replication/slave/debiancnf.yml
Normal file
21
tasks/replication/slave/debiancnf.yml
Normal file
@@ -0,0 +1,21 @@
|
||||
---
|
||||
|
||||
- name: FETCH | Get /etc/mysql/debian.cnf on master
|
||||
fetch: >
|
||||
src=/etc/mysql/debian.cnf
|
||||
dest=/tmp/{{ mysql_slave_import_from }}/debian.cnf
|
||||
flat=yes
|
||||
changed_when: false
|
||||
delegate_to: "{{ mysql_slave_import_from }}"
|
||||
|
||||
- name: LOCAL_ACTION FILE | Secure fetched file
|
||||
local_action: file path=/tmp/{{ mysql_slave_import_from }}/debian.cnf mode=0600
|
||||
become: no
|
||||
|
||||
- name: COPY | Fetched file to /etc/mysql/debian.cnf
|
||||
copy: >
|
||||
src=/tmp/{{ mysql_slave_import_from }}/debian.cnf
|
||||
dest=/etc/mysql/debian.cnf
|
||||
owner=root group=root mode=0600
|
||||
notify: restart mysql
|
||||
|
||||
72
tasks/replication/slave/import_data.yml
Normal file
72
tasks/replication/slave/import_data.yml
Normal file
@@ -0,0 +1,72 @@
|
||||
---
|
||||
|
||||
# Doc: https://www.percona.com/doc/percona-xtrabackup/2.1/howtos/recipes_ibkx_gtid.html
|
||||
|
||||
- name: WAIT_FOR | source data (prevent rsync bug) - TODO find another hack
|
||||
wait_for: host={{ mysql_slave_import_from }} port=22
|
||||
|
||||
- name: COMMAND | Prepare backup another server
|
||||
command: innobackupex --no-timestamp {{ mysql_backup_dir }} creates={{ mysql_backup_dir }}
|
||||
delegate_to: "{{ mysql_slave_import_from }}"
|
||||
register: backup
|
||||
|
||||
- name: SHELL | Dump
|
||||
shell: innobackupex --apply-log {{ mysql_backup_dir }}
|
||||
delegate_to: "{{ mysql_slave_import_from }}"
|
||||
when: backup.changed
|
||||
|
||||
- name: FILE | Remove mysql db from backup
|
||||
file: path={{ mysql_backup_dir }}/mysql state=absent
|
||||
delegate_to: "{{ mysql_slave_import_from }}"
|
||||
when: backup.changed and not mysql_slave_replicate_mysqldb
|
||||
|
||||
- name: MYSQL_VARIABLES | Get datadir
|
||||
mysql_variables: variable=datadir
|
||||
register: datadir
|
||||
|
||||
- name: SET_FACT | related to mysql datadir
|
||||
set_fact:
|
||||
mysql_mysql_datadir: "{{ datadir.msg[0][1] }}"
|
||||
mysql_binlog_info: "{{ datadir.msg[0][1] }}/xtrabackup_binlog_info"
|
||||
|
||||
- name: SERVICE | Stop MySQL before importing data
|
||||
service: name=mysql state=stopped
|
||||
|
||||
- name: COMMAND | Sync backup to slave - TODO remove vagrant as static user (see why mysql_backup_user is not working)
|
||||
shell: "sudo -E rsync --rsync-path='sudo rsync' -a -e 'ssh -o StrictHostKeyChecking=no' vagrant@{{ mysql_slave_import_from }}:{{ mysql_backup_dir }}/ {{ mysql_mysql_datadir }}/"
|
||||
become: no
|
||||
|
||||
- name: FILE | Re-apply owner
|
||||
file: >
|
||||
path={{ mysql_mysql_datadir }}
|
||||
state=directory
|
||||
owner=mysql
|
||||
group=mysql
|
||||
recurse=yes
|
||||
|
||||
- name: SERVICE | Start MySQL
|
||||
service: name=mysql state=started
|
||||
|
||||
- name: SHELL | Get master_log_file
|
||||
command: awk '{ print $1 }' {{ mysql_binlog_info }}
|
||||
register: master_log_file
|
||||
|
||||
- name: SHELL | Get master_log_pos
|
||||
command: awk '{ print $2 }' {{ mysql_binlog_info }}
|
||||
register: master_log_pos
|
||||
|
||||
- name: SHELL | Get master GTID
|
||||
command: awk '{ print $3 }' {{ mysql_binlog_info }}
|
||||
register: master_gtid
|
||||
|
||||
- name: SET_FACT | master_log_file
|
||||
set_fact:
|
||||
mysql_master_log_file: "{{ master_log_file.stdout }}"
|
||||
mysql_master_log_pos: "{{ master_log_pos.stdout }}"
|
||||
mysql_master_gtid: "{{ master_gtid.stdout }}"
|
||||
|
||||
- name: FILE | Delete dump
|
||||
file: path={{ mysql_backup_dir }} state=absent
|
||||
delegate_to: "{{ mysql_slave_import_from }}"
|
||||
when: mysql_slave_import_flush_dump
|
||||
|
||||
14
tasks/replication/slave/mariadb_gtid.yml
Normal file
14
tasks/replication/slave/mariadb_gtid.yml
Normal file
@@ -0,0 +1,14 @@
|
||||
---
|
||||
|
||||
# Need this hack before:
|
||||
# - https://github.com/ansible/ansible-modules-extras/issues/817
|
||||
# - https://mariadb.com/kb/en/mariadb/global-transaction-id/#switching-an-existing-old-style-slave-to-use-gtid
|
||||
|
||||
- name: MYSQL_REPLICATION | Stop slave
|
||||
mysql_replication: mode=stopslave
|
||||
|
||||
- name: COMMAND | Migrate to MariaDB GTID
|
||||
command: mysql -e "CHANGE MASTER TO master_use_gtid=current_pos";
|
||||
|
||||
- name: MYSQL_REPLICATION | Start slave
|
||||
mysql_replication: mode=startslave
|
||||
23
tasks/replication/slave/replication.yml
Normal file
23
tasks/replication/slave/replication.yml
Normal file
@@ -0,0 +1,23 @@
|
||||
---
|
||||
|
||||
- name: MYSQL_REPLICATION | Stop slave
|
||||
mysql_replication: mode=stopslave
|
||||
|
||||
- name: MYSQL_REPLICATION | Configure master host
|
||||
mysql_replication: >
|
||||
mode=changemaster
|
||||
master_host={{ mysql_replication_host }}
|
||||
master_port={{ mysql_replication_port }}
|
||||
master_user={{ mysql_replication_user }}
|
||||
master_password={{ mysql_replication_password }}
|
||||
|
||||
- name: MYSQL_REPLICATION | Change master
|
||||
mysql_replication: >
|
||||
mode=changemaster
|
||||
master_log_file={{ mysql_master_log_file }}
|
||||
master_log_pos={{ mysql_master_log_pos }}
|
||||
when: mysql_master_log_file is defined and mysql_master_log_pos is defined
|
||||
|
||||
- name: MYSQL_REPLICATION | Start slave
|
||||
mysql_replication: mode=startslave
|
||||
|
||||
Reference in New Issue
Block a user