利用ansible的playbook实现批量编译安装部署httpd-2.4

[root@cent8_yzl_10 ansible]# cat hosts 

[websrvs]
10.0.0.20
10.0.0.30
[root@cent8_yzl_10 ansible]# cat install_httpd.yml 
---
- hosts: websrvs
  remote_user: root
  vars:
    download_dir: /usr/local/src
    install_dir: /apps/httpd
    httpd_version: httpd-2.4.46
    apr_version: apr-1.7.0
    apr_util_version: apr-util-1.6.1
    httpd_url: https://mirrors.tuna.tsinghua.edu.cn/apache/httpd
    apr_url: https://mirrors.tuna.tsinghua.edu.cn/apache/apr

  tasks:
  - name: install packages
    yum: name=gcc,make,pcre-devel,openssl-devel,expat-devel,bzip2 state=installed
  - name: download httpd file
    unarchive: src="{{ httpd_url }}/{{ httpd_version }}.tar.bz2" dest={{ download_dir }} owner=root remote_src=yes
  - name: download apr file
    unarchive: src="{{ apr_url }}/{{ apr_version }}.tar.bz2" dest={{ download_dir }} owner=root remote_src=yes
  - name: download apr_util file
    unarchive: src="{{ apr_url }}/{{ apr_util_version }}.tar.bz2" dest={{ download_dir }} owner=root remote_src=yes
  - name: prepare apt dir
    shell: chdir={{ download_dir }} mv {{ apr_version }} {{ download_dir }}/{{ httpd_version }}/srclib/apr
  - name: prepare apr_util dir
    shell: chdir={{ download_dir }} mv {{ apr_util_version }} {{ download_dir }}/{{ httpd_version }}/srclib/apr-util
  - name: build httpd
    shell: chdir={{ download_dir }}/{{ httpd_version }} ./configure --prefix={{ install_dir }} --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork && make -j {{ ansible_processor_vcpus }} && make install
  - name: create group
    group: name=apache gid=80 system=yes
  - name: create user
    user: name=apache uid=80 group=apache shell=/sbin/nologin system=yes create_home=no home={{ install_dir }}/conf/httpd
  - name: set httpd user
    lineinfile: path={{ install_dir }}/conf/httpd.conf regexp='^User' line='User apache'
  - name: set httpd group
    lineinfile: path={{ install_dir }}/conf/httpd.conf regexp='^Group' line='Group apache'
  - name: set variable PATH
    shell: echo PATH={{ install_dir }}/bin:$PATH >> /etc/profile.d/httpd.sh
  - name: prepare service file
    template: src=./httpd.service.j2 dest=/usr/lib/systemd/system/httpd.service
  - name: start service
    service: name=httpd state=started enabled=yes 
[root@cent8_yzl_10 ansible]# 

[root@cent8_yzl_10 ansible]# cat httpd.service.j2 
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=forking
ExecStart={{ install_dir }}/bin/apachectl start
ExecReload={{ install_dir }}/bin/httpd $OPTIONS -k graceful
ExecStop={{ install_dir }}/bin/apachectl stop
KillSignal=SIGCONT
privateTmp=true

[Install]
WangtedBy=multi-user.target
[root@cent8_yzl_10 ansible]# 
[root@cent8_yzl_10 ansible]# ansible-playbook -C install_httpd.yml 


[root@cent8_yzl_10 ansible]# ansible-playbook install_httpd.yml 
[root@cent8_yzl_20 httpd-2.4.46]# ss -tln
State                           Recv-Q                          Send-Q                                                    Local Address:Port                                                     Peer Address:Port                          
LISTEN                          0                               128                                                             0.0.0.0:22                                                            0.0.0.0:*                             
LISTEN                          0                               100                                                           127.0.0.1:25                                                            0.0.0.0:*                             
LISTEN                          0                               128                                                                   *:80                                                                  *:*                             
LISTEN                          0                               128                                                                [::]:22                                                               [::]:*                             
LISTEN                          0                               100                                                               [::1]:25                                                               [::]:*                             
[root@cent8_yzl_20 httpd-2.4.46]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; static; vendor preset: disabled)
   Active: active (running) since Wed 2021-05-26 22:22:38 CST; 1min 44s ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 50748 ExecStart=/apps/httpd/bin/apachectl start (code=exited, status=0/SUCCESS)
 Main PID: 50751 (httpd)
    Tasks: 6 (limit: 23537)
   Memory: 5.5M
   CGroup: /system.slice/httpd.service
           ├─50751 /apps/httpd/bin/httpd -k start
           ├─50762 /apps/httpd/bin/httpd -k start
           ├─50763 /apps/httpd/bin/httpd -k start
           ├─50764 /apps/httpd/bin/httpd -k start
           ├─50765 /apps/httpd/bin/httpd -k start
           └─50766 /apps/httpd/bin/httpd -k start

May 26 22:22:27 cent8_yzl_20.yzil.xyz systemd[1]: Starting The Apache HTTP Server...
May 26 22:22:38 cent8_yzl_20.yzil.xyz systemd[1]: Started The Apache HTTP Server.
[root@cent8_yzl_20 httpd-2.4.46]# 


[root@cent8_yzl_30 ~]# ss -tln
State                           Recv-Q                          Send-Q                                                    Local Address:Port                                                     Peer Address:Port                          
LISTEN                          0                               128                                                             0.0.0.0:22                                                            0.0.0.0:*                             
LISTEN                          0                               100                                                           127.0.0.1:25                                                            0.0.0.0:*                             
LISTEN                          0                               128                                                                [::]:22                                                               [::]:*                             
LISTEN                          0                               100                                                               [::1]:25                                                               [::]:*                             
LISTEN                          0                               128                                                                   *:80                                                                  *:*                             
[root@cent8_yzl_30 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; static; vendor preset: disabled)
   Active: active (running) since Wed 2021-05-26 22:16:09 CST; 8min ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 68940 ExecStart=/apps/httpd/bin/apachectl start (code=exited, status=0/SUCCESS)
 Main PID: 68943 (httpd)
    Tasks: 6 (limit: 23537)
   Memory: 5.5M
   CGroup: /system.slice/httpd.service
           ├─68943 /apps/httpd/bin/httpd -k start
           ├─68954 /apps/httpd/bin/httpd -k start
           ├─68955 /apps/httpd/bin/httpd -k start
           ├─68956 /apps/httpd/bin/httpd -k start
           ├─68957 /apps/httpd/bin/httpd -k start
           └─68958 /apps/httpd/bin/httpd -k start

May 26 22:16:09 cent8_yzl_30.yzil.xyz systemd[1]: Starting The Apache HTTP Server...
May 26 22:16:09 cent8_yzl_30.yzil.xyz systemd[1]: Started The Apache HTTP Server.
[root@cent8_yzl_30 ~]# 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

y_zilong

一分钱的肯定

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值