使用角色编译安装httpd
·
使用角色编译安装httpd
文章目录
1. 创建基本结构
创建目录并且下载安装包
[root@129a ~]# mkdir roles
[root@129a ~]# cd roles
[root@129a roles]#
[root@129a roles]# ls
[root@129a roles]# ansible-galaxy init httpd
- Role httpd was created successfully
[root@129a roles]# ls
httpd
[root@129a roles]#
[root@129a roles]# ls
httpd
[root@129a roles]# cp /opt/playbook/httpd/ansible.cfg .
[root@129a roles]# cp /opt/playbook/httpd/inventory .
[root@129a roles]# ls
ansible.cfg httpd inventory
[root@129a roles]# cd httpd/
[root@129a httpd]# ls
README.md files meta templates vars
defaults handlers tasks tests
[root@129a httpd]# cd files/
[root@129a files]# ls
[root@129a files]# cp /opt/playbook/httpd/files/package/* .
[root@129a files]# ls
apr-1.7.0.tar.gz httpd-2.4.53.tar.gz
apr-util-1.6.1.tar.gz
[root@129a files]# cp -r /opt/playbook/httpd/files/game/ .
[root@129a files]# ls
apr-1.7.0.tar.gz game
apr-util-1.6.1.tar.gz httpd-2.4.53.tar.gz
[root@129a files]#
[root@129a files]# cp /opt/playbook/httpd/files/CentOS-Base.repo .
[root@129a files]#
[root@129a files]# ls
CentOS-Base.repo game
apr-1.7.0.tar.gz httpd-2.4.53.tar.gz
apr-util-1.6.1.tar.gz install.sh.j2
[root@129a files]#
2. 编写执行playbook
[root@129a roles]# ls
ansible.cfg httpd inventory main.yml
[root@129a roles]# cat main.yml
---
- hosts: 134h.example.com
roles:
- httpd
[root@129a roles]#
3. 配置角色
3.1 编写任务文件
[root@129a tasks]# vim install.yml
[root@129a tasks]# vim install.yml
[root@129a tasks]# cat install.yml
---
- name: create user for apache
user:
name: apache
state: present
create_home: no
system: yes
shell: /sbin/nologin
- name: Delete the yum source
file:
path: /etc/yum.repos.d
state: absent
- name: Provide the yum source file
copy:
src: files/CentOS-Base.repo
dest: /etc/yum.repos.d/
- name: install dependency packages
yum:
name: "{{ apache_depend_pkg }}"
state: present
- name: Provide software packages
copy:
src: "{{ item }}"
dest: /usr/src
loop: "{{ apache_software }}"
- name: test {{ install_dir }} is exists
command: test -d {{ install_dir }}
register: result
ignore_errors: yes
- name: provide install script for httpd
template:
src: files/install.sh.j2
dest: /tmp/install.sh
owner: root
group: root
mode: 0755
- name: install apache
shell: /tmp/install.sh
when: result['rc'] != 0
ignore_errors: yes
- name: provide services files for httpd
template:
src: templates/httpd.service.j2
dest: /usr/lib/systemd/system/httpd.service
[root@129a tasks]#
3.2 编写主程序文件
[root@129a tasks]# vim main.yml
[root@129a tasks]# cat main.yml
---
# tasks file for httpd
- include_tasks: install.yml
- name: config httpd
template:
src: templates/vhosts.conf.j2
dest: "{{ install_dir }}/conf/extra/vhosts.conf"
notify:
- restart httpd
- name: include extra for httpd
lineinfile:
line: Include conf/extra/vhosts.conf
path: "{{ install_dir }}/conf/httpd.conf"
state: present
notify:
- restart httpd
- name: Provide web site for httpd
copy:
src: files/game
dest: "{{ install_dir }}/htdocs"
- name: close firewalld for runtime
service:
name: firewalld
state: stopped
enabled: no
- name: start service for httpd
service:
name: httpd
state: started
enabled: yes
[root@129a tasks]#
3.3 编写执行脚本模板
[root@129a httpd]# vim files/install.sh.j2
[root@129a httpd]# cat files/install.sh.j2
#!/bin/bash
cd /usr/src
tar xf apr-1.7.0.tar.gz
tar xf apr-util-1.6.1.tar.gz
tar xf httpd-2.4.53.tar.gz
sed -i '/$RM "$cfgfile"/d' apr-1.7.0/configure
cd apr-1.7.0
./configure --prefix=/usr/local/apr && \
make && make install && \
cd ../apr-util-1.6.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && \
make && make install && \
cd ../httpd-2.4.53
./configure --prefix={{ install_dir }} \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork && \
make && make install
[root@129a httpd]#
3.4 配置虚拟主机,开机自启
配置虚拟主机
[root@129a templates]# vim vhosts.conf.j2
[root@129a templates]# cat vhosts.conf.j2
<VirtualHost *:{{ PORT }}>
DocumentRoot "{{ install_dir }}/htdocs/game"
ServerName {{ game_domain }}
ErrorLog "logs/{{ game_domain }}-error_log"
CustomLog "logs/{{ game_domain }}-access_log" common
</VirtualHost>
[root@129a templates]#
配置开机自启
[root@129a templates]# vim httpd.service.j2
[root@129a templates]# cat httpd.service.j2
[Unit]
Description=httpd server daemon
After=network.target
[Service]
Type=forking
ExecStart={{ install_dir }}/bin/apachectl start
ExecStop={{ install_dir }}/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@129a templates]#
3.5 定义变量
[root@129a httpd]# cd defaults/
[root@129a defaults]# ls
main.yml
[root@129a defaults]# vim main.yml
[root@129a defaults]# cat main.yml
---
# defaults file for httpd
apache_depend_pkg:
- openssl-devel
- pcre-devel
- expat-devel
- libtool
- make
- gcc
- gcc-c++
apache_software:
- files/apr-1.7.0.tar.gz
- files/apr-util-1.6.1.tar.gz
- files/httpd-2.4.53.tar.gz
install_dir: /usr/local/apache
PORT: 80
game_domain: game.example.com
[root@129a defaults]#
3.6 编写handlers
[root@129a httpd]# cd handlers/
[root@129a handlers]# ls
main.yml
[root@129a handlers]# vim main.yml
[root@129a handlers]# cat main.yml
---
# handlers file for httpd
- name: restart httpd
service:
name: httpd
state: restarted
[root@129a handlers]#
4. 运行
[root@129a roles]# ansible-playbook main.yml
PLAY [134h.example.com] ************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************
ok: [134h.example.com]
TASK [httpd : include_tasks] *******************************************************************************************************
included: /root/roles/httpd/tasks/install.yml for 134h.example.com
TASK [httpd : create user for apache] **********************************************************************************************
ok: [134h.example.com]
TASK [httpd : Delete the yum source] ***********************************************************************************************
changed: [134h.example.com]
TASK [httpd : Provide the yum source file] *****************************************************************************************
changed: [134h.example.com]
[root@129a roles]# ansible-playbook main.yml
PLAY [134h.example.com] ************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************
ok: [134h.example.com]
TASK [httpd : include_tasks] *******************************************************************************************************
included: /root/roles/httpd/tasks/install.yml for 134h.example.com
TASK [httpd : create user for apache] **********************************************************************************************
ok: [134h.example.com]
TASK [httpd : Delete the yum source] ***********************************************************************************************
changed: [134h.example.com]
TASK [httpd : Provide the yum source file] *****************************************************************************************
changed: [134h.example.com]
TASK [httpd : install dependency packages] *****************************************************************************************
ok: [134h.example.com]
TASK [httpd : Provide software packages] *******************************************************************************************
ok: [134h.example.com] => (item=files/apr-1.7.0.tar.gz)
ok: [134h.example.com] => (item=files/apr-util-1.6.1.tar.gz)
ok: [134h.example.com] => (item=files/httpd-2.4.53.tar.gz)
TASK [httpd : test /usr/local/apache is exists] ************************************************************************************
changed: [134h.example.com]
TASK [provide install script for httpd] ********************************************************************************************
ok: [134h.example.com]
TASK [httpd : install apache] ******************************************************************************************************
skipping: [134h.example.com]
TASK [provide services files for httpd] ********************************************************************************************
ok: [134h.example.com]
TASK [config httpd] ****************************************************************************************************************
ok: [134h.example.com]
TASK [include extra for httpd] *****************************************************************************************************
changed: [134h.example.com]
TASK [Provide web site for httpd] **************************************************************************************************
changed: [134h.example.com]
TASK [httpd : close firewalld for runtime] *****************************************************************************************
changed: [134h.example.com]
TASK [start service for httpd] *****************************************************************************************************
changed: [134h.example.com]
RUNNING HANDLER [restart httpd] ****************************************************************************************************
changed: [134h.example.com]
PLAY RECAP *************************************************************************************************************************
134h.example.com : ok=16 changed=8 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
[root@129a roles]#
5. 查看
[root@134h ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@134h ~]# systemctl status httpd
● httpd.service - httpd server daemon
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2022-06-15 23:17:54 CST; 3min 26s ago
Process: 259319 ExecStop=/usr/local/apache/bin/apachectl stop (code=exited, status=0/SUCCESS)
Process: 259669 ExecStart=/usr/local/apache/bin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 259977 (httpd)
Tasks: 10 (limit: 23502)
Memory: 6.9M
CGroup: /system.slice/httpd.service
├─259977 /usr/local/apache/bin/httpd -k start
├─260281 /usr/local/apache/bin/httpd -k start
├─260282 /usr/local/apache/bin/httpd -k start
├─260283 /usr/local/apache/bin/httpd -k start
├─260284 /usr/local/apache/bin/httpd -k start
├─260285 /usr/local/apache/bin/httpd -k start
├─260816 /usr/local/apache/bin/httpd -k start
├─260952 /usr/local/apache/bin/httpd -k start
├─261008 /usr/local/apache/bin/httpd -k start
└─261009 /usr/local/apache/bin/httpd -k start
Jun 15 23:17:44 134h.example.com systemd[1]: httpd.service: Succeeded.
Jun 15 23:17:44 134h.example.com systemd[1]: Stopped httpd server daemon.
[root@134h ~]#
- [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PI3Fc0fI-1655478404049)(C:/Users/Administrator/AppData/Roaming/Typora/typora-user-images/image-20220615232210176.png)]
6. 结构
[root@129a ~]# cd roles/
[root@129a roles]# tree
.
|-- ansible.cfg
|-- httpd
| |-- README.md
| |-- defaults
| | `-- main.yml
| |-- files
| | |-- CentOS-Base.repo
| | |-- apr-1.7.0.tar.gz
| | |-- apr-util-1.6.1.tar.gz
| | |-- game
| | | |-- #U670d#U52a1#U5668#U4e4b#U5bb6.url
| | | |-- #U7cbe#U54c1#U514d#U8d39#U5546#U4e1a#U6e90#U7801#U4e0b#U8f7d.url
| | | |-- css
| | | | |-- font.css
| | | | `-- style.css
| | | |-- fonts
| | | | |-- OPPOSans-B.woff2
| | | | |-- OPPOSans-M.woff2
| | | | |-- OPPOSans-R.woff2
| | | | `-- PangMenZhengDao.woff2
| | | |-- images
| | | | |-- icon_APP.gif
| | | | |-- icon_VR.gif
| | | | |-- icon_WEB.gif
| | | | |-- icon_bili.svg
| | | | |-- icon_mail.svg
| | | | |-- icon_mouse.svg
| | | | |-- icon_mouse2.svg
| | | | |-- icon_wechat.svg
| | | | |-- lock.svg
| | | | `-- vx.png
| | | |-- index.html
| | | |-- js
| | | | |-- clipboard.min.js
| | | | |-- jquery.SuperSlide.2.1.1.js
| | | | |-- jquery.movebg.js
| | | | |-- respond.min.js
| | | | `-- uaredirect.js
| | | `-- m
| | | |-- css
| | | | `-- style.css
| | | `-- index.html
| | |-- httpd-2.4.53.tar.gz
| | `-- install.sh.j2
| |-- handlers
| | `-- main.yml
| |-- meta
| | `-- main.yml
| |-- tasks
| | |-- install.yml
| | `-- main.yml
| |-- templates
| | |-- httpd.service.j2
| | `-- vhosts.conf.j2
| |-- tests
| | |-- inventory
| | `-- test.yml
| `-- vars
| `-- main.yml
|-- inventory
`-- main.yml
16 directories, 45 files
[root@129a roles]#
更多推荐
所有评论(0)