ansible部署MySQL主从

添加主机清单

[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 }}

编写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] *********************************************************************************************
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值