脚本案例
第一步:yum install -y ansible
第二步:编辑andible主机清单文件
echo -e "10.0.0.156\n10.0.0.157\n10.0.0.158" >>/etc/ansible/hosts
第三步:编写shell脚本
#!/bin/bash
num=$(echo 10.0.0.15{6..8}|xargs -n1)
INSTALL=install_php.sh
for IP in $num
do
scp -rp /server/scripts/$INSTALL root@$IP:/server/scripts &>/dev/null
ansible $IP -m command -a "/bin/sh /server/scripts/$INSTALL" &>/dev/null
done
=====================================================================
[root@www /server/scripts]# cat install_php.sh
#!/bin/bash
NGINX_V=1.18.0
PHP_V=7.2.20
TMP_DIR=/tmp
INSTALL_DIR=/usr/local
PWD_C=$PWD
echo
echo -e "\tMenu\n"
echo -e "1. Install Nginx"
echo -e "2. Install PHP"
echo -e "3. Install MySQL"
echo -e "4. Deploy LNMP"
echo -e "9. Quit"
function command_status_check() {
if [ $? -ne 0 ]; then
echo $1
exit
fi
}
function install_nginx() {
cd $TMP_DIR
yum install -y gcc gcc-c++ make openssl-devel pcre-devel wget
wget http://nginx.org/download/nginx-${NGINX_V}.tar.gz
tar zxf nginx-${NGINX_V}.tar.gz
cd nginx-${NGINX_V}
./configure --prefix=$INSTALL_DIR/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-stream
command_status_check "Nginx - 平台环境检查失败!"
make -j 4
command_status_check "Nginx - 编译失败!"
make install
command_status_check "Nginx - 安装失败!"
mkdir -p $INSTALL_DIR/nginx/conf/vhost
#alias cp=cp ; cp -rf $TMP_DIR/nginx-${NGINX_V}/nginx.conf $INSTALL_DIR/nginx/conf
rm -rf $INSTALL_DIR/nginx/html/*
echo "ok" > $INSTALL_DIR/nginx/html/status.html
echo '<?php echo "ok"?>' > $INSTALL_DIR/nginx/html/status.php
$INSTALL_DIR/nginx/sbin/nginx
command_status_check "Nginx - 启动失败!"
}
function install_php() {
cd $TMP_DIR
yum install -y gcc gcc-c++ make zlib zlib-devel pcre pcre-devel \
libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel \
glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel \
e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel \
openldap openldap-devel nss_ldap openldap-clients openldap-servers \
wget http://docs.php.net/distributions/php-${PHP_V}.tar.gz
tar zxf php-${PHP_V}.tar.gz
cd php-${PHP_V}
./configure --prefix=$INSTALL_DIR/php \
--with-config-file-path=$INSTALL_DIR/php/etc \
--exec-prefix=$INSTALL_DIR/php --with-mysqli --with-mysql\
--with-pdo-mysql --with-gd --bindir=$INSTALL_DIR/php/bin \
--sbindir=$INSTALL_DIR/php/sbin --includedir=$INSTALL_DIR/php/include \
--libdir=$INSTALL_DIR/php/lib/php --mandir=$INSTALL_DIR/php/php/man \
--with-curl --with-zlib --enable-opcache --with-jpeg-dir --with-png-dir \
--with-openssl --enable-mbstring --enable-fpm --enable-hash \
command_status_check "PHP - 平台环境检查失败!"
make -j 4
command_status_check "PHP - 编译失败!"
make install
command_status_check "PHP - 安装失败!"
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
ln -s /usr/local/php/bin/php /usr/local/bin
/etc/init.d/php-fpm start
command_status_check "PHP - 启动失败!"
}
read -p "请输入编号:" number
case $number in
1)
install_nginx;;
2)
install_php;;
3)
install_mysql;;
4)
install_nginx
install_php
;;
9)
exit;;
esac
=======================================================================================================