1.构建安装目录
mkdir -p /ansible/roles/mysql/{defaults,files,handlers,meta,tasks,templates,vars}
defaults 默认寻找路径
tasks 存放playbooks路径
files 存放文件和脚本,copy模块文件搜索路径
templates 模版存放路径
handlers notify调用部分playbook存放路径
vars roles内变量存放路径
2.文件目录结构
3.playbooks & Shell
/etc/ansible/roles/mysql/files/install_mysql.sh脚本内容为:
#!/bin/bash
#
DBDIR=‘/data/mysql/‘
PASSWD=‘fanshine123‘
[ -d $DBDIR ] || mkdir $DBDIR -p
id mysql &> /dev/null
if [ $? -ne 0 ];then
useradd mysql -s /sbin/nologin -M
fi
chown -R mysql.mysql $DBDIR
cd /tmp/
tar xf mysql-5.6.13.tar.gz
cd mysql-5.6.13
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=$DBDIR \
-DMYSQL_UNIX_ADDR=$DBDIR/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DEXTRA_CHARSETS=all \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_READLINE=1 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EMBEDDED_SERVER=1
if [ $? != 0 ];then
echo "make error!"
exit 1
fi
make && make install
if [ $? -ne 0 ];then
echo "install mysql is failed!" && /bin/false
fi
sleep 2
ln -s /usr/local/mysql/bin/* /usr/bin/
cp -f /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
cp -f /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod 700 /etc/init.d/mysqld
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=$DBDIR --user=mysql
if [ $? -ne 0 ];then
echo "install mysql is failed!" && /bin/false
fi
/etc/init.d/mysqld start
if [ $? -ne 0 ];then
echo "install mysql is failed!" && /bin/false
fi
chkconfig --add mysqld
chkconfig mysqld on
/usr/local/mysql/bin/mysql -e "update mysql.user set password=password(‘$PASSWD‘) where host=‘localhost‘ and user=‘root‘;"
/usr/local/mysql/bin/mysql -e "update mysql.user set password=password(‘$PASSWD‘) where host=‘127.0.0.1‘ and user=‘root‘;"
/usr/local/mysql/bin/mysql -e "delete from mysql.user where user=‘‘;"
/usr/local/mysql/bin/mysql -e "flush privileges;"
if [ $? -eq 0 ];then
echo "ins_done"
fi
/etc/ansible/roles/mysql/tasks/main.yml文件内容为:
- name: copy mysql to client
copy: src=mysql-5.6.13.tar.gz dest=/tmp/mysql-5.6.13.tar.gz
- name: copy install_script to client
copy: src=install_mysql.sh dest=/tmp/install_mysql.sh owner=root group=root mode=755
- name: install mysql
shell: /bin/bash /tmp/install_mysql.sh
/etc/ansible/mysql.yml文件内容为:
- hosts: webserver
remote_user: root
roles:
- mysql
4.执行安装
[root@Centos ansible]# ansible-playbook mysql.yml --syntax-check --检查语法
[root@Centos ansible]# ansible-playbook mysql.yml --执行安装
原文地址:http://sumail.blog.51cto.com/7402899/1963212