243eqd 发表于 2015-11-24 08:52:05

ansible 自动化安装apache

ansible 自动化配置安装apache
一、准备工作
1、下载httpd、php、mysql ,我本次实验使用的版本如下:
http   2.4.17
php      5.6.15
mysql    5.6.19二进制版本
apr      1.5.2
apr-util 1.5.4
libiconv 1.14
2、在ansible服务端开始安装
编译安装apr
#cd /usr/local/src
#tar xf apr-1.5.2.tar.gz
#cd apr-1.5.2
#./configure --prefix=/usr/local/apr--如果提示如下错误(不知道忽略这种错误会如何,没测试):
………………
error info:rm: cannot remove `libtoolT': No such file or directory
………………
解决方法:
#vim configure --找到$RM "$cfgfile" 这行, 将其注释掉
#$RM "$cfgfile" ,然后重新执行configure 就不会报错了
#make && make install

编译安装apr-util
#cd /usr/local/src
#tar xf apr-util-1.5.4.tar.gz
#cd apr-util-1.5.4
#./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
#make && make install

#tar xf libiconv-1.14.tar.gz
#cd libiconv-1.14
#./configure --prefix=/usr/local/
#make && make install
然后拷贝libiconv的so文件到一个目录
#mkdir /usr/local/libiconv
#cd /usr/local/libiconv
#cp /usr/local/lib/libiconv.la ./
#cp /usr/local/lib/libiconv.so.2.5.1 ./
#cp /usr/local/lib/preloadable_libiconv.so ./
创建软连接
#ln -sv libiconv.so.2.5.1 libiconv.so
#ln -sv libiconv.so.2.5.1 libiconv.so.2
然后打包:
#tar -zcf libiconv.tar.gz ./*
#mv libiconv.tar.gz /tmp

安装httpd
安装依赖包:
#yum install pcre pcre-devel -y
#tar xf httpd-2.4.17.tar.gz
#cd httpd-2.4.17
#./configure --prefix=/usr/local/apache--enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib \
--with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/
#make
#make install
在httpd.conf配置文件中添加pid文件,找到# least PidFile,添加其下:
PidFile"/var/run/httpd.pid"
为httpd提供SysV服务脚本/etc/rc.d/init.d/httpd,内容如下:
#cat httpd
#!/bin/bash
#
# httpd      Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server.It is used to serve \
#             HTML files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd.pid

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/httpd ]; then
      . /etc/sysconfig/httpd
fi

# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0

start() {
      echo -n $"Starting $prog: "
      LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
      RETVAL=$?
      echo
      [ $RETVAL = 0 ] && touch ${lockfile}
      return $RETVAL
}

stop() {
        echo -n $"Stopping $prog: "
        killproc -p ${pidfile} -d 10 $httpd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
      RETVAL=$?
      echo $"not reloading due to configuration syntax error"
      failure $"not reloading $httpd due to configuration syntax error"
    else
      killproc -p ${pidfile} $httpd -HUP
      RETVAL=$?
    fi
    echo
}

# See how we were called.
case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
status)
      status -p ${pidfile} $httpd
        RETVAL=$?
        ;;
restart)
        stop
        start
        ;;
condrestart)
        if [ -f ${pidfile} ] ; then
                stop
                start
        fi
        ;;
reload)
      reload
        ;;
graceful|help|configtest|fullstatus)
        $apachectl $@
        RETVAL=$?
        ;;
*)
        echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
        exit 1
esac

exit $RETVAL
授予权限
#chmod +x /etc/init.d/httpd
启动httpd
#service httpd start
然后在浏览器测试
#http://ip   --出现It works! 的网页,表示httpd安装成功了。

压缩apache目录:
#cd /usr/local/
#tar -zcf httpd-2.4.17.tar.gz apache/
#mv httpd-2.4.17.tar.gz /tmp
压缩apr目录:
#tar -zcf apr.tar.gz apr/
#mv apr.tar.gz /tmp
压缩apr-util目录:
#tar -zcf apr-util.tar.gz apr-util/
#mv apr-util.tar.gz /tmp
二、配置playbook ,通过ansible安装配置远端服务器的httpd
1、规划(实现安装和删除httpd)
安装:
#cd /etc/ansible/roles
#mkdir apache_install/{files,handlers,meta,tasks,templates,vars}
#cd apache_install
#mv /tmp/httpd.tar.gz files/
#mv /tmp/libiconv.tar.gz files/
#mv /tmp/apr.tar.gz files/
#mv /tmp/apr-util.tar.gz files/

配置meta
#vimmeta/main.yml
galaxy_info:
author: liguang xing
description: Install Apache
license: MIT
min_ansible_version: 1.9.4
platforms:
- name: CentOS
    version:
    - 6
categories:
- Service
dependencies: []
配置vars
#vim vars/main.yml
apr_version: 1.5.2
apr_util_version: 1.5.4
libiconv_version: 1.14
apache_version: 2.4.17
apache_web_dir: /usr/local/apache/htdocs
apache_log: /usr/local/apache/logs/
apache_vhost: /usr/local/apache/conf/vhost
apache_port: 80
apache_user: www

配置tasks
#cat tasks/copy.yml
- name: Copy Apache software to client
copy: src={{ item }} dest=/tmp/ owner=root group=root
with_items:
    - httpd-{{ apache_version }}.tar.gz
    - libiconv.tar.gz
    - apr.tar.gz
    - apr-util.tar.gz
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: create apache user in client
user: name={{ apache_user }} state=present createhome=no shell=/sbin/nologin
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: uncompression apache software to client
shell: tar xf /tmp/httpd-{{ apache_version }}.tar.gz -C /usr/local/
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: uncompression apr software to client
shell: tar xf /tmp/apr.tar.gz -C /usr/local/
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: uncompression apr-util software to client
shell: tar xf /tmp/apr-util.tar.gz -C /usr/local/
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: copy apache sysv start service script to client
template: src=httpd dest=/etc/init.d/httpd owner=root group=root mode=0755
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: copy apache config to Client
template: src=httpd.conf dest=/usr/local/apache/conf/httpd.conf owner={{ apache_user }} group={{ apache_user }} mode=0644
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

注:when参数不配置也可以
#cattasks/delete.yml
- name: delete apache compression software in client
shell: rm -f /tmp/httpd-{{ apache_version }}.tar.gz /tmp/libiconv.tar.gz /tmp/apr.tar.gz /tmp/apr-util.tar.gz
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
#cat tasks/install.yml
- name: create lib install dir
file: dest=/usr/local/lib state=directory
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: check apache Iconv in client
shell: find / -name "libiconv.so.2"|grep -Ev "tmp|soft"|wc -l
register: apache_iconv
ignore_errors: True
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: install apache iconv in client
shell: tar xf /tmp/libiconv.tar.gz -C /usr/local/lib/
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6 and apache_iconv|int ==0
- name: check lib in config in client
shell: grep -c /usr/local/lib/ /etc/ld.so.conf
register: apache_lib
ignore_errors: True
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: install apache Iconv in client
shell: echo "/usr/local/lib/" >> /etc/ld.so.conf;/sbin/ldconfig
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6 and apache_iconv|int ==0
- name: create apache Dir
file: dest={{ item }} state=directory
with_items:
    - "{{ apache_vhost }}"
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: start apache service in client
shell: /etc/init.d/httpd start
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: add boot start apache service in client
shell: chkconfig --add httpd;chkconfig httpd on
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
# cat tasks/main.yml
- include: copy.yml
- include: install.yml
- include: delete.yml

将httpd启动脚本和httpd.conf配置文件copy一份到templates目录

2、配置common
#cd /etc/ansible/roles
#mkdir -pv common/{meta,tasks}
#cat meta/main.yml
galaxy_info:
author: liguang xing
description: Install initializtion Software
license: MIT
min_ansible_version: 1.9.4
platforms:
- name: CentOS
    versions:
    - 5
    - 6
- name: Ubuntu
    versions:
    - precise
categories:
- system
dependencies: []

#cat tasks/main.yml
- name: install initializtion require software
shell: yum -y install make cmake bc gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel nss_ldap openldap openldap-devel openldap-clients libxslt-devel libevent-devel ntp libtool-ltdl bison libtool vim-enhanced wget readline-devel libyaml-devel patch telnet pcre pcre-devel
poll: 0

3、创建安装脚本
#cd /etc/ansible/roles
#cat apache_install.yml
---
- hosts: "{{host}}"
remote_user: "{{user}}"
gather_facts: True
roles:
    - common
    - apache_install

检查语法:
# ansible-playbook apache_install.yml --syntax-check
playbook: apache_install.yml
没错误开始执行
#ansible-playbook apache_install.yml --extra-vars "host=web user=root"
PLAY ********************************************************************
GATHERING FACTS ***************************************************************
ok:
ok:
TASK: ***********************
changed:
changed:
TASK: ***********************
changed: => (item=httpd-2.4.17.tar.gz)
changed: => (item=httpd-2.4.17.tar.gz)
changed: => (item=libiconv.tar.gz)
changed: => (item=libiconv.tar.gz)
changed: => (item=apr.tar.gz)
changed: => (item=apr.tar.gz)
changed: => (item=apr-util.tar.gz)
changed: => (item=apr-util.tar.gz)
TASK: *************************
changed:
changed:
TASK: **************
changed:
changed:
TASK: *****************
changed:
changed:
TASK: ************
changed:
changed:
TASK: ******
changed:
changed:
TASK: *************************
changed:
changed:
TASK: *******************************
ok:
ok:
TASK: *************************
changed:
changed:
TASK: ***********************
changed:
changed:
TASK: ************************
failed: => {"changed": true, "cmd": "grep -c /usr/local/lib/ /etc/ld.so.conf", "delta": "0:00:00.002203", "end": "2015-11-23 18:12:47.338012", "rc": 1, "start": "2015-11-23 18:12:47.335809", "warnings": []}
stdout: 0
...ignoring
failed: => {"changed": true, "cmd": "grep -c /usr/local/lib/ /etc/ld.so.conf", "delta": "0:00:00.002412", "end": "2015-11-23 18:12:47.349201", "rc": 1, "start": "2015-11-23 18:12:47.346789", "warnings": []}
stdout: 0
...ignoring
TASK: ***********************
changed:
changed:
TASK: ************************************
changed: => (item=/usr/local/apache/conf/vhost)
changed: => (item=/usr/local/apache/conf/vhost)
TASK: ***********************
changed:
changed:
TASK: **************
changed:
changed:
TASK: *********
changed:
changed:
PLAY RECAP ********************************************************************
10.0.90.24               : ok=18   changed=16   unreachable=0    failed=0   
10.0.90.25               : ok=18   changed=16   unreachable=0    failed=0   

然后可以到90.24和90.25这两台客户端服务器上检查apache的安装情况,进行测试。

4、删除apache
#cd /etc/ansible/roles
#mkdir -pv apache_delete/{meta,tasks,vars}
配置yml文件
#cat apache_delete/meta/main.yml
galaxy_info:
author: liguang xing
description: Delete Apache
license: MIT
min_ansible_version: 1.9.4
platforms:
- name: CentOS
    versions:
    - 6
categories:
- Service
dependencies: []

# cat apache_delete/tasks/delete.yml
- name: stop httpd service in client
service: name=httpd state=stopped
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: delete boot start in client
shell: chkconfig --del httpd
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: delete apache Dir in client
shell: rm -rf /usr/local/apache
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: delete apr Dir in client
shell: rm -rf /usr/local/apr
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: delete apr-util Dir in client
shell: rm -rf /usr/local/apr-util
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: delete apache service script in client
shell: rm -f /etc/init.d/httpd
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: delete apache user
shell: userdel www
ignore_errors: yes
when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

# cat apache_delete/tasks/main.yml
- include: delete.yml

5、创建删除脚本
#cd /etc/ansible/roles
#cat apache_delete.yml
---
- hosts: "{{host}}"
remote_user: "{{user}}"
gather_facts: True
roles:
    - apache_delete

检查语法:
# ansible-playbook apache_delete.yml --syntax-check
playbook: apache_delete.yml
没有错误,开始执行:
# ansible-playbook apache_delete.yml --extra-vars "host=web user=root"
PLAY ********************************************************************
GATHERING FACTS ***************************************************************
ok:
ok:
TASK: **************************
changed:
changed:
TASK: ***************************
changed:
changed:
TASK: ***************************
changed:
changed:
TASK: ******************************
changed:
changed:
TASK: *************************
changed:
changed:
TASK: ****************
changed:
changed:
TASK: ************************************
changed:
changed:
PLAY RECAP ********************************************************************
10.0.90.24               : ok=8    changed=7    unreachable=0    failed=0   
10.0.90.25               : ok=8    changed=7    unreachable=0    failed=0   


页: [1]
查看完整版本: ansible 自动化安装apache