添加主机清单
[root@ansible /etc/ansible]#vim hosts
...
[mysql]
node1
node2
[mysql_master]
node1
[mysql_slave]
node2
创建mariadb角色
[root@ansible /etc/ansible]#cd roles/
[root@ansible /etc/ansible/roles]#ansible-galaxy init mariadb
- Role php was created successfully
编写tasks任务
[root@ansible mariadb]# vim 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: install mariadb
yum:
name: "{
{ packages }}"
vars:
packages:
- mariadb-server
- mariadb
- name: cp config1
template:
src: mastermy.cnf.j2
dest: /etc/my.cnf
shell:
cmd: mysql -uroot -e "grant all privileges on *.* to root@'%' identified by '111';"
- name: master
shell:
cmd: mysql -uroot -e "grant replication slave on *.* to 'user'@'slave' identified by '111';"
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='111';"
when: inventory_hostname in {
{
groups.mysql_slave }}
- name: start slave
shell:
cmd: mysql -uroot -e "start slave;"
when: inventory_hostname in {
{
groups.mysql_slave }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
编写template文件
[root@ansible /etc/ansible/roles/mariadb]#vim templates/mastermy.cnf.j2
[mysqld]
log_bin=mysql-bin
server_id=10
[root@ansible /etc/ansible/roles/mariadb]#vim templates/slavemy.cnf.j2
[mysqld]
log_bin=mysql-bin
server_id=20
编写执行文件
[root@ansible ansible]# cat m.yml
---
- name: mariadb
hosts: mysql
roles:
- mariadb
执行
[root@ansible ansible]# ansible-playbook m.yml
PLAY [mariadb] *******************************************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************