[db_server]
192.168.1.148
cd /etc/ansible/roles/
mkdir -p mysql_install/{default,files,handlers,meta,tasks,templates,vars}
cd mysql_install/files/
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
vim /etc/ansible/roles/mysql_install/templates/my.cnf.j2
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
explicit_defaults_for_timestamp=true
datadir={{ data_dir }}
basedir={{ install_dir }}
#socket=
# Disabling symbolic-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
log-error=/data/mysql/logs/mysqld.log
pid-file=/data/mysql/run/mysqld.pid
[mysqld_safe]
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
vim /etc/ansible/roles/mysql_install/templates/mysql_install.sh.j2
#!/bin/bash
INSTALL_DIR={{ install_dir }}
DATADIR={{ data_dir }}
SOURCE_DIR={{ source_dir }}
install_mysql(){
PASSWD='123456'
if [ ! -d $DATADIR ];then
mkdir -p $INSTALL_DIR/{data,run,logs}
fi
id mysql &>/dev/null
if [ $? -ne 0 ];then
useradd mysql -s /sbin/nologin -M
fi
#MySQL initialization and startup
cp -p $INSTALL_DIR/support-files/mysql.server /etc/init.d/mysqld
if [ -d $INSTALL_DIR/logs ];then
touch $INSTALL_DIR/logs/mysqld.log
chown -R mysql:mysql $INSTALL_DIR/logs/mysqld.log
else
echo "No logs directory and mysqld.log file!"
exit $?
fi
chown -R mysql:mysql $INSTALL_DIR
chmod -R 755 $INSTALL_DIR
rm -f $DATADIR/*
$INSTALL_DIR/bin/mysqld --initialize --basedir=$INSTALL_DIR --datadir=$DATADIR --user=mysql
/etc/init.d/mysqld start
if [ $? -ne 0 ];then
echo "mysql start is failed!"
exit $?
fi
chkconfig --add mysqld
chkconfig mysqld on
root_pass=`grep 'temporary password' $INSTALL_DIR/logs/mysqld.log | awk '{print $11}'`
$INSTALL_DIR/bin/mysql --connect-expired-password -uroot -p$root_pass -e "alter user 'root'@'localhost' identified by '$PASSWD';"
if [ $? -eq 0 ];then
echo "+---------------------------+"
echo "+------mysql安装完成--------+"
echo "+---------------------------+"
fi
#add path
echo "export PATH=$PATH:$INSTALL_DIR/bin" >> /etc/profile
source /etc/profile
}
install_mysql
cd /etc/ansible/roles/
vim mysql_install.yml
- hosts: db_server
remote_user: root
roles:
- mysql_install
vim mysql_install/vars/main.yml
mysql_version: mysql-5.7.28-linux-glibc2.12-x86_64
install_dir: /data/mysql
data_dir: /data/mysql/data
source_dir: /tmp
vim mysql_install/tasks/copy.yml
- name: 分发mysql安装包
copy: src={{ item }} dest={{ source_dir }}
with_fileglob:
- /etc/ansible/roles/mysql_install/files/*
- name: 分发mysql配置文件 my.cnf
template: src=my.cnf.j2 dest=/etc/my.cnf
- name: 分发mysql_install.sh 自动安装文件
template: src=mysql_install.sh.j2 dest={{ source_dir }}/mysql_install.sh
- name: 解压包mysql安装包
unarchive:
src: /tmp/{{ mysql_version }}.tar.gz
dest: "{{ source_dir }}"
copy: no
- name: 移动mysql解压后的文件夹到指定位置,并重新命名
shell: "mv {{ source_dir }}/{{ mysql_version }} {{ install_dir }}"
vim mysql_install/tasks/install.yml
- name: install mysql
shell: bash {{source_dir}}/mysql_install.sh
vim mysql_install/tasks/main.yml
- include: copy.yml
- include: install.yml
检查playbook作业
ansible-playbook -C mysql_install.yml
运行playbook作业
ansible-playbook mysql_install.yml
大部分的程序员,都是面向百度或者谷歌进行编程的,而网上的资料乱七八糟,有时候找起来让人难受,于是本人无偿进行资料收集的工作,大部分资料都是本人实打实收集的而且测试过,大家不用怀疑准确性,奈何能力有限,免于遗漏,希望读者可以在评论或者私信我,进行改正,大家一起为互联网技术做贡献。
收集资料枯燥无味,如果本文对你有帮助,可以点个赞,这个也是对我最大的鼓励和赞许。
本人行不改名坐不改姓,潮汕的灿灿展
立志在互联网这一行,做出自己的贡献