角色部署MySQL主从
准备工作
[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# vim hosts
...
[mysql]
node1
node2
[mysql_master]
node1
[mysql_slave]
node2
创建mariadb角色
[root@ansible ansible]# cd roles/
[root@ansible roles]# ansible-galaxy init mariadb
- Role mariadb was created successfully
部署mariadb
编写角色模板playbook
[root@ansible roles]# cd mariadb/
[root@ansible mariadb]# vim tasks/main.yml
[root@ansible mariadb]# cat tasks/main.yml
---
# tasks file for mariadb
- name: stop firewalld
service:
name: firewalld
state: stopped
enabled: no
- name: stop selinux
lineinfile:
path: /etc/selinux/config
regexp: 'SELINUX='
line: SELINUX=disabled
- name: stop selinux1
shell:
cmd: setenforce 0
- name: set yum
script: yum.sh
- name: install mariadb
yum:
name: "{{ packages }}"
vars:
packages:
- mariadb-server
- mariadb
- name: cp config1
template:
src: mastermy.cnf.j2
dest: /etc/my.cnf
when: inventory_hostname in {{ groups.mysql_master }}
- name: cp config2
template:
src: slavemy.cnf.j2
dest: /etc/my.cnf
when: inventory_hostname in {{ groups.mysql_master }}
- name: start mariadb
service:
name: mariadb
state: restarted
enabled: yes
- name: grant for root
shell:
cmd: mysql -uroot -e "grant all privileges on *.* to root@'%' identified by 'redhat';"
- name: master
shell:
cmd: mysql -uroot -e "grant replication slave on *.* to 'user'@'slave' identified by 'redhat';"
when: inventory_hostname in {{ groups.mysql_master }}
- name: master
shell:
cmd: mysql -uroot -e "change master to master_host='master',master_user='user',master_password='redhat';"
when: inventory_hostname in {{ groups.mysql_slave }}
- name: start slave
shell:
cmd: mysql -uroot -e "start slave;"
when: inventory_hostname in {{ groups.mysql_slave }}
编写yum.sh脚本
[root@ansible mariadb]# vim files/yum.sh
[root@ansible mariadb]# cat files/yum.sh
#!/bin/bash
rm -rf /etc/yum.repos.d/*
/usr/bin/curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
/usr/bin/sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
/usr/bin/sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
编写template文件
[root@ansible mariadb]# vim templates/mastermy.cnf.j2
[root@ansible mariadb]# cat templates/mastermy.cnf.j2
[mysqld]
log_bin=mysql-bin
server_id=10
[root@ansible mariadb]# vim templates/slavemy.cnf.j2
[root@ansible mariadb]# cat templates/slavemy.cnf.j2
[mysqld]
log_bin=mysql-bin
server_id=20
编写执行playbook
[root@ansible mariadb]# vim mariadb.yml
[root@ansible mariadb]# cat mariadb.yml
---
- name: mariadb
hosts: mysql
roles:
- mariadb
执行
[root@ansible mariadb]# ansible-playbook mariadb.yml
PLAY RECAP **************************************************************************************
node1 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=10 changed=9 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
验证
[root@node2 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 10.3.28-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: master
Master_User: user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File:
Read_Master_Log_Pos: 4
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 4
Relay_Master_Log_File:
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes