利用ansible的playbook实现批量编译安装部署httpd-2.4
Posted y_zilong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用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 ~]#
以上是关于利用ansible的playbook实现批量编译安装部署httpd-2.4的主要内容,如果未能解决你的问题,请参考以下文章