ansible-playbook实战之批量安装lamp
来源:互联网 发布:2016网络大电影 编辑:程序博客网 时间:2024/06/07 14:48
简介
通过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/apachephp=$source_dir/phpmemcached=$source_dir/memcachedinstall_dir={{install_dir}}#Source function library.. /etc/init.d/functions#安装apachecd $apachetar -zxvf apr-1.5.0.tar.gzcd apr-1.5.0./configure --prefix=$install_dir/aprmake && make installif [ $? -ne 0 ];then action "install apr is failed!" /bin/false exit $?ficd ..#tar -zxvf apr-util-1.5.3.tar.gzcd apr-util-1.5.3./configure --prefix=$install_dir/apr-util --with-apr=$install_dir/aprmake && make installif [ $? -ne 0 ];then action "install apr-util is failed!" /bin/false exit $?ficd ..#tar -zxvf pcre-8.36.tar.gzcd pcre-8.36./configure --prefix=$install_dir/pcremake && make installif [ $? -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-somake && make installif [ $? -ne 0 ];then action "install httpd is failed!" /bin/false exit $?ficd ..#unzip symfony.zip -d $install_dircd ..#安装memcachedcd $memcachedtar -zxvf libevent-2.0.22-stable.tar.gzcd libevent-2.0.22-stable./configure --prefix=$install_dir/libeventmake && make installif [ $? -ne 0 ];then action "install libevent is failed!" /bin/false exit $?ficd ..tar -zxvf memcached-1.4.22.tar.gzcd memcached-1.4.22./configure --prefix=$install_dir/memcached --with-libevent=$install_dir/libeventmake && make installif [ $? -ne 0 ];then action "install memcached is failed!" /bin/false exit $?fi#安装magentcd ..ln -s $install_dir/libevent/lib/libevent-2.0.so.5 /lib64/libevent-2.0.so.5tar -zxf magent-0.5.tar.gzsed -i "s#LIBS = -levent#LIBS = -levent -lm -L$install_dir/libevent/lib#g" Makefilesed -i "/LIBS/a INCLUDE=-I$install_dir/libevent/include" Makefilesed -i "1i\#ifndef SSIZE_MAX" ketama.hsed -i "4i\#define SSIZE_MAX 32676" ketama.hsed -i '$i\#endif' ketama.h#编译makemkdir -p $install_dir/magent/binmv magent $install_dir/magent/bin/cd ..#安装phpyum install -y libxml2-devel libjpeg-devel libpng-devel freetype-devel openssl-devel libcurl-devel unzipcd $phprpm -ivh libmcrypt-2.5.7-1.2.el6.rf.x86_64.rpmrpm -ivh libmcrypt-devel-2.5.7-1.2.el6.rf.x86_64.rpmtar -zxvf php-5.4.22.tar.gzcd 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-mysqlmake && make installif [ $? -ne 0 ];then action "install php is failed!" /bin/false exit $?ficp php.ini-production $install_dir/php/etc/php.inised -i '/mime.types/a\ AddType application/x-httpd-php .php' $install_dir/apache/conf/httpd.confsed -i '/mime.types/a\ AddType application/x-httpd-php-source .phps' $install_dir/apache/conf/httpd.confsed -i '/;date.timezone/a date.timezone = "Asia/Shanghai"' $install_dir/php/etc/php.inised -i '/;date.timezone/a date.timezone = PRC' $install_dir/php/etc/php.inicd ../#安装php扩展#安装mono扩展tar -zxvf mongo-1.2.10.tgzcd mongo-1.2.10$install_dir/php/bin/phpize./configure --with-php-config=$install_dir/php/bin/php-config make && make installsed -i '/php_bz2.dll/a extension=mongo.so' $install_dir/php/etc/php.inicd ..#安装memcached扩展cd memcached_extensiontar -zxvf libmemcached-1.0.18.tar.tarcd libmemcached-1.0.18./configure --prefix=$install_dir/libmemcached --with-memcachedmake && make installcd ..tar -zxvf memcached-2.2.0.tgzcd 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-saslmake && make installsed -i '/php_bz2.dll/a extension=memcached.so' $install_dir/php/etc/php.inicd ../../#安装memcache扩展cd memcache_extensiontar -zxvf memcache-2.2.7.tgzcd memcache-2.2.7$install_dir/php/bin/phpize./configure --with-php-config=$install_dir/php/bin/php-config --enable-memcachemake && make installsed -i '/php_bz2.dll/a extension=memcache.so' $install_dir/php/etc/php.inicd ../../#安装soapcd php-5.4.22/ext/soap/$install_dir/php/bin/phpize./configure --enable-soap --with-php-config=$install_dir/php/bin/php-configmake && make installsed -i '/php_bz2.dll/a extension=soap.so' $install_dir/php/etc/php.inicd ../#安装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-configmake && make installsed -i '/php_bz2.dll/a extension=gd.so' $install_dir/php/etc/php.inicd ..#安装mbstring扩展cd mbstring$install_dir/php/bin/phpize./configure --with-php-config=$install_dir/php/bin/php-configmake && make installsed -i '/php_bz2.dll/a extension=mbstring.so' $install_dir/php/etc/php.inicd ..#安装exif扩展cd exif$install_dir/php/bin/phpize./configure --with-php-config=$install_dir/php/bin/php-configmake && make installsed -i '/php_bz2.dll/a extension=exif.so' $install_dir/php/etc/php.inicd ../../../#安装eaccelerator扩展tar -zxvf eaccelerator-eaccelerator-42067ac.tar.gzcd eaccelerator-eaccelerator-42067ac$install_dir/php/bin/phpize./configure --enable-eaccelerator=shared --with-php-config=$install_dir/php/bin/php-configmake && make installsed -i '/php_bz2.dll/a extension=eaccelerator.so' $install_dir/php/etc/php.inimkdir -p $install_dir/eaccelerator_cachechmod -R 777 $install_dir/eaccelerator_cachecat >> $install_dir/php/etc/php.ini <<EOFeaccelerator.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.confsed -i "s/#LoadModule rewrite_module/LoadModule rewrite_module/g" $install_dir/apache/conf/httpd.confsed -i "s/#ServerName www.example.com:80/ServerName localhost:80/g" $install_dir/apache/conf/httpd.confsed -i "s/AllowOverride None/AllowOverride All/g" $install_dir/apache/conf/httpd.confsed -i "s/AllowOverride none/AllowOverride All/g" $install_dir/apache/conf/httpd.confzsed -i "s/Require all denied/Require all granted/g" $install_dir/apache/conf/httpd.confsed -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
- ansible-playbook实战之批量安装lamp
- ansible-playbook实战之批量安装mysql
- ansible-playbook实战之批量部署mysql主从同步
- ansible-playbook批量部署nginx
- ansible之playbook入门
- ansible-playbook实战之部署redis+sentinel+twemproxy
- ansible使用playbook安装nginx
- Ansible之playbook学习一
- ansible之创建可复用playbook
- Ansible playbook
- ansible-playbook
- ansible playbook
- ansible playbook
- Ansible学习之playbook(4)
- 集群运维自动化工具ansible之使用playbook安装zabbix客户端
- ansible-playbook 安装配置zabbix iptables selinux ntp alipay源
- ansible playbook最佳实践
- ansible-playbook简介
- WEB_03_JavaScript_第一讲
- Servlet和CGI的区别
- Mybatis 中$与#的区别
- unity 压缩图片
- selvert 小解
- ansible-playbook实战之批量安装lamp
- English学习工具和资料
- log4j基本原理
- Go的并发
- 输入两个正整数m和n,求其最大公约数和最小公倍数
- leetcode--17. Letter Combinations of a Phone Number
- MarkDown的一些小使用
- Linux 虚拟机虚拟网卡问题导致无法连接问题
- Java xml编程