• ansible部署MySQL主从


    添加主机清单

    [root@ansible /etc/ansible]#vim hosts
    ...
    [mysql]
    node1
    node2
    
    [mysql_master]
    node1
    
    [mysql_slave]
    node2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    创建mariadb角色

    [root@ansible /etc/ansible]#cd roles/
    [root@ansible /etc/ansible/roles]#ansible-galaxy init mariadb
    - Role php was created successfully
    
    • 1
    • 2
    • 3

    编写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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    编写执行文件

    [root@ansible ansible]# cat m.yml 
    ---
    - name: mariadb
      hosts: mysql
      roles:
        - mariadb
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    执行

    [root@ansible ansible]# ansible-playbook m.yml
    
    PLAY [mariadb] *******************************************************************************************************************************************
    
    TASK [Gathering Facts] *********************************************************************************************
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    Unity/C# 把时间转换成时间戳的方法
    promise原理
    grafana可视化
    RHCSA 文件的上传下载(Linux-Windows)
    设计模式学习笔记(二十二)解释器模式及其实现
    SpringRetry
    Java 简单实现令牌桶
    python入门函数讲解(简单明了,一分钟掌握一个)
    ThreadLocal原理和使用场景
    TP5 模型更新的返回值、返回值的判断以及所使用的SQL
  • 原文地址:https://blog.csdn.net/m0_56681539/article/details/127793909