1.创建mysql的playbook结构
[root@ansible roles]# pwd
/etc/ansible/roles
[root@ansible roles]# mkdir -p mysql_install/{files,handlers,meta,tasks,templates,vars}
[root@ansible roles]# tree mysql_install/
mysql_install/
├── files
├── handlers
├── meta
├── tasks
├── templates
└── vars
2.创建需要用到的vars
[root@ansible roles]# cat /etc/ansible/roles/mysql_install/vars/main.ymlmysql_version: mysql-5.5.37
3.创建远程安装脚本
[root@ansible roles]# cat /etc/ansible/roles/mysql_install/templates/mysql_install.sh#!/bin/bash DATADIR='/data/mysql/data'VERSION='{{mysql_version}}'export LANG=zh_CN.UTF-8 #Source function library.. /etc/init.d/functions #camke install mysql5.5.Xinstall_mysql(){ #read -p "please input a password for root: " PASSWDPASSWD='ly36843' if [ ! -d $DATADIR ];then mkdir -p $DATADIR fi yum install cmake make gcc-c++ bison-devel ncurses-devel -y id mysql &>/dev/null if [ $? -ne 0 ];then useradd mysql -s /sbin/nologin -M fi #useradd mysql -s /sbin/nologin -M #change datadir owner to mysql chown -R mysql.mysql $DATADIR cd #wget http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.38.tar.gz tar xf $VERSION.tar.gz cd $VERSION cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/$VERSION \ -DMYSQL_DATADIR=$DATADIR \ -DMYSQL_UNIX_ADDR=$DATADIR/mysql.sock \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DENABLED_LOCAL_INFILE=ON \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_FEDERATED_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \ -DWITHOUT_PARTITION_STORAGE_ENGINE=1 make && make install if [ $? -ne 0 ];then action "install mysql is failed!" /bin/false exit $? fi sleep 2 #link ln -s /usr/local/$VERSION/ /usr/local/mysql ln -s /usr/local/mysql/bin/* /usr/bin/ #copy config and start file /bin/cp /usr/local/mysql/support-files/my-small.cnf /etc/my.cnf cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld chmod 700 /etc/init.d/mysqld #init mysql /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=$DATADIR --user=mysql if [ $? -ne 0 ];then action "install mysql is failed!" /bin/false exit $? fi #check mysql /etc/init.d/mysqld start if [ $? -ne 0 ];then action "mysql start is failed!" /bin/false exit $? 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 password='';" /usr/local/mysql/bin/mysql -e "flush privileges;" #/usr/local/mysql/bin/mysql -e "select version();" >/dev/null 2>&1 if [ $? -eq 0 ];then echo "+---------------------------+" echo "+------mysql安装完成--------+" echo "+---------------------------+" fi #/etc/init.d/mysqld stop} install_mysql
4.创建任务
复制文件
[root@ansible roles]# cat /etc/ansible/roles/mysql_install/tasks/copy.yml - name: copy mysql source code to client copy: src={{mysql_version}}.tar.gz dest=/root/{{mysql_version}}.tar.gz owner=root group=root- name: copy mysql install script to client template: src=mysql_install.sh dest=/root/mysql_install.sh owner=root group=root mode=0755
解压安装
[root@ansible roles]# cat /etc/ansible/roles/mysql_install/tasks/install.yml- name: install mysql shell: /bin/sh /root/mysql_install.sh
创建包含文件
[root@ansible roles]# cat /etc/ansible/roles/mysql_install/tasks/main.yml- include: copy.yml- include: install.yml
最后的playbook结构是
[root@ansible roles]# tree /etc/ansible/roles/mysql_install//etc/ansible/roles/mysql_install/├── files│ └── mysql-5.5.37.tar.gz├── handlers├── meta├── tasks│ ├── copy.yml│ ├── install.yml│ └── main.yml├── templates│ └── mysql_install.sh└── vars └── main.yml
5.创建mysql的playbook配置文件
[root@ansible ~]# cat /etc/ansible/mysql_server_install.yml- hosts: mysql remote_user: root gather_facts: False roles: - mysql_install
本文出自 “ly36843运维” 博客,请务必保留此出处http://ly36843.blog.51cto.com/3120113/1671314