设为首页 收藏本站
查看: 639|回复: 0

[经验分享] ansible 自动化安装apache

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-11-24 08:52:05 | 显示全部楼层 |阅读模式
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
#vim  meta/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参数不配置也可以
#cat  tasks/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 [web] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [10.0.90.24]
ok: [10.0.90.25]
TASK: [common | install initializtion require software] ***********************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_install | Copy Apache software to client] ***********************
changed: [10.0.90.25] => (item=httpd-2.4.17.tar.gz)
changed: [10.0.90.24] => (item=httpd-2.4.17.tar.gz)
changed: [10.0.90.24] => (item=libiconv.tar.gz)
changed: [10.0.90.25] => (item=libiconv.tar.gz)
changed: [10.0.90.24] => (item=apr.tar.gz)
changed: [10.0.90.25] => (item=apr.tar.gz)
changed: [10.0.90.25] => (item=apr-util.tar.gz)
changed: [10.0.90.24] => (item=apr-util.tar.gz)
TASK: [apache_install | create apache user in client] *************************
changed: [10.0.90.25]
changed: [10.0.90.24]
TASK: [apache_install | uncompression apache software to client] **************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_install | uncompression apr software to client] *****************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_install | uncompression apr-util software to client] ************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_install | copy apache sysv start service script to client] ******
changed: [10.0.90.25]
changed: [10.0.90.24]
TASK: [apache_install | copy apache config to Client] *************************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_install | create lib install dir] *******************************
ok: [10.0.90.24]
ok: [10.0.90.25]
TASK: [apache_install | check apache Iconv in client] *************************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_install | install apache iconv in client] ***********************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_install | check lib in config in client] ************************
failed: [10.0.90.24] => {"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: [10.0.90.25] => {"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: [apache_install | install apache Iconv in client] ***********************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_install | create apache Dir] ************************************
changed: [10.0.90.24] => (item=/usr/local/apache/conf/vhost)
changed: [10.0.90.25] => (item=/usr/local/apache/conf/vhost)
TASK: [apache_install | start apache service in client] ***********************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_install | add boot start apache service in client] **************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_install | delete apache compression software in client] *********
changed: [10.0.90.24]
changed: [10.0.90.25]
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 [web] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [10.0.90.25]
ok: [10.0.90.24]
TASK: [apache_delete | stop httpd service in client] **************************
changed: [10.0.90.25]
changed: [10.0.90.24]
TASK: [apache_delete | delete boot start in client] ***************************
changed: [10.0.90.25]
changed: [10.0.90.24]
TASK: [apache_delete | delete apache Dir in client] ***************************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_delete | delete apr Dir in client] ******************************
changed: [10.0.90.24]
changed: [10.0.90.25]
TASK: [apache_delete | delete apr-util Dir in client] *************************
changed: [10.0.90.25]
changed: [10.0.90.24]
TASK: [apache_delete | delete apache service script in client] ****************
changed: [10.0.90.25]
changed: [10.0.90.24]
TASK: [apache_delete | delete apache user] ************************************
changed: [10.0.90.24]
changed: [10.0.90.25]
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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-142864-1-1.html 上篇帖子: Ansible Conditionals 下篇帖子: Ansible基本部署&&常用模块
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表