配置主机
[root@ansible ~]# cd /etc/ansible/
[mysql]
node4
node5
[my_master]
node4
[my_slave]
node5
创建角色
[root@ansible ansible]# ansible-galaxy init roles/mysqld
- Role roles/mysqld was created successfully
调用角色配置主从
tasks任务
[root@ansible ansible]# vim roles/mysqld/tasks/main.yml
---
# tasks file for roles/mysqld
- name: set firewalld
service:
name: firewalld
state: stopped
enabled: no
- name: stop selinux
lineinfile:
path: /etc/sysconfig/selinux
regexp: '^SELINUX='
line: 'SELINUX=disabled'
- name: setenforce 0
shell: setenforce 0
- name: install mariadb
yum:
name:
- mariadb-server
- mariadb
state: present
- name: copy moban1
template:
src: my_master.cnf.j2
dest: /etc/my.cnf
when: inventory_hostname in {{ groups.my_master }}
- name: copy moban2
template:
src: my_slave.cnf.j2
dest: /etc/my.cnf
when: inventory_hostname in {{ groups.my_slave }}
- name: restare mariadb
service:
name: mariadb
state: started
enabled: yes
- name: quanxian
shell: mysql -uroot -e "GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY '123456';"
- name: quanxian2
shell: mysql -uroot -e "GRANT REPLICATION SLAVE ON *.* TO 'sb'@'slave' IDENTIFIED BY '123456';"
when: inventory_hostname in {{ groups.my_master }}
- name: quanxian3
shell: mysql -uroot -e "CHANGE MASTER TO MASTER_HOST='192.168.40.103', MASTER_USER='sb', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=154;"
when: inventory_hostname in {{ groups.my_slave }}
- name: start
shell: mysql -uroot -e "start slave;"
when: inventory_hostname in {{ groups.my_slave }}
模板文件
[root@ansible ~]# vim /etc/ansible/roles/mysqld/templates/my_master.cnf.j2
[mysqld]
log_bin=mysql-bin
server_id=66
[root@ansible ~]# vim /etc/ansible/roles/mysqld/templates/my_slave.cnf.j2
[mysqld]
log_bin=mysql-bin
server_id=99
调用mysqld角色
[root@ansible ansible]# vim mysql.yml
---
- name: use mysqld
hosts: mysql
roles:
- mysqld
[root@ansible ansible]# ansible-playbook mysql.yml
PLAY [use mysqld] *******************************************************************************************
TASK [Gathering Facts] **************************************************************************************
ok: [node5]
ok: [node4]
TASK [mysqld : set firewalld] *******************************************************************************
ok: [node4]
ok: [node5]
TASK [mysqld : stop selinux] ********************************************************************************
ok: [node5]
ok: [node4]
TASK [mysqld : setenforce 0] ********************************************************************************
changed: [node4]
changed: [node5]
TASK [mysqld : install mariadb] *****************************************************************************
ok: [node4]
ok: [node5]
TASK [mysqld : copy moban1] *********************************************************************************
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.
Found: inventory_hostname in {{ groups.my_master }}
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.
Found: inventory_hostname in {{ groups.my_master }}
skipping: [node5]
ok: [node4]
TASK [mysqld : copy moban2] *********************************************************************************
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.
Found: inventory_hostname in {{ groups.my_slave }}
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.
Found: inventory_hostname in {{ groups.my_slave }}
skipping: [node4]
ok: [node5]
TASK [mysqld : restare mariadb] *****************************************************************************
changed: [node4]
changed: [node5]
TASK [mysqld : quanxian] ************************************************************************************
changed: [node4]
changed: [node5]
TASK [mysqld : quanxian2] ***********************************************************************************
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.
Found: inventory_hostname in {{ groups.my_master }}
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.
Found: inventory_hostname in {{ groups.my_master }}
skipping: [node5]
changed: [node4]
TASK [mysqld : quanxian3] ***********************************************************************************
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.
Found: inventory_hostname in {{ groups.my_slave }}
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.
Found: inventory_hostname in {{ groups.my_slave }}
skipping: [node4]
changed: [node5]
TASK [mysqld : start] ***************************************************************************************
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.
Found: inventory_hostname in {{ groups.my_slave }}
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.
Found: inventory_hostname in {{ groups.my_slave }}
skipping: [node4]
changed: [node5]
PLAY RECAP **************************************************************************************************
node4 : ok=9 changed=4 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
node5 : ok=10 changed=5 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
查看
[root@node4 ~]# cat /etc/my.cnf
[mysqld]
log_bin=mysql-bin
server_id=66
[root@node5 ~]# cat /etc/my.cnf
[mysqld]
log_bin=mysql-bin
server_id=99
[root@node5 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 10.3.28-MariaDB-log 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: 192.168.40.103
Master_User: sb
Master_Port: 3306
Connect_Retry: 60
Master_Log_File:
Read_Master_Log_Pos: 4
Relay_Log_File: node5-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File:
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes