【自动化运维神器Ansible】playbook实践示例:HTTPD安装与卸载全流程解析
目录
1 HTTPD安装Playbook深度解析
1.1 安装Playbook完整代码
1.2 任务详解
任务1:安装HTTPD包
任务2:安装配置文件
任务3:修改配置文件
任务4:创建网站目录
任务5:部署网站内容
任务6:启动并设置开机自启
1.3 安装流程
1.4 执行示例
2 HTTPD卸载Playbook深度解析
2.1 卸载Playbook完整代码
2.2 任务详解
任务1:卸载HTTPD包
任务2:删除Apache用户
任务3:删除配置文件
任务4:删除网站内容
2.3 卸载流程图
2.4 执行示例
3 实践:完整生命周期管理
3.1 文件结构准备
3.2 Inventory文件(inventory)
3.3 配置文件示例(files/httpd.conf)
3.4 首页文件示例(files/index.html)
3.5 完整执行流程
3.6 验证命令
4 高级优化
4.1 使用变量增强灵活性
4.2 条件任务与循环
4.3 错误处理与重试
4.4 标签管理
5 总结
1 HTTPD安装Playbook深度解析
1.1 安装Playbook完整代码
---
- hosts: websrvsremote_user: rootgather_facts: notasks:- name: Install httpdyum: name=httpd state=present- name: Install configure filecopy: src=files/httpd.conf dest=/etc/httpd/conf/- name: modify configlineinfile: path: /etc/httpd/conf/httpd.conf regexp: '^Listen' line: 'Listen 8080'- name: mkdir website dirfile: path=/data/html state=directory- name: web htmlcopy: src: files/index.html dest=/data/html/- name: start serviceservice: name=httpd state=started enabled=yes
1.2 任务详解
任务1:安装HTTPD包
- name: Install httpdyum: name=httpd state=present
- 模块:yum包管理模块(适用于RHEL/CentOS)
- 参数:
- name=httpd:指定安装包名
- state=present:确保已安装(幂等操作)
- 作用:安装Apache HTTP服务器及其依赖包
任务2:安装配置文件
- name: Install configure filecopy: src=files/httpd.conf dest=/etc/httpd/conf/
- 模块:copy文件复制模块
- 参数:
- src=files/httpd.conf:源文件路径(相对于Playbook目录)
- dest=/etc/httpd/conf/:目标目录
- 作用:覆盖默认配置文件为自定义版本
任务3:修改配置文件
- name: modify configlineinfile: path: /etc/httpd/conf/httpd.conf regexp: '^Listen' line: 'Listen 8080'
- 模块:lineinfile文件行管理模块
- 参数:
- path:目标文件路径
- regexp='^Listen':匹配以"Listen"开头的行
- line='Listen 8080':替换为指定内容
- 作用:修改监听端口为8080(避免与默认80端口冲突)
任务4:创建网站目录
- name: mkdir website dirfile: path=/data/html state=directory
- 模块:file文件/目录管理模块
- 参数:
- path=/data/html:目标路径
- state=directory:确保是目录
- 作用:创建网站根目录
任务5:部署网站内容
- name: web htmlcopy: src: files/index.html dest=/data/html/
- 模块:copy文件复制模块
- 参数:
- src=files/index.html:源文件
- dest=/data/html/:目标目录
- 作用:部署默认首页文件
任务6:启动并设置开机自启
- name: start serviceservice: name=httpd state=started enabled=yes
- 模块:service服务管理模块
- 参数:
- name=httpd:服务名称
- state=started:确保服务已启动
- enabled=yes:设置开机自启
- 作用:启动HTTPD服务并确保持久化
1.3 安装流程

1.4 执行示例
# 语法检查
ansible-playbook install_httpd.yml --syntax-check# 检查模式预演
ansible-playbook install_httpd.yml --check --diff# 限制执行(仅对192.168.10.31执行)
ansible-playbook install_httpd.yml --limit 192.168.10.31# 正式执行
ansible-playbook install_httpd.yml
2 HTTPD卸载Playbook深度解析
2.1 卸载Playbook完整代码
---
- hosts: websrvsremote_user: roottasks:- name: remove httpd packageyum: name=httpd state=absent- name: remove apache useruser: name=apache state=absent- name: remove config filefile: name=/etc/httpd state=absent- name: remove web htmlfile: name=/data/html/ state=absent
2.2 任务详解
任务1:卸载HTTPD包
- name: remove httpd packageyum: name=httpd state=absent
- 模块:yum包管理模块
- 参数:
- name=httpd:指定卸载包名
- state=absent:确保已卸载
- 作用:彻底删除HTTPD软件包及其依赖
任务2:删除Apache用户
- name: remove apache useruser: name=apache state=absent
- 模块:user用户管理模块
- 参数:
- name=apache:指定用户名
- state=absent:确保用户不存在
- 作用:删除HTTPD运行专用用户
任务3:删除配置文件
- name: remove config filefile: name=/etc/httpd state=absent
- 模块:file文件/目录管理模块
- 参数:
- name=/etc/httpd:目标路径
- state=absent:确保不存在(递归删除)
- 作用:删除HTTPD配置文件目录
任务4:删除网站内容
- name: remove web htmlfile: name=/data/html/ state=absent
- 模块:file文件/目录管理模块
- 参数:
- name=/data/html/:目标路径
- state=absent:确保不存在(递归删除)
- 作用:删除网站根目录及其内容
2.3 卸载流程图

2.4 执行示例
# 语法检查
ansible-playbook remove_httpd.yml --syntax-check# 检查模式预演
ansible-playbook remove_httpd.yml --check --diff# 正式执行
ansible-playbook remove_httpd.yml
3 实践:完整生命周期管理
3.1 文件结构准备
ansible-httpd/
├── install_httpd.yml
├── remove_httpd.yml
├── files/
│ ├── httpd.conf
│ └── index.html
└── inventory
3.2 Inventory文件(inventory)
[websrvs]
192.168.10.30
192.168.10.31
3.3 配置文件示例(files/httpd.conf)
ServerRoot "/etc/httpd"
Listen 8080
ServerAdmin admin@example.com
ServerName www.example.com:8080
DocumentRoot "/data/html"
3.4 首页文件示例(files/index.html)
<!DOCTYPE html>
<html>
<head><title>Ansible HTTPD Test</title>
</head>
<body><h1>HTTPD Server Deployed by Ansible</h1><p>Server IP: {{ ansible_eth0.ipv4.address }}</p>
</body>
</html>
3.5 完整执行流程

3.6 验证命令
# 安装后验证
ansible websrvs -m shell -a "systemctl status httpd"# 卸载后验证
ansible websrvs -m shell -a "rpm -q httpd"
ansible websrvs -m shell -a "ls -la /etc/httpd"
4 高级优化
4.1 使用变量增强灵活性
---
- hosts: websrvsvars:httpd_port: 8080web_root: /data/htmltasks:- name: modify configlineinfile: path: /etc/httpd/conf/httpd.conf regexp: '^Listen' line: 'Listen {{ httpd_port }}'- name: mkdir website dirfile: path={{ web_root }} state=directory
4.2 条件任务与循环
- name: 卸载基于OS的包yum: name={{ item }} state=absentloop:- httpd- httpd-toolswhen: ansible_os_family == "RedHat"- name: 删除多个目录file: name={{ item }} state=absentloop:- /etc/httpd- /var/www/html- /var/log/httpd
4.3 错误处理与重试
- name: 卸载httpd包(带重试)yum: name=httpd state=absentretries: 3delay: 5until: result.rc == 0register: resultignore_errors: yes
4.4 标签管理
---
- hosts: websrvstasks:- name: 安装httpdyum: name=httpd state=presenttags: install- name: 卸载httpdyum: name=httpd state=absenttags: remove- name: 修改配置lineinfile: path: /etc/httpd/conf/httpd.conf regexp: '^Listen' line: 'Listen 8080'tags: config
- 标签执行示例:
# 仅执行安装任务
ansible-playbook install_httpd.yml --tags install# 仅执行卸载任务
ansible-playbook remove_httpd.yml --tags remove
5 总结
通过Ansible Playbook实现HTTPD的全生命周期管理,不仅是技术能力的体现,更是自动化运维思维的升华。掌握安装、配置、卸载的完整流程,能够构建"安全、高效、可追溯"的自动化体系,为企业数字化转型提供坚实支撑。运维人员应持续实践,将手动操作升华为自动化艺术,让技术真正服务于业务价值。