Ansible-Roles应用—安装MySQL

实验环境

192.168.1.1:Ansible

192.168.1.4:MySQL

192.168.1.5:MySQL

实验目的

使用Ansible的角色功能,安装MySQL服务

实验步骤

准备工作

免密登录

首先先做Ansible服务器对被控端的免密登录

192.168.1.1

ssh-keygen # 生成密钥对
# 为两台被控端传输密钥,方便ansible进行管理
ssh-copy-id -i root@192.168.1.4
ssh-copy-id -i root@192.168.1.5

编辑主机清单

vim /etc/ansible/hosts
# 末尾添加
[dbservers]
192.168.1.4
192.168.1.5

创建mysql角色目录

mkdir -p /etc/ansible/roles/mysql/{defaults,files,handlers,meta,tasks,templates,vars}

files目录中

将安装MySQL的所需tar包和文件,都放入/etc/ansible/roles/mysql/files/目录中

ls /etc/ansible/roles/mysql/files/
cmake-3.15.1.tar.gz  mysql-5.6.33.tar.gz

templates目录中

.j2文件的制作

my.cnf.j2模板文件的制作

vim /etc/ansible/roles/mysql/templates/my.cnf.j2
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock
# Disabling symbolib-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=

# 
# include all files from the config directory
#
!includedir /etc/my.cnf.d

vars目录中

设置编写yml需要用到的变量

vim /etc/ansible/roles/mysql/vars/main.yml
cmakedir: cmake-3.15.1
installdir: /usr/src
mysqlcmake: -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all
mysqldir: mysql-5.6.33
mysqlname: mysql

handlers目录中

添加需要调用的handlers

vim /etc/ansible/roles/mysql/handlers/main.yml
- name: start mysqld
  service: name=mysqld state=started
- name: restart mysqld
  service: name=mysqld state=restarted
- name: stop firewalld
  service: name=firewalld state=stopped

tasks目录中

将传统的playbook的一大篇的tasks可以分为好几个大步骤来做,方便以后管理时调用各个yml文件

第一大步骤

编译安装MySQL

vim /etc/ansible/roles/mysql/tasks/install.yml
- name: yilai
  yum: name=ncurses-devel,gcc,gcc-c++
- name: cmake install
  unarchive: src=cmake-3.15.1.tar.gz dest={{installdir}}
- name: make install cmake
  shell: ./configure && gmake && gmake install
  args:
    chdir: /usr/src/{{cmakedir}}
- name: mysql install
  unarchive: src=mysql-5.6.33.tar.gz dest={{installdir}}
- name: make install mysql
  shell: cmake {{mysqlcmake}} && make && make install
  args:
    chdir: /usr/src/{{mysqldir}}

第二大步骤

优化MySQL的命令环境以及创建程序用户

vim /etc/ansible/roles/mysql/tasks/profile.yml
- name: create service user
  user: name=mysql create_home=no shell=/sbin/nologin state=present
- name: mysql directory permissions
  file: path=/usr/local/{{mysqlname}} owner=mysql group=mysql recurse=yes
- name: opt command path
  lineinfile: path=/etc/profile insertbefore=EOF line="PATH=$PATH:/usr/local/mysql/bin"

第三大步骤

设置mysql的启动脚本并授权,将mysql添加为系统服务

vim /etc/ansible/roles/mysql/tasks/startsci.yml
- name: copy mysql start manager script
  copy: src=/usr/local/{{mysqlname}}/support-files/mysql.server dest=/etc/init.d/mysqld remote_src=yes
- name: mysql script permissions
  file: path=/etc/init.d/mysqld mode=0755
- name: add system service
  shell: chkconfig --add mysqld
- name: open system auto start
  shell: chkconfig mysqld on

第四大步骤

初始化数据库做配置文件的调整并初始化数据库

vim /etc/ansible/roles/mysql/tasks/viewconf.yml
- name: template .j2 file
  template: src=my.cnf.j2 dest=/etc/my.cnf
- name: view pid file
  replace: path=/etc/my.cnf regexp='pid-file=' replace='pid-file=/usr/local/mysql/data/{{mysqlname}}.pid'
- name: init mysql data
  shell: /usr/local/{{mysqlname}}/scripts/mysql_install_db --user=mysql --group=mysql --basedir=/usr/local/{{mysqlname}} --datadir=/usr/local/{{mysqlname}}/data
  notify: 
    - start mysqld
    - stop firewalld

最后将四个大步骤整合在一个main.yml

执行顺序将按照文件中的先后顺序执行

vim /etc/ansible/roles/mysql/tasks/main.yml
- import_tasks: install.yml
- import_tasks: profile.yml
- import_tasks: startsci.yml
- import_tasks: viewconf.yml

roles是目录中

nsible-playbook执行的文件

vim /etc/ansible/roles/mysql.yml
- hosts: dbservers
  remote_user: root
  roles:
    - role: mysql # 调用roles/mysql目录中的tasks目录的main.yml

执行mysql.yml

ansible-playbook /etc/ansible/roles/mysql.yml

安装完成后找不到mysql命令

由于ansible使用ssh登录客户端会进入与执行命令环境不一样的shell环境,在yml中我们写了一个/etc/profile文件,在ansible被控端要使用这些命令,也就是登录mysql,需要首先执行source /etc/profile,不然就会报错找不到命令

Ansible-Roles的MySQL目录结构

/etc/ansible/roles/
├── mysql.yml  # 执行文件
└── mysql
    ├── defaults
    ├── files  # 存放copy/script/file/unarchive模块使用的文件
    │   ├── cmake-3.15.1.tar.gz
    │   └── mysql-5.6.33.tar.gz
    ├── handlers # 一般用于重启服务,被notify触发
    │   └── main.yml
    ├── meta
    ├── tasks  # mysql.yml执行的主要任务
    │   ├── install.yml  # 按照camek、mysql的task
    │   ├── main.yml  # 将tasks目录中除main文件之外的yml文件排序执行
    │   ├── profile.yml  # 创建程序用户,授权目录,调整mysql命令变量的task
    │   ├── startsci.yml # 准备启动脚本的task
    │   └── viewconf.yml # 传送.j2文件启动服务的task
    ├── templates # 存放template模块使用的.j2文件
    │   └── my.cnf.j2
    └── vars  # 存放在各个yml中调用的变量值
        └── main.yml