1. Create a MySQL playbook structure
[Email protected] roles]# pwd/etc/ansible/roles[[email protected] roles]# mkdir-p Mysql_install/{files,handlers,meta , Tasks,templates,vars}[[email protected] roles]# tree mysql_install/mysql_install/├──files├──handlers├──meta├── Tasks├──templates└──vars
2. Create the VARs you need to use
[Email protected] roles]# cat/etc/ansible/roles/mysql_install/vars/main.ymlmysql_version:mysql-5.5.37
3. Create a Remote Installation script
[[email protected] 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 &NBsp; -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&nbsP;/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 Installation complete--------+" echo "+---------------------------+" fi #/etc/init.d/ Mysqld stop} install_mysql
4. Create a task
Copying files
[[email protected] roles]# cat/etc/ansible/roles/mysql_install/tasks/copy.yml-name:copy MySQL source code to client C opy: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
Unzip the installation
[[email protected] roles]# cat/etc/ansible/roles/mysql_install/tasks/install.yml-name:install mysql Shell:/bin/sh/r oot/mysql_install.sh
Create an include file
[Email protected] roles]# Cat/etc/ansible/roles/mysql_install/tasks/main.yml-include:copy.yml-include: Install.yml
The final playbook structure is
[Email protected] 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│└──my Sql_install.sh└──vars└──main.yml
5. Create a MySQL playbook configuration file
[Email protected] ~]# cat/etc/ansible/mysql_server_install.yml-hosts:mysql remote_user:root gather_facts:false ro Les:-Mysql_install
This article is from the "ly36843" blog, please be sure to keep this source http://ly36843.blog.51cto.com/3120113/1671314
Operation and maintenance Automation Ansible Playbook installation of MySQL