ansible-playbook批量部署MySQL

通过ansible-playbook,以二进制包方式批量部署mysql。

  • 将所有部署mysql主机分为dbserver组:
# vim /etc/ansible/hosts
[dbserver]
192.168.30.128
192.168.30.129
192.168.30.130
  • 创建管理目录:
#  mkdir -p mysql/roles/mysql_install/{files,handlers,meta,tasks,templates,vars}

# cd mysql/

说明:

files:存放需要同步到异地服务器的源码文件及配置文件; 
handlers:当资源发生变化时需要进行的操作,若没有此目录可以不建或为空; 
meta:存放说明信息、说明角色依赖等信息,可留空; 
tasks:mysql安装过程成需要进行的执行的任务; 
templates:用于执行mysql安装的模板文件,一般为脚本; 
vars:本次安装定义的变量
# tree .
.
├── mysql.yml
└── roles
    └── mysql_install
        ├── files
        │   └── mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz              #可提前下载好mysql包放到files下
        ├── handlers
        ├── meta
        ├── tasks
        │   ├── copy.yml
        │   ├── install.yml
        │   ├── main.yaml
        │   └── prepare.yml
        ├── templates
        │   ├── change_passwd.sh
        │   ├── my.cnf
        │   └── mysqld.service
        └── vars
            └── main.yml

8 directories, 10 files

  • 创建mysql入口文件,用来调用mysql_install:
# vim mysql.yml 

#用于批量安装MySQL
---
- hosts: dbserver
  remote_user: root
  gather_facts: True

  roles:
    - mysql_install
  • 创建变量:
# vim roles/mysql_install/vars/main.yml

#定义mysql安装中的变量
MYSQL_VER: 5.7.25
MYSQL_VER_MAIN: "{{ MYSQL_VER.split('.')[0] }}.{{ MYSQL_VER.split('.')[1] }}"

DOWNLOAD_URL: https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-{{ MYSQL_VER_MAIN }}/mysql-{{ MYSQL_VER }}-linux-glibc2.12-x86_64.tar.gz
MYSQL_USER: mysql
MYSQL_PORT: 3306
MYSQL_PASSWD: 123456789
SOURCE_DIR: /software
BASE_DIR: /usr/local/mysql
DATA_DIR: /data/mysql
  • 创建模板文件:

mysql配置文件

# vim roles/mysql_install/templates/my.cnf

[client]
port    = {{ MYSQL_PORT }}
socket = {{ BASE_DIR }}/tmp/mysql.sock

[mysql]
default-character-set=utf8

[mysqld]
default-storage-engine=INNODB
character_set_server=utf8
explicit_defaults_for_timestamp
basedir={{ BASE_DIR }}
datadir={{ DATA_DIR }}
socket={{ BASE_DIR }}/tmp/mysql.sock
log_error = {{ BASE_DIR }}/log/error.log

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

mysql服务文件

# vim roles/mysql_install/templates/mysqld.service

[Unit]
Description=MySQL Server
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql
ExecStart={{ BASE_DIR }}/bin/mysqld --defaults-file=/etc/my.cnf

#连接数限制
LimitNOFILE=65535
LimitNPROC=65535

#Restart配置可以在进程被kill掉之后,让systemctl产生新的进程,避免服务挂掉
#Restart=always
PrivateTmp=false

更改数据库root密码脚本

# vim roles/mysql_install/templates/change_passwd.sh

#!/bin/bash
#该脚本用于更改数据库root密码

passwd={{ MYSQL_PASSWD }}
n=`grep "{{ BASE_DIR }}/bin" /etc/profile |wc -l`

if [ $n -eq 0 ]
then
        echo "export PATH=$PATH:{{ BASE_DIR }}/bin" >> /etc/profile
        source /etc/profile
else
        source /etc/profile
fi

{{ BASE_DIR }}/bin/mysql -uroot -D mysql -e "UPDATE user SET authentication_string=PASSWORD("$passwd") WHERE user='root';"

{{ BASE_DIR }}/bin/mysql -uroot -e "FLUSH PRIVILEGES;"

{{ BASE_DIR }}/bin/mysql -uroot -p$passwd -e "grant all privileges on *.* to root@'%'  identified by '$passwd';"
  • 环境准备prepare.yml:
# vim roles/mysql_install/tasks/prepare.yml
- name: 关闭firewalld
  service: name=firewalld state=stopped enabled=no

- name: 临时关闭 selinux
  shell: "setenforce 0"
  failed_when: false

- name: 永久关闭 selinux
  lineinfile:
    dest: /etc/selinux/config
    regexp: "^SELINUX="
    line: "SELINUX=disabled"

- name: 添加EPEL仓库
  yum: name=epel-release state=latest

- name: 安装常用软件包
  yum:
    name:
      - vim
      - lrzsz
      - net-tools
      - wget
      - curl
      - bash-completion
      - rsync
      - gcc
      - unzip
      - git
      - perl-Data-Dumper
      - libaio-devel
    state: latest

- name: 更新系统
  shell: "yum update -y"
  args:
    warn: False
  • 文件拷贝copy.yml:
# vim roles/mysql_install/tasks/copy.yml
- name: 创建mysql用户组
  group: name={{ MYSQL_USER }}  state=present

- name: 创建mysql用户
  user: name={{ MYSQL_USER }}  group={{ MYSQL_USER }}  state=present create_home=False shell=/sbin/nologin

- name: 创建所需目录
  file: name={{ item }} state=directory mode=0755 recurse=yes
  with_items:
  - "{{ SOURCE_DIR }}"
  - "{{ DATA_DIR }}"

- name: 更改目录属主属组
  file: name={{ DATA_DIR }} owner={{ MYSQL_USER }} group={{ MYSQL_USER }}

#当前主机下没有mysql包
- name: 下载mysql包
  get_url: url={{ DOWNLOAD_URL }} dest={{ SOURCE_DIR }} owner={{ MYSQL_USER }} group={{ MYSQL_USER }}

#当前主机file目录下已有mysql包
#- name: 拷贝现有mysql包到所有主机
#  copy: src=mysql-{{ MYSQL_VER }}-linux-glibc2.12-x86_64.tar.gz dest={{ SOURCE_DIR }} owner={{ MYSQL_USER }} group={{ MYSQL_USER }}

- name: 解压mysql包
  unarchive: src={{ SOURCE_DIR }}/mysql-{{ MYSQL_VER }}-linux-glibc2.12-x86_64.tar.gz dest=/usr/local owner={{ MYSQL_USER }} group={{ MYSQL_USER }}

- name: 目录重命名
  shell: "mv /usr/local/mysql-{{ MYSQL_VER }}-linux-glibc2.12-x86_64 {{ BASE_DIR }} && chown -R {{ MYSQL_USER }}:{{ MYSQL_USER }} {{ BASE_DIR }}"

#复制mysql配置文件
- name: 拷贝mysql配置文件
  template: src=my.cnf dest=/etc/my.cnf owner=root group=root

#复制mysql服务文件
- name: 拷贝mysql服务文件
  template: src=mysqld.service dest=/usr/lib/systemd/system/mysqld.service owner=root group=root

#复制更改密码脚本
- name: 拷贝更改密码脚本
  template: src=change_passwd.sh dest={{ SOURCE_DIR }} owner=root group=root

- name: 创建日志目录
  file: name={{ item }} state=directory owner={{ MYSQL_USER }} group={{ MYSQL_USER }} mode=0755 recurse=yes
  with_items:
  - "/var/log/mysql"
  - "/var/run/mysqld"
  - "{{ BASE_DIR }}/tmp"
  - "{{ BASE_DIR }}/log"

- name: 创建错误日志文件
  file: dest={{ BASE_DIR }}/log/error.log state=touch owner={{ MYSQL_USER }} group={{ MYSQL_USER }}
  • mysql初始化install.yml:
# vim roles/mysql_install/tasks/install.yml
#初始化安装mysql
- name: mysql初始化
  shell: "{{ BASE_DIR }}/bin/mysqld --initialize-insecure --user={{ MYSQL_USER }} --basedir={{ BASE_DIR }}  --datadir={{ DATA_DIR }}"

- name: 拷贝启动脚本到/etc下
  copy: src={{ BASE_DIR }}/support-files/mysql.server dest=/etc/init.d/mysql

- name: 修改启动脚本_1
  lineinfile:
    dest: /etc/init.d/mysql
    regexp: "^basedir="
    insertbefore: "^# Default value, in seconds, afterwhich the script should timeout waiting"
    line: "basedir={{ BASE_DIR }}"

- name: 修改启动脚本_2
  lineinfile:
    dest: /etc/init.d/mysql
    regexp: "^datadir="
    insertbefore: "^# Default value, in seconds, afterwhich the script should timeout waiting"
    line: "datadir={{ DATA_DIR }}"

- name: 修改启动脚本_3  
  file: dest=/etc/init.d/mysql state=file mode=0755

- name: 配置环境变量
  shell: " if [ `grep {{ BASE_DIR }}/bin /etc/profile |wc -l` -eq 0 ]; then echo export PATH=$PATH:{{ BASE_DIR }}/bin >> /etc/profile && source /etc/profile; else source /etc/profile; fi"

- name: 启动mysql并开机启动
  shell: "systemctl daemon-reload && systemctl enable mysqld && systemctl start mysqld"

- name: 设置数据库root密码
  shell: "bash {{ SOURCE_DIR }}/change_passwd.sh"
  • 引用文件main.yml:
# vim roles/mysql_install/tasks/main.yml
#引用prepare、copy、install模块
- include: prepare.yml
- include: copy.yml
- include: install.yml
  • 执行安装:
# ansible-playbook mysql.yml
# netstat -lntp |grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      30768/mysqld

mysql安装目录是/usr/local/mysql,数据存放目录是data/mysql,而且数据库root的密码是123456789

测试安装没有问题,如果本地没有下载好的mysql包,安装会慢一点。已存放至个人gitgub:ansible-playbook


你可能感兴趣的:(Ansible)