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
發佈了179 篇原創文章 · 獲贊 299 · 訪問量 61萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章