Ansible二进制部署MySQL 5.7.36

Ansible二进制部署MySQL 5.7.36

gitee链接:https://gitee.com/jiayu997/ansible/attach_files/1010740/download/ansible_mysql_57.tar.gz

目录结构

[root@boy ansible-example]# tree /root/ansible-example/
/root/ansible-example/
├── ansible.cfg
├── hosts
├── install_mysql_57.yml
└── roles
    └── mysql_install_57
        ├── files
        │   └── mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
        ├── tasks
        │   ├── init.yml
        │   ├── main.yml
        │   └── mysql.yml
        ├── templates
        │   ├── my.cnf.j2
        │   └── mysqld.service.j2
        └── vars
            └── main.yml

安装过程

[root@boy ansible-example]# ansible-playbook install_mysql_57.yml 

PLAY [MySQL 5.7.36 二进制安装] **************************************************************************************************************************

TASK [mysql_install_57 : include_tasks] ************************************************************************************************************
included: /root/ansible-example/roles/mysql_install_57/tasks/init.yml for 192.168.0.12

TASK [mysql_install_57 : Stop Firewalld] ***********************************************************************************************************
changed: [192.168.0.12]

TASK [mysql_install_57 : Setenforce] ***************************************************************************************************************
[WARNING]: SELinux state temporarily changed from 'enforcing' to 'permissive'. State change will take effect next reboot.
changed: [192.168.0.12]

TASK [mysql_install_57 : Init Dependency] **********************************************************************************************************
changed: [192.168.0.12] => (item=MySQL-python)
changed: [192.168.0.12] => (item=libaio-devel)

TASK [mysql_install_57 : include_tasks] ************************************************************************************************************
included: /root/ansible-example/roles/mysql_install_57/tasks/mysql.yml for 192.168.0.12

TASK [mysql_install_57 : Create MySQL User] ********************************************************************************************************
changed: [192.168.0.12]

TASK [mysql_install_57 : Check MySQL Package] ******************************************************************************************************
ok: [192.168.0.12]

TASK [mysql_install_57 : Copy MySQL Package] *******************************************************************************************************
changed: [192.168.0.12]

TASK [mysql_install_57 : Modify Package Privilege] *************************************************************************************************
changed: [192.168.0.12]

TASK [mysql_install_57 : Export MySQL Path] ********************************************************************************************************
changed: [192.168.0.12]

TASK [mysql_install_57 : Create MySQL Data Dir] ****************************************************************************************************
changed: [192.168.0.12]

TASK [mysql_install_57 : Create MySQL LOG Dir] *****************************************************************************************************
changed: [192.168.0.12]

TASK [mysql_install_57 : Copy MySQL Configuration] *************************************************************************************************
changed: [192.168.0.12]

TASK [mysql_install_57 : Copy MySQL Service Configuration] *****************************************************************************************
changed: [192.168.0.12]

TASK [mysql_install_57 : Check MySQL Init Status] **************************************************************************************************
ok: [192.168.0.12]

TASK [mysql_install_57 : Init MySQL] ***************************************************************************************************************
changed: [192.168.0.12]

TASK [mysql_install_57 : Enable MySQL SERVICE] *****************************************************************************************************
changed: [192.168.0.12]

TASK [mysql_install_57 : Configure ROOT PASSWORD] **************************************************************************************************
[WARNING]: Module did not set no_log for update_password
changed: [192.168.0.12]

TASK [mysql_install_57 : MySQL Service Status] *****************************************************************************************************
ok: [192.168.0.12] => {
    "msg": "MySQL Service Status OK"
}

TASK [mysql_install_57 : MySQL Service Status] *****************************************************************************************************
skipping: [192.168.0.12]

PLAY RECAP *****************************************************************************************************************************************
192.168.0.12               : ok=19   changed=14   unreachable=0    failed=0    skipped=1    rescued=0    ignored=0  

install_mysql_57.yml

[root@boy ansible-example]# cat install_mysql_57.yml 
---
- name: MySQL 5.7.36 二进制安装
  gather_facts: False
  hosts: mysql
  roles:
  - mysql_install_57

main.yml

[root@boy tasks]# cat main.yml 
---
- include_tasks: init.yml
- include_tasks: mysql.yml

init.yml

[root@boy tasks]# cat init.yml 
---
- name: Stop Firewalld
  service: name=firewalld enabled=no state=stopped 
- name: Setenforce 
  selinux: state=disabled
- name: Init Dependency
  yum: name={{item}} state=present
  loop:
  - MySQL-python
  - libaio-devel

mysql.yml

[root@boy tasks]# cat mysql.yml 
---
- block:
  - name: Create MySQL User
    user: name={{MySQL_USER}} shell=/sbin/nologin state=present create_home=False
  - name: Check MySQL Package
    stat: path={{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}}
    register: RE
  - name: Copy MySQL Package
    unarchive: src={{MySQL_Package}} dest={{MySQL_INSTALL_PATH}} remote_src=no
    when: RE.stat.exists != True
  - name: Modify Package Privilege
    file: path={{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}} state=directory owner={{MySQL_USER}} group={{MySQL_USER}} recurse=yes mode=0755
  - name: Export MySQL Path
    lineinfile: path={{MySQL_PATH}} regexp=="export.*PATH=\$PATH:{{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}}/bin/" line="export PATH=$PATH:{{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}}/bin/"
  - name: Create MySQL Data Dir
    file: path={{MySQL_DATA_PATH}} state=directory owner={{MySQL_USER}} group={{MySQL_USER}} mode=0755 recurse=yes
  - name: Create MySQL LOG Dir
    file: path="{{MySQL_LOG_PATH}}" state=directory owner={{MySQL_USER}} group={{MySQL_USER}} mode=0755 recurse=yes
  - name: Copy MySQL Configuration
    template: src={{MySQL_CON_J}} dest={{MySQL_CON_F}}
  - name: Copy MySQL Service Configuration
    template: src=mysqld.service.j2 dest={{MySQL_SERVICE}}
  - name: Check MySQL Init Status
    stat: path={{MySQL_DATA_PATH}}/auto.cnf
    register: RE
  - name: Init MySQL
    shell: "{{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}}/bin/mysqld --initialize-insecure --user={{MySQL_USER}} --basedir={{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}} --datadir={{MySQL_DATA_PATH}}"
    when: RE.stat.exists != True
  - name: Enable MySQL SERVICE
    systemd: name=mysqld enabled=yes state=started
  - block:
    - name: Configure ROOT PASSWORD
      mysql_user: login_user=root login_port={{MySQL_PORT}} login_password="" user=root password={{MySQL_ROOT_PASSWORD}} update_password=always login_unix_socket={{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}}/mysql.sock
    rescue:
    - name: CHECK ROOT LOGIN
      mysql_db: login_user=root login_port={{MySQL_PORT}} login_password={{MySQL_ROOT_PASSWORD}} login_unix_socket={{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}}/mysql.sock name=mysql state=present
      register: RE
    always:
    - name: MySQL Service Status
      debug: msg="MySQL Service Status OK"
      when: RE.failed == false
    - name: MySQL Service Status
      debug: msg="MySQL Service Status Faild, Now will uninstall MySQL"
      when: RE.failed == true
  # 服务无法启动、无法连接等原因出现异常,安装文件全部删除
  rescue:
  - name: STOP MySQL SERVICE
    systemd: name=mysqld eanbled=no state=stoped
    ignore_errors: True
  - name: REMOVE CONFIGURE
    file: path={{MySQL_CON_F}} state=absent
  - name: REMOVE SERVICE
    file: path={{MySQL_SERVICE}} state=absent
  - name: REMOVE LOG
    file: path={{MySQL_LOG_PATH}} state=absent
  - name: REMOVE PACKAGE
    file: path={{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}} state=absent
  - name: REMOVE MYSQL DATA
    file: path={{MySQL_DATA_PATH}} state=absent
  - name: REMOVE mysql USER
    user: name={{MySQL_USER}} state=absent remove=yes

vars

[root@boy mysql_install_57]# cat vars/main.yml 
---
# MySQL 二进制安装包:./ansible-example/roles/mysql_install_57/files
MySQL_Package: mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz

# MySQL安装位置
MySQL_INSTALL_PATH: /opt

# MySQL data存放位置
MySQL_DATA_PATH: /opt/MySQL

# MySQL 运行用户
MySQL_USER: mysql

# MySQL 环境变量写入文件
MySQL_PATH: /etc/profile

# MySQL jinja2配置文件
MySQL_CON_J: my.cnf.j2

# MySQL Service 文件
MySQL_SERVICE: /usr/lib/systemd/system/mysqld.service

# MySQL 配置文件存放位置
MySQL_CON_F: /etc/my.cnf

# MySQL LOG位置
MySQL_LOG_PATH: /var/log/mysql

# MySQL PORT
MySQL_PORT: 3306

# MySQL ROOT PASSWORD
MySQL_ROOT_PASSWORD: 'kylin@123'

# SERVER_ID
SERVER_ID: 1

# MySQL Bin log
MySQL_BIN_LOG: "yes"

# MySQL Relay log
MySQL_RELAY_LOG: "yes"

# MySQL Slow log
MySQL_SLOW_LOG: "yes"

my.cnf.j2

[root@boy templates]# cat my.cnf.j2 
[client]
port={{MySQL_PORT}}
socket={{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}}/mysql.sock
default-character-set=utf8

[mysqld]
basedir={{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}}
datadir={{MySQL_DATA_PATH}}
port={{MySQL_PORT}}
socket={{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}}/mysql.sock
user={{MySQL_USER}}
sql_mode="STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"
log-error={{MySQL_LOG_PATH}}/mysqld.log
expire_logs_days=7

{% if MySQL_BIN_LOG == "yes" %}
server_id={{SERVER_ID}}
log-bin={{MySQL_LOG_PATH}}/mysql-bin
binlog-format=Row
max_binlog_size=500M
gtid_mode=on
enforce_gtid_consistency=on
{% endif %}

{% if MySQL_RELAY_LOG == "yes" %}
relay-log={{MySQL_LOG_PATH}}/relay-log
{% endif %}

{% if MySQL_SLOW_LOG == "yes" %}
slow_query_log=ON
slow_query_log_file={{MySQL_LOG_PATH}}/slow_query.log
long_query_time=1
{% endif %}
[root@boy templates]# cat mysqld.service.j2 
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart={{MySQL_INSTALL_PATH}}/{{MySQL_Package[:-7]}}/bin/mysqld --defaults-file={{MySQL_CON_F}}
LimitNOFILE = 5000

你可能感兴趣的:(Ansible,linux,centos)