使用Ansible角色自动化部署HTTPD服务
项目实践背景:
随着企业服务器规模扩大,传统手动部署 Web 服务效率低、一致性差、易出错,已无法满足批量运维需求。为提升自动化部署能力,掌握企业级配置管理工具,本次实验基于 Ansible 自动化运维平台 ,采用 Role 角色化架构对多台远程节点批量部署、配置、启动 HTTPD 服务,实现 Web 服务的标准化、自动化、可复用交付。实验环境包含 1 台 Ansible 控制节点、2 台被管理节点,通过 Inventory 清单管理主机,以 Playbook + Roles 完成全流程自动化部署。
1.环境架构搭建
此次项目实践采用Redhat9.6系统版本,首先在主控节点上安装ansible环境,被控环境清除ansible旧环境;
1.1创建项目目录httpd1
bash
[root@ansible-controller ~]# mkdir httpd1
[root@ansible-controller ~]# cd httpd1/
1.2创建清单目录及其文件
bash
[root@ansible-controller inventory]# vim hosts
内容如下:
[server]
192.168.110.150
192.168.110.151
1.3创建ansible.cfg全局配置文件
[root@ansible-controller inventory]# cd ..
[root@ansible-controller httpd1]# vim ansible.cfg
[root@ansible-controller httpd1]# cat ansible.cfg
[defaults]
inventory=inventory/hosts
1.4编写playbook.yaml剧本执行文件
[root@ansible-controller demo1]# vim playbook.yaml
文件内容如下:
---
- name: Install httpd playbook
hosts: server
roles:
- role: roles/httpd
2.构建roles角色目录结构
[root@ansible-controller httpd1]# mkdir roles
[root@ansible-controller httpd1]# cd roles/
[root@ansible-controller roles]# ansible-galaxy role init httpd
- Role httpd was created successfully
[root@ansible-controller ~]# tree httpd1/
httpd1/
├── ansible.cfg
├── inventory
│ └── hosts
├── playbook.yaml
└── roles
└── httpd
├── files
│ └── httpd.conf
├── handlers
│ └── main.yml
├── tasks
│ ├── config_httpd.yaml
│ ├── index_httpd.yaml
│ ├── install_httpd.yaml
│ ├── main.yml
│ └── service_httpd.yaml
├── templates
│ └── index.html
└── vars
└── main.yml
11 directories, 17 files
2.1编写tasks顺序配置清单文件
[root@ansible-controller httpd1]# cd roles/
[root@ansible-controller roles]# ls
httpd
[root@ansible-controller roles]# cd httpd/
[root@ansible-controller httpd]# ls
defaults files handlers meta README.md tasks templates tests vars
[root@ansible-controller httpd]# cd tasks/
[root@ansible-controller tasks]# ls
main.yml
[root@ansible-controller tasks]# vim main.yml
[root@ansible-controller tasks]# cat main.yml
---
- include: install_httpd.yaml
- include: config_httpd.yaml
- include: index_httpd.yaml
- include: service_httpd.yam
2.2编写install_httpd.yaml安装模块
[root@ansible-controller tasks]# vim install_httpd.yaml
[root@ansible-controller tasks]# cat install_httpd.yaml
---
- name: Install httpd
ansible.builtin.dnf:
name: httpd
state: present
2.3编写config_httpd.yaml配置模块
把 files/ 下的 httpd.conf 复制到目标主机的 /etc/httpd/conf/ 目录
[root@ansible-controller tasks]# vim config_httpd.yaml
[root@ansible-controller tasks]# cat config_httpd.yaml
---
- name: configure httpd
ansible.builtin.copy:
src: "files/httpd.conf"
dest: "/etc/httpd/conf/httpd.conf"
notify: restart httpd
2.4编写index_httpd.yaml页面编辑模块
把 templates/ 下的 index.html 部署到目标主机的 /var/www/html/(HTTPD 默认网页目录),实现自定义首页。
[root@ansible-controller tasks]# vim index_httpd.yaml
[root@ansible-controller tasks]# cat index_httpd.yaml
---
- name: create index
ansible.builtin.template:
src: index.html
dest: "/var/www/html/index.html"
2.5编写service_httpd.yaml服务模块
启动 / 重启 HTTPD 服务,并设置开机自启
[root@ansible-controller tasks]# vim service_httpd.yaml
[root@ansible-controller tasks]# cat service_httpd.yaml
---
- name: start httpd service
ansible.builtin.service:
name: httpd
state: started
3.配置文件编写
3.1roles/httpd/files/httpd.conf下配置文件的编写
[root@ansible-controller tasks]# cd ..
[root@ansible-controller httpd]# ls
defaults files handlers meta README.md tasks templates tests vars
[root@ansible-controller httpd]# cd files/
[root@ansible-controller files]# vim httpd.conf
[root@ansible-controller files]# cat httpd.conf
ServerRoot "/etc/httpd"
Listen 80 #开放80端口,需保障端口不被占用
Include conf.modules.d/*.conf
User apache
Group apache
DocumentRoot "/var/www/html"
<Directory "/var/www">
AllowOverride None
Require all granted
</Directory>
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
<Files ".ht*">
Require all denied
</Files>
ErrorLog "logs/error_log"
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
<IfModule mime_module>
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf
3.2编写templates/index.html文件
[root@ansible-controller files]# cd ../templates/
[root@ansible-controller templates]# ls
[root@ansible-controller templates]# vim index.html
[root@ansible-controller templates]# cat index.html
<html>
<head>
<meta charset="UTF-8">
<title>index.html</title>
</head>
<body>
<h1>{{ index_content }}</h1>
</body>
</html>
3.3编写vars/main.yaml,在这个文件中定义所用到的变量
[root@ansible-controller templates]# cd ..
[root@ansible-controller httpd]# ls
defaults files handlers meta README.md tasks templates tests vars
[root@ansible-controller httpd]# cd vars/
[root@ansible-controller vars]# ls
main.yml
[root@ansible-controller vars]# vim main.yml
[root@ansible-controller vars]# cat main.yml
---
index_content: "welcome to httpd index.html"
3.4编写handlers/main.yml,在这个文件中定义任务处理器
bash
[root@ansible-controller ~]# cd demo1/roles/httpd/handlers/
[root@ansible-controller handlers]# vim main.yml
[root@ansible-controller handlers]# cat main.yml
---
- name: restart httpd
ansible.builtin.service:
name: httpd
state: restarted
4.执行playbook
[root@harbor ~]# cd httpd1/
[root@harbor httpd1]# ansible-playbook playbook.yml
[DEPRECATION WARNING]: "include" is deprecated, use include_tasks/import_tasks instead. See
https://docs.ansible.com/ansible-core/2.14/user_guide/playbooks_reuse_includes.html for details. This
feature will be removed in version 2.16. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.
PLAY [install httpd playbook] *****************************************************************************
TASK [Gathering Facts] ************************************************************************************
ok: [192.168.110.150]
ok: [192.168.110.151]
TASK [roles/httpd : install httpd] ************************************************************************
ok: [192.168.110.151]
ok: [192.168.110.150]
TASK [roles/httpd : configure httpd] **********************************************************************
ok: [192.168.110.151]
ok: [192.168.110.150]
TASK [roles/httpd : create index] *************************************************************************
ok: [192.168.110.150]
ok: [192.168.110.151]
TASK [roles/httpd : start httpd service] ******************************************************************
ok: [192.168.110.150]
changed: [192.168.110.151]
PLAY RECAP ************************************************************************************************
192.168.110.150 : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.110.151 : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
由此可见,剧本正常执行无报错,httpd服务被成功安装和配置,接下来,进行测试服务;
5.测试是否正常服务
在主控节点进行测试:
[root@harbor httpd1]# curl localhost:80
<html>
<head>
<meta charset="UTF-8">
<title>index.html</title>
</head>
<body>
<h1>welcome to httpd index.html</h1>
</body>
</html>
在受控节点进行测试:
[root@worker2 ~]# curl localhost:80
<html>
<head>
<meta charset="UTF-8">
<title>index.html</title>
</head>
<body>
<h1>welcome to httpd index.html</h1>
</body>
</html>
[root@worker2 ~]#
验证可知,httpd服务被成功部署
6.实验总结:
本次实验成功完成 基于 Ansible Role 的 HTTPD 服务批量自动化部署。
通过构建标准化角色目录结构,将安装、配置、首页发布、服务启动拆分为独立任务模块,实现任务解耦与复用。实验中完成:
- 搭建 Ansible 项目架构,配置主机清单与全局参数;
- 编写 Role 角色,实现任务模块化、配置文件管理、模板渲染、变量定义、触发器重启等完整功能;
- 批量部署 HTTPD 服务到两台远程节点;
- 完成配置下发、页面发布、服务启动与开机自启;
- 验证服务可用性,实现 80 端口正常访问自定义页面。
最终所有节点部署成功,服务运行稳定,达到自动化运维交付标准。
7.实验反思:
1.路径与语法问题是高频错误点
实验中曾出现配置文件路径错误、Listen 端口语法错误、缺少引号等问题,导致服务启动失败。说明对 Ansible 文件查找规则与 HTTPD 配置语法不够熟练,后续需加强路径规范与配置校验习惯。
2.端口占用会直接导致服务启动失败
实验中 80 端口被其他服务占用导致 HTTPD 无法启动,加深了对 "端口冲突→服务无法绑定→启动失败" 的理解,提升了端口排查能力。
3.角色化结构让排错更清晰
任务拆分后,安装、配置、服务各模块独立,出错时能快速定位问题环节,体现企业标准化架构的优势。
4.Ansible 幂等性重要性
重复执行剧本不会重复安装或异常重启,保证环境稳定,体现自动化工具的核心价值。
8.实验意义:
1.更熟悉和掌握企业主流自动化运维工具 Ansible熟练使用 Playbook、Role、Inventory、Tasks、Handlers、Templates 等核心组件,符合现代运维 / 云计算 / DevOps 岗位技能要求。
2.理解 Web 服务架构与部署流程掌握 HTTPD 安装、配置、监听端口、文档根目录、服务管理等关键知识点,具备 Web 服务独立部署与排错能力。
3.提升批量运维与标准化交付能力能够一次性配置多台服务器,大幅提升部署效率,降低人为错误,符合企业生产环境运维规范。
4.该项目可增加 Linux 运维、自动化部署、Web 服务维护、云服务器管理 的实战经历,提升了动手能力、问题排查能力与工程化思维。