ansible-playbook一键部署mysql双主集群

 前面的博客中,介绍过mysql的双主集群的搭建,那么ansible就是将之前的手动的操作过程转换成剧本的方式,然后执行剧本,就完成了mysql的双主集群的搭建,具体的搭建过程可看之前的mysql的博客模块,下面是ansible部署mysql双主的文件结构


[root@wyl01 mysql]# tree 
.
├── group_vars
│   └── mysql.yml    # 存放的是所用到的参数
├── hosts            # 主机清单
├── README.txt
├── roles
│   └── mysql
│       ├── files
│       │   └── mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz   # 安装的版本
│       ├── tasks     #目录下面是具体的每一项任务
│       │   ├── add_user.yml                    
│       │   ├── change_slave_to_master.yml
│       │   ├── install_mysql.yml
│       │   ├── main.yml
│       │   └── pre_install.yml
│       └── templates     # 模板文件
│           ├── my.cnf.j2
│           └── mysql.service.j2
└── site.yml   # 执行的入口文件

 1. 查看主机清单和所用到的变量

[root@wyl01 mysql]# cat hosts    #主机清单
[mysql]
wyl02   ansible_ssh_host=192.168.52.129   ansible_ssh_pass=123456  master=true
wyl03   ansible_ssh_host=192.168.52.130   ansible_ssh_pass=123456  slave=true


[root@wyl01 mysql]# cat group_vars/mysql.yml    # 变量,下面有的参数使用是为了多实例部署,是剧本具有高效性
---
mysql_version: mysql-5.7.20-linux-glibc2.12-x86_64
mysql_install_path: /usr/local
mysql_link: mysql
mysql_sock: /tmp/mysql.sock
mysql_port: 3306
mysql_root_passwd: "123456"
master_ip: 192.168.52.129
slave_ip: 192.168.52.130
repl_user: repl
repl_passwd: "123456"

 2 剧本的入口文件

[root@wyl01 mysql]# cat site.yml 
---
- hosts: all
  gather_facts: yes
  roles:
    - mysql

 3 剧本执行的每一项任务

ansible-playbook一键部署mysql双主集群_第1张图片

[root@wyl01 tasks]# cat pre_install.yml    # 安装前的准备工作
- name: install_MySQL-python
  yum:
    name: MySQL-python
    state: latest
  tags:
    - install_MySQL-python
[root@wyl01 tasks]# cat add_user.yml    # mysql的用户
---
- name: add_mysql_user
  user:
    name: mysql
    create_home: no
    shell: /sbin/nologin
  tags:
    - add_mysql_user

 

[root@wyl01 tasks]# cat install_mysql.yml    # 解压,授权,启动,修改初始化密码等操作
---
- name:  unarchive_mysql_tar
  unarchive:
    src:  "{{ mysql_version }}.tar.gz"
    dest: "{{ mysql_install_path }}"
    owner: mysql
    group: mysql
  tags:
    - unarchive_mysql_tar


- name: chown_mysql
  file: 
    dest: "{{ mysql_install_path }}/{{mysql_version}}"
    owner: mysql
    group: mysql
    recurse: yes
  tags:
    - chown_mysql


- name: link_mysql
  file:
    src: "{{ mysql_install_path }}/{{mysql_version}}"
    dest: "{{ mysql_install_path }}/{{ mysql_link }}"
    owner: mysql
    group: mysql
    state: link
  tags:
    - link_mysql



- name: mkdir_log
  file:
    path: "{{ mysql_install_path }}/{{ mysql_link }}/log"
    owner: mysql
    group: mysql
    state: directory
  tags:
    - mkdir_log



- name: touch_mysql.log
  file:
    path: "{{ mysql_install_path }}/{{ mysql_link }}/log/mysqld.log"
    owner: mysql
    group: mysql
    state: touch
  tags:
    - touch_mysql.log


- name: copy_my.cnf
  template:
    src: my.cnf.j2
    dest: /etc/my.cnf
  tags:
    - copy_my.cnf
 


- name: init_mysql
  shell: ./bin/mysqld --initialize  --user=mysql
  args:
    chdir: "{{ mysql_install_path }}/{{ mysql_link }}"
  tags: 
    - init_mysql
    

- name: get_mysql_passwd
  shell: cat log/mysqld.log |grep localhost|awk -F ':' '{print $4}'|cut -c  2-
  register: mysql_init_passwd
  args:  
    chdir: "{{ mysql_install_path }}/{{ mysql_link }}"
  tags: 
    - get_mysql_passwd



- name: dispaly_passwd
  debug: 
    msg: "{{ mysql_init_passwd.stdout }}"
  tags:
    - dispaly_passwd



- name: copy_mysql.server
  copy:
    src: "{{ mysql_install_path }}/{{ mysql_link }}/support-files/mysql.server"
    dest: /etc/init.d/mysql-{{ mysql_port }}.server
    mode: 0755
  tags:
     - copy_mysql.server



- name: add_mysql_systemd
  template:
    src: mysql.service.j2
    dest: /etc/systemd/system/mysql-{{ mysql_port }}.service
  tags:
    - add_mysql_systemd



- name: start_mysql_service
  systemd:
    name: mysql-{{ mysql_port }}
    daemon_reload: yes
    enabled: yes
    state: restarted
  tags:
    - start_mysql_service



- name: alter_passwd
  shell: ./bin/mysqladmin -u root -p'{{mysql_init_passwd.stdout}}' password '{{ mysql_root_passwd }}'
  args:
    chdir: "{{ mysql_install_path }}/{{ mysql_link }}"
  tags:
    - alter_passwd
[root@wyl01 mysql]# cat roles/mysql/tasks/change_slave_to_master.yml     #change master操作 
---
- name: create_user
  mysql_user: 
    login_host: 127.0.0.1
    login_port: "{{ mysql_port }}"
    login_user: root 
    login_password: "{{ mysql_root_passwd }}"
    name: "{{ repl_user }}"
    password: "{{ repl_passwd }}"
    priv: "*.*:ALL"
    state: present 
    host: "%"
  when: master is defined
  tags:
    - create_user



- name: change_slave_to_master1
  mysql_replication:
    login_unix_socket: "{{ mysql_sock }}"
    login_host: 127.0.0.1
    login_port: "{{ mysql_port }}"
    login_user: root     
    login_password: "{{ mysql_root_passwd }}"
    master_host: "{{ master_ip }} " 
    master_user: "{{ repl_user }}" 
    master_password: "{{ repl_passwd }}"
    master_port: "{{ mysql_port }}"
    master_auto_position: "1"
    mode: changemaster
  when: slave is defined
  tags:
    - change_slave_to_master1 


- name: start_slave1
  mysql_replication: 
    login_unix_socket: "{{ mysql_sock }}"
    login_user: root 
    login_host: 127.0.0.1
    login_port: "{{ mysql_port }}"
    login_password: "{{ mysql_root_passwd }}"
    mode: startslave
  tags: 
    - start_slave1


- name: change_slave_to_master2
  mysql_replication:
    login_unix_socket: "{{ mysql_sock }}"
    login_host: 127.0.0.1
    login_port: "{{ mysql_port }}"
    login_user: root
    login_password: "{{ mysql_root_passwd }}"
    master_host: "{{ slave_ip }} "
    master_user: "{{ repl_user }}"
    master_password: "{{ repl_passwd }}"
    master_port: "{{ mysql_port }}"
    master_auto_position: "1"
    mode: changemaster
  when: master is defined
  tags:
    - change_slave_to_master2


- name: start_slave2
  mysql_replication:
    login_unix_socket: "{{ mysql_sock }}"
    login_user: root
    login_host: 127.0.0.1
    login_port: "{{ mysql_port }}"
    login_password: "{{ mysql_root_passwd }}"
    mode: startslave
  tags:
    - start_slave2

- name: get_slave_info
  mysql_replication:
    login_host: 127.0.0.1
    login_user: root
    login_port: 3306
    login_password: "123456"
    mode: getslave
  register: info
  tags:
    - get_slave_info


- name: dispaly_slave
  debug:
    msg: "Slave_SQL_Running={{ info.Slave_IO_Running }}       Slave_SQL_Running={{ info.Slave_SQL_Running }}"
  tags:
    - dispaly_slave

 执行结果如下:

[root@wyl01 mysql]# ansible-playbook  -i hosts  site.yml 

PLAY [all] **********************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************
ok: [wyl03]
ok: [wyl02]

TASK [mysql : install_MySQL-python] *********************************************************************************************************************************************************
ok: [wyl02]
ok: [wyl03]

TASK [mysql : add_mysql_user] ***************************************************************************************************************************************************************
ok: [wyl03]
ok: [wyl02]

TASK [mysql : unarchive_mysql_tar] **********************************************************************************************************************************************************
changed: [wyl03]
changed: [wyl02]

TASK [mysql : chown_mysql] ******************************************************************************************************************************************************************
changed: [wyl03]
changed: [wyl02]

TASK [mysql : link_mysql] *******************************************************************************************************************************************************************
changed: [wyl02]
changed: [wyl03]

TASK [mysql : mkdir_log] ********************************************************************************************************************************************************************
changed: [wyl02]
changed: [wyl03]

TASK [mysql : touch_mysql.log] **************************************************************************************************************************************************************
changed: [wyl02]
changed: [wyl03]

TASK [mysql : copy_my.cnf] ******************************************************************************************************************************************************************
ok: [wyl03]
ok: [wyl02]

TASK [mysql : init_mysql] *******************************************************************************************************************************************************************
changed: [wyl03]
changed: [wyl02]

TASK [mysql : get_mysql_passwd] *************************************************************************************************************************************************************
changed: [wyl03]
changed: [wyl02]

TASK [mysql : dispaly_passwd] ***************************************************************************************************************************************************************
ok: [wyl02] => {
    "msg": "tosEGBto+3xg"
}
ok: [wyl03] => {
    "msg": "&CUQdQ&Qb7S4"
}

TASK [mysql : copy_mysql.server] ************************************************************************************************************************************************************
ok: [wyl03]
ok: [wyl02]

TASK [mysql : add_mysql_systemd] ************************************************************************************************************************************************************
ok: [wyl03]
ok: [wyl02]

TASK [mysql : start_mysql_service] **********************************************************************************************************************************************************
changed: [wyl03]
changed: [wyl02]

TASK [mysql : alter_passwd] *****************************************************************************************************************************************************************
changed: [wyl03]
changed: [wyl02]

TASK [mysql : create_user] ******************************************************************************************************************************************************************
skipping: [wyl03]
changed: [wyl02]

TASK [mysql : change_slave_to_master1] ******************************************************************************************************************************************************
skipping: [wyl02]
changed: [wyl03]

TASK [mysql : start_slave1] *****************************************************************************************************************************************************************
ok: [wyl02]
changed: [wyl03]

TASK [mysql : change_slave_to_master2] ******************************************************************************************************************************************************
skipping: [wyl03]
changed: [wyl02]

TASK [mysql : start_slave2] *****************************************************************************************************************************************************************
changed: [wyl02]
ok: [wyl03]

PLAY RECAP **********************************************************************************************************************************************************************************
wyl02                      : ok=20   changed=12   unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
wyl03                      : ok=19   changed=11   unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   

最后显示sql和i/o进程yes状态,看到两个yes状态表示安装成功

ansible-playbook一键部署mysql双主集群_第2张图片

 

 

 

你可能感兴趣的:(MySQL)