当前位置: 首页 > java >正文

【自动化运维神器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的全生命周期管理,不仅是技术能力的体现,更是自动化运维思维的升华。掌握安装、配置、卸载的完整流程,能够构建"安全、高效、可追溯"的自动化体系,为企业数字化转型提供坚实支撑。运维人员应持续实践,将手动操作升华为自动化艺术,让技术真正服务于业务价值。
http://www.xdnf.cn/news/17504.html

相关文章:

  • Vue 3.6 Vapor模式完全指南:告别虚拟DOM,性能飞跃式提升
  • [TryHackMe]Challenges---Game Zone游戏区
  • ThingsBoard配置邮件发送保姆级教程(新版qq邮箱)
  • 第二十天:余数相同问题
  • 相册管理系统介绍
  • ARMv8 MMU页表格式及地址转换过程分析
  • 当配置项只支持传入数字,即无法指定单位为rem,需要rem转px
  • js零基础入门
  • java之父-新特性
  • 如何搭建ELK
  • 李宏毅深度学习教程 第16-18章 终身学习+网络压缩+可解释性人工智能
  • LeetCode 刷题【36. 有效的数独】
  • 【Datawhale AI夏令营第三期】多模态RAG
  • c++ 容器vector基础
  • 【递归、搜索和回溯】FloodFill 算法介绍及相关例题
  • Zread:把 GitHub 仓库“一键变说明书”的体验与实战指南
  • AutoML 的下半场——从“模型选择”到“端到端业务闭环”
  • Redhat Linux 9.6 配置本地 yum 源
  • Java类和对象课上练习题目设计
  • 计算机网络:CIDR地址块如何划分子网
  • 24SpringCloud黑马商城微服务整合Seata重启服务报错的解决办法
  • Day 36: 复习
  • 【机器学习深度学习】模型选型:如何根据模型的参数算出合适的设备匹配?
  • 05.【数据结构-C语言】栈(先进后出,栈的实现:进栈、出栈、获取栈顶元素,栈实现代码,括号匹配问题)
  • [Oracle] SUBSTR()函数
  • [CUDA] CUTLASS | `CuTe DSL` 创新
  • 化工安防误报率↓82%!陌讯多模态融合算法实战解析
  • ARM CPU 安全更新:Training Solo(关于 Spectre-v2 攻击中域隔离机制的局限性)
  • 在ubuntu服务器下安装cuda和cudnn(笔记)
  • 基于Prometheus、Grafana、Loki与Tempo的统一监控平台故障排查与解决方案