ansible-playbook实战之批量安装lamp,


简介

通过ansible-playbook批量编译安装lamp(apache+php及扩展)并进行初始化(自动加载php模块,修改mpm、日志轮储等),后续我们只需修改站点文件即可。

ansible-playbook配置思路:
1.通过vars中的main.yml配置变量,主要为源码存放目录及安装目录
2.通过tasks中的copy.yml将源码文件传输到异地服务器上源码存放目录
3.通过tasks中的install.yml调用模板lamp_install.sh,将lamp安装到变量中定义的安装目录
4.通过tasks中的main.yml调用copy模块和install模块
5.通过lamp.yml调用剧本(playbook):lamp_install

playbook的目录结构

[root@test ansible]# cd /etc/ansible/
[root@test ansible]# mkdir -p roles/lamp_install/{files,handlers,meta,tasks,templates,vars}
[root@test ansible]# tree /etc/ansible
├── ansible.cfg
├── hosts
├── lamp.yml
├── log
│   └── ansible.log
├── roles
│   ├── lamp_install
│   │   ├── files
│   │   │   ├── apache
│   │   │   │   ├── apr-1.5.0.tar.gz
│   │   │   │   ├── apr-util-1.5.3.tar.gz
│   │   │   │   ├── cHost.conf
│   │   │   │   ├── httpd-2.4.7.tar.gz
│   │   │   │   ├── httpd-mpm.conf
│   │   │   │   ├── pcre-8.36.tar.gz
│   │   │   │   ├── rewrite.conf
│   │   │   │   └── symfony.zip
│   │   │   ├── memcached
│   │   │   │   ├── libevent-2.0.22-stable.tar.gz
│   │   │   │   ├── magent-0.5.tar.gz
│   │   │   │   └── memcached-1.4.22.tar.gz
│   │   │   └── php
│   │   │       ├── eaccelerator-eaccelerator-42067ac.tar.gz
│   │   │       ├── libmcrypt-2.5.7-1.2.el6.rf.x86_64.rpm
│   │   │       ├── libmcrypt-devel-2.5.7-1.2.el6.rf.x86_64.rpm
│   │   │       ├── memcached_extension
│   │   │       │   ├── libmemcached-1.0.18.tar.tar
│   │   │       │   └── memcached-2.2.0.tgz
│   │   │       ├── memcache_extension
│   │   │       │   └── memcache-2.2.7.tgz
│   │   │       ├── mongo-1.2.10.tgz
│   │   │       └── php-5.4.22.tar.gz
│   │   ├── handlers
│   │   ├── meta
│   │   ├── tasks
│   │   │   ├── copy.yml
│   │   │   ├── install.yml
│   │   │   └── main.yml
│   │   ├── templates
│   │   │   └── lamp_install.sh
│   │   └── vars
│   │       └── main.yml

说明:
files:存放需要同步到异地服务器的源码文件及配置文件;
handlers:当资源发生变化时需要进行的操作,若没有此目录可以不建或为空;
meta:角色定义可留空;
tasks:lamp安装过程成需要进行的执行的任务;
templates:用于执行lamp安装的模板文件,一般为脚本;
vars:本次安装定义的变量

具体操作

1.创建lamp角色文件,用于调用lamp_install

[root@test  ansible]# vim lamp.yml
- hosts: test
  remote_user: root
  gather_facts: False
  roles:
    - lamp_install

2.创建变量文件

[root@test ansible]# cd /etc/ansible/roles/lamp_install/vars
[root@test ansible]#vim main.yml
#源码存放目录
source_dir: /home/ap/src/lamp/
#源码安装目录
install_dir: /home/ap/

3.创建任务文件

[root@test ansible]# cd /etc/ansible/roles/mysql_install/tasks
[root@test ansible]# vim copy.yml
#复制php组件至目标服务器
- name: copy php dir to client
  copy: src=php dest={{source_dir}} owner=root group=root
#复制apache组件至目标服务器
- name: copy apache dir to client
  copy: src=apache dest={{source_dir}} owner=root group=root
#复制memcached组件至目标服务器
- name: copy memcached dir to client
  copy: src=memcached dest={{source_dir}} owner=root group=root
#复制模板文件至目标服务器
- name: copy lamp script to client
  template: src=lamp_install.sh dest={{source_dir}} owner=root group=root mode=0775
[root@test ansible]# vim install.yml
#执行模板文件进行安装
- name: install lamp
  shell: bash {{source_dir}}/lamp_install.sh
[root@test ansible]# vim main.yml
#引用copy、install模块
- include: copy.yml
- include: install.yml

注意:
a.copy如果复制目录,需要加上递归参数,recurse;
b.copy如果复制目录,没有目录将会在目标服务器上创建;
c.copy如果复制文件到目标服务器的某一个目录下,需要在dest参数上加上/home/ap/src/lamp/,而不是/home/ap/lamp,否则ansible将会把文件复制为lamp,而不是放在lamp目录下。
4.编写模板脚本

[root@test ansible]#cd /etc/ansible/roles/lamp_install/templates
#!/bin/bash
#author:yanggd
#comment:lamp环境部署
source_dir={{source_dir}}
apache=$source_dir/apache
php=$source_dir/php
memcached=$source_dir/memcached
install_dir={{install_dir}}
#Source function library.
. /etc/init.d/functions
#安装apache
cd $apache
tar -zxvf apr-1.5.0.tar.gz
cd apr-1.5.0
./configure --prefix=$install_dir/apr
make && make install
if [ $? -ne 0 ];then
    action "install apr is failed!"  /bin/false
    exit $?
fi
cd ..
#
tar -zxvf apr-util-1.5.3.tar.gz
cd apr-util-1.5.3
./configure --prefix=$install_dir/apr-util --with-apr=$install_dir/apr
make && make install
if [ $? -ne 0 ];then
    action "install apr-util is failed!"  /bin/false
    exit $?
fi
cd ..
#
tar -zxvf pcre-8.36.tar.gz
cd pcre-8.36
./configure --prefix=$install_dir/pcre
make && make install
if [ $? -ne 0 ];then
    action "install pcre is failed!"  /bin/false
    exit $?
fi
#
cd ..
tar -zxvf httpd-2.4.7.tar.gz 
cd httpd-2.4.7
./configure --prefix=$install_dir/apache --with-apr=$install_dir/apr --with-apr-util=$install_dir/apr-util --with-pcre=$install_dir/pcre --enable-modules=mall --enable-rewrite --enable-mpms-shared=all --with-mpm=event --enable-v4-mapped --enable-so
make && make install
if [ $? -ne 0 ];then
    action "install httpd is failed!"  /bin/false
    exit $?
fi
cd ..
#
unzip symfony.zip -d $install_dir
cd ..

#安装memcached
cd $memcached
tar -zxvf libevent-2.0.22-stable.tar.gz
cd libevent-2.0.22-stable
./configure --prefix=$install_dir/libevent
make && make install
if [ $? -ne 0 ];then
    action "install libevent is failed!"  /bin/false
    exit $?
fi
cd ..
tar -zxvf memcached-1.4.22.tar.gz
cd memcached-1.4.22
./configure --prefix=$install_dir/memcached --with-libevent=$install_dir/libevent
make && make install
if [ $? -ne 0 ];then
    action "install memcached is failed!"  /bin/false
    exit $?
fi
#安装magent
cd ..
ln -s $install_dir/libevent/lib/libevent-2.0.so.5 /lib64/libevent-2.0.so.5
tar -zxf magent-0.5.tar.gz
sed -i "s#LIBS = -levent#LIBS = -levent -lm -L$install_dir/libevent/lib#g" Makefile
sed -i "/LIBS/a INCLUDE=-I$install_dir/libevent/include" Makefile
sed -i "1i\#ifndef SSIZE_MAX" ketama.h
sed -i "4i\#define SSIZE_MAX 32676" ketama.h
sed -i '$i\#endif' ketama.h
#编译
make
mkdir -p $install_dir/magent/bin
mv magent $install_dir/magent/bin/

cd ..
#安装php
yum install -y libxml2-devel libjpeg-devel libpng-devel freetype-devel openssl-devel libcurl-devel unzip
cd $php
rpm -ivh libmcrypt-2.5.7-1.2.el6.rf.x86_64.rpm
rpm -ivh libmcrypt-devel-2.5.7-1.2.el6.rf.x86_64.rpm
tar -zxvf php-5.4.22.tar.gz
cd php-5.4.22
./configure --prefix=$install_dir/php --with-apxs2=$install_dir/apache/bin/apxs --with-openssl --with-mcrypt --with-zlib --with-libxml-dir --enable-xml --with-freetype-dir --with-curl --enable-sockets --with-config-file-path=$install_dir/php/etc --with-mysql --with-mysqli --with-pdo-mysql
make && make install
if [ $? -ne 0 ];then
    action "install php is failed!"  /bin/false
    exit $?
fi
cp php.ini-production $install_dir/php/etc/php.ini
sed -i '/mime.types/a\    AddType application/x-httpd-php .php' $install_dir/apache/conf/httpd.conf
sed -i '/mime.types/a\    AddType application/x-httpd-php-source .phps' $install_dir/apache/conf/httpd.conf
sed -i '/;date.timezone/a date.timezone = "Asia/Shanghai"' $install_dir/php/etc/php.ini
sed -i '/;date.timezone/a date.timezone = PRC' $install_dir/php/etc/php.ini

cd ../
#安装php扩展
#安装mono扩展
tar -zxvf mongo-1.2.10.tgz
cd mongo-1.2.10
$install_dir/php/bin/phpize
./configure --with-php-config=$install_dir/php/bin/php-config 
make && make install
sed -i '/php_bz2.dll/a extension=mongo.so' $install_dir/php/etc/php.ini
cd ..
#安装memcached扩展
cd memcached_extension
tar -zxvf libmemcached-1.0.18.tar.tar
cd libmemcached-1.0.18
./configure --prefix=$install_dir/libmemcached --with-memcached
make && make install
cd ..
tar -zxvf memcached-2.2.0.tgz
cd memcached-2.2.0
$install_dir/php/bin/phpize
./configure --enable-memcached --with-php-config=$install_dir/php/bin/php-config --with-libmemcached-dir=$install_dir/libmemcached --disable-memcached-sasl
make && make install
sed -i '/php_bz2.dll/a extension=memcached.so' $install_dir/php/etc/php.ini
cd ../../
#安装memcache扩展
cd memcache_extension
tar -zxvf memcache-2.2.7.tgz
cd memcache-2.2.7
$install_dir/php/bin/phpize
./configure --with-php-config=$install_dir/php/bin/php-config --enable-memcache
make && make install
sed -i '/php_bz2.dll/a extension=memcache.so' $install_dir/php/etc/php.ini
cd ../../
#安装soap
cd php-5.4.22/ext/soap/
$install_dir/php/bin/phpize
./configure --enable-soap  --with-php-config=$install_dir/php/bin/php-config
make && make install
sed -i '/php_bz2.dll/a extension=soap.so' $install_dir/php/etc/php.ini
cd ../
#安装gd扩展
cd gd
$install_dir/php/bin/phpize
./configure --with-png-dir --with-freetype-dir --with-jpeg-dir --with-gd --with-php-config=$install_dir/php/bin/php-config
make && make install
sed -i '/php_bz2.dll/a extension=gd.so' $install_dir/php/etc/php.ini
cd ..
#安装mbstring扩展
cd mbstring
$install_dir/php/bin/phpize
./configure --with-php-config=$install_dir/php/bin/php-config
make && make install
sed -i '/php_bz2.dll/a extension=mbstring.so' $install_dir/php/etc/php.ini
cd ..
#安装exif扩展
cd exif
$install_dir/php/bin/phpize
./configure --with-php-config=$install_dir/php/bin/php-config
make && make install
sed -i '/php_bz2.dll/a extension=exif.so' $install_dir/php/etc/php.ini
cd ../../../
#安装eaccelerator扩展
tar -zxvf eaccelerator-eaccelerator-42067ac.tar.gz
cd eaccelerator-eaccelerator-42067ac
$install_dir/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=$install_dir/php/bin/php-config
make && make install
sed -i '/php_bz2.dll/a extension=eaccelerator.so' $install_dir/php/etc/php.ini
mkdir -p $install_dir/eaccelerator_cache
chmod -R 777 $install_dir/eaccelerator_cache
cat >> $install_dir/php/etc/php.ini <<EOF
eaccelerator.shm_size="64"
eaccelerator.cache_dir="$install_dir/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter="!*.yml.php"
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
eaccelerator.allowed_admin_path = "/web/smallcity.cityhouse.cn"
EOF

##prepare apache
#修改 http.conf
sed -i "s/#LoadModule rewrite_module/LoadModule rewrite_module/g" $install_dir/apache/conf/httpd.conf
sed -i "s/#ServerName www.example.com:80/ServerName localhost:80/g" $install_dir/apache/conf/httpd.conf
sed -i "s/AllowOverride None/AllowOverride All/g" $install_dir/apache/conf/httpd.conf
sed -i "s/AllowOverride none/AllowOverride All/g" $install_dir/apache/conf/httpd.confz
sed -i "s/Require all denied/Require all granted/g" $install_dir/apache/conf/httpd.conf
sed -i "s#logs/access_log#| $install_dir/apache/bin/rotatelogs $install_dir/apache/logs/access_log.%Y-%m-%d 86400 480#g" $install_dir/apache/conf/httpd.conf
#prepare *.conf
\cp $apache/http-mpm.conf $install_dir/apache/conf/extra/
\cp $apache/rewrite.conf $install_dir/apache/conf/
\cp $apache/cHost.conf $install_dir/apache/conf/

#开机启动
echo "$install_dir/apache/bin/apachectl -k start" >> /etc/rc.local

安装脚本功能:
1)安装apache及相关组件
2)安装memcached、magent
3)安装php及相关扩展mogo、memcached、memcache、soap、gd、mbstring、exif、eaccelerator,并添加至php.ini;在apache中引用php
4)修改http.conf,包括添加rewrite模块,修改AllowOverride等
5)修改http-mpm.conf
6)添加rewrite.conf
7)添加站点配置文件

执行playbook

#检查文件
[root@test ansible]# ansible-playbook -C lamp.yml
#执行playbook
[root@test ansible]# ansible-playbook lamp.yml

相关内容

    暂无相关文章