Ansible之角色基础服务MySQL安装

2023-04-24 17:09:06 浏览数 (2)

Ansible批量部署编译安装MySQL

本文是通过ansible-playbook的roles功能实现批量编译安装mysql-5.7.31和初始化,使用Ansible角色来编译安装MySQL,来对之前学的做总结。

思路: 安装MySQL的编译环境 下载文件、解压、创建软连接 初始化 使用到yum、user、get_url、file、unarchive、shell等模块。

环境准备:

代码语言:javascript复制
[root@ansbile01 ~/roles]$ mkdir mysql/{vars,tasks,templates,handlers,files} -p 

创建启动MySQL用户

代码语言:javascript复制
[root@ansbile01 ~/roles]$ cat mysql/tasks/user.yml 
- name: Create MySQL group
  group: name={{ group }} gid={{ gid }} system=yes
- name: Create MySQL user
  user: name={{ user }} group={{ group }} uid={{ uid }} system=yes shell=/sbin/nologin create_home=no

安装依赖软件

代码语言:javascript复制
[root@ansible01 ~/roles/mysql/tasks]$cat packages.yml 
- name: installed MySQL packages
  yum: name={{ packages }} state=present
  vars:
    packages:
      - libaio-devel
      - MySQL-python

下载MySQL、创建软连接

代码语言:javascript复制
[root@ansbile01 ~/roles]$ cat mysql/tasks/download.yml 
- name: download MySQL
  get_url: dest={{ download }} url=https://downloads.mysql.com/archives/get/p/23/file/{{ mysql_ver|default('mysql-5.7.31-linux-glibc2.12-x86_64') }}.tar.gz force=no

- name: tar xf MySQL
  unarchive: src={{ download }}/{{ mysql_ver|default('mysql-5.7.31-linux-glibc2.12-x86_64') }}.tar.gz dest={{ download }} copy=no creates={{ install }}
  
- name: linke mysql dir
  file: 
    src: "{{ download }}/{{ mysql_ver|default('mysql-5.7.31-linux-glibc2.12-x86_64') }}"
    dest: "{{ install }}"
    state: link
- name: Create data dir.
  file:
    path: "{{ data_mysql }}"
    state: directory
    owner: "{{ user }}"
    group: "{{ group }}"

初始化安装MySQL

代码语言:javascript复制
[root@ansbile01 ~/roles]$ cat mysql/tasks/install.yml 
- name: initialize mysql data
  command: >
    ./bin/mysqld --initialize-insecure
    --user={{ user }}
    --basedir={{ install }}
    --datadir={{ data_mysql }}
  args:
    chdir: "{{ install }}"
    creates: "{{ data_mysql }}/mysql"
  changed_when: yes

添加MySQL环境

代码语言:javascript复制
[root@ansbile01 ~/roles]$ cat mysql/tasks/profile.yml 
- name: MySQL profile
   lineinfile:
      dest: /etc/profile
      line: "export PATH={{ install }}/bin/:$PATH"
      state: present
      backup: yes

准备MySQL配置文件

代码语言:javascript复制
[root@ansbile01 ~/roles]$ cat mysql/tasks/conf.yml
- name: Configure MySQL
  template: src=my.conf.j2 dest=/etc/my.conf
准备templates模板
代码语言:javascript复制
[root@ansible01 ~/roles]$cat mysql/templates/my.conf.j2 
[mysqld]
user={{ user }}
basedir={{ install }}
datadir={{ data_mysql }}
port=3306
socket=/tmp/mysql.sock
[client]
socket=/tmp/mysql.sock

启动MySQL

代码语言:javascript复制
[root@ansible01 ~/roles]$cat mysql/tasks/start.yml
- name: copy start mysql script
  copy:
    src: "{{ install }}/support-files/mysql.server"
    dest: "/etc/init.d/mysqld"
    mode: 0650
    remote_src: yes
  notify: reload mysql

- name: started mysqld
  command: service mysqld start

初始化MySQL数据库

代码语言:javascript复制
[root@ansible01 ~/roles]$cat mysql/tasks/init.yml
---
- name: Set Root password
  mysql_user:
    user: "{{ dbuser }}"
    password: "{{ dbpass }}"
    host: "localhost"
    update_password: always


- name: Drop database {{ testdb|default('test') }}
  mysql_db:
    name: "{{ testdb|default('test') }}"
    login_user: "{{ dbuser }}"
    login_password: "{{ dbpass }}"
    state: absent
    run_once: true

- name: Create {{ newdb }} Databases
  mysql_db:
    name: "{{ newdb }}"
    login_user: "{{ dbuser }}"
    login_password: "{{ dbpass }}"
    state: present
    run_once: true
    encoding: utf8mb4

- name: Create {{ newuser }} user
  mysql_user:
    login_user: "{{ dbuser }}"
    login_password: "{{ dbpass }}"
    name: "{{ newuser }}"
    host: "{{ newhost }}"
    password: "{{ newpass }}"
    priv: "{{ newpriv|default('*.*:ALL')}}"
    state: present

环境变量准备

代码语言:javascript复制
[root@ansible01 ~/roles]$cat mysql/vars/main.yml 
# MySQL user
group: mysql
user: mysql
gid: 306
uid: 306

# MySQL vars
#
download: "/data"
install: "/usr/local/mysql"

data_mysql: "/data/3306/data"
dbuser: root
dbpass: "123456"
# testdb: test
newdb:
  - zabbix
newuser: zabbix
newhost: "10.1.1.%"
newpass: "zabbix"
newpriv: "zabbix.*:insert,update,delete,select"

tasks主体

代码语言:javascript复制
[root@ansible01 ~/roles]$cat mysql/tasks/main.yml
- include_tasks: user.yml
- include_tasks: packages.yml
- include_tasks: download.yml
- include_tasks: install.yml
- include_tasks: profile.yml
- include_tasks: conf.yml
- include_tasks: start.yml
- include_tasks: init.yml

编写playbook

代码语言:javascript复制
[root@ansible01 ~/roles]$cat mysql.yml 
- hosts: dbserver
  roles:
    - role: mysql

遇到问题

ansbile管理MySQL 8.0 版本修改root密码时,会出现caching_sha2_password报错,修改配置文件default-authentication-plugin=mysql_native_password创建新用户没有问题。

mysql_user官方解释

代码语言:javascript复制
MySQL server installs with default login_user of 'root' and no password. To secure this user as part of an idempotent playbook, you must
       create at least two tasks: the first must change the root user's password, without providing any login_user/login_password details. The
       second must drop a ~/.my.cnf file containing the new root credentials. Subsequent runs of the playbook will then succeed by reading the
       new credentials from the file.
     * Currently, there is only support for the `mysql_native_password` encrypted password hash module.
     * Requires the PyMySQL (Python 2.7 and Python 3.X) or MySQL-python (Python 2.X) package on the remote host. The Python package may be
       installed with apt-get install python-pymysql (Ubuntu; see [apt]) or yum install python2-PyMySQL (RHEL/CentOS/Fedora; see [yum]). You
       can also use dnf install python2-PyMySQL for newer versions of Fedora; see [dnf].
     * Both `login_password' and `login_user' are required when you are passing credentials. If none are present, the module will attempt to
       read the credentials from `~/.my.cnf', and finally fall back to using the MySQL default login of 'root' with no password.

不知谁能解决一下?

0 人点赞