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

@系统管理 - Ansible 补丁管理方案(Windows Linux)

Ansible 补丁管理方案(Windows & Linux)

1. Ansible 简介

Ansible 是一款开源的自动化运维工具,由 Red Hat 公司维护。它采用声明式的 YAML 语言来描述自动化任务,具有以下核心特点:

  • 无代理架构:通过 SSH (Linux) 或 WinRM (Windows) 进行通信,无需在目标主机安装客户端
  • 幂等性:相同 playbook 多次执行结果一致
  • 模块化设计:提供丰富的内置模块(超过 750 个)
  • 跨平台支持:可管理 Linux、Windows、网络设备等
  • 简单易用:YAML 语法易于理解和编写

2. 补丁管理方案设计

2.1 系统要求

  • 控制节点:运行 Ansible 的主机(Linux/macOS)

    • Python 3.8+
    • Ansible 8.0+
    • 必要的依赖包(python3-winrm 用于 Windows 管理)
  • 被管节点

    • Linux:SSH 服务,Python 2.7+/3.5+
    • Windows:WinRM 配置,PowerShell 5.1+

2.2 目录结构

patch_management/
├── inventory/
│   ├── production/
│   │   ├── linux_hosts
│   │   └── windows_hosts
│   └── staging/
│       ├── linux_hosts
│       └── windows_hosts
├── group_vars/
│   ├── linux.yml
│   └── windows.yml
├── roles/
│   ├── linux-patching/
│   │   ├── tasks/
│   │   │   ├── main.yml
│   │   │   ├── pre_reboot.yml
│   │   │   └── post_reboot.yml
│   │   └── vars/
│   │       └── main.yml
│   └── windows-patching/
│       ├── tasks/
│       │   ├── main.yml
│       │   ├── pre_reboot.yml
│       │   └── post_reboot.yml
│       └── vars/
│           └── main.yml
├── playbooks/
│   ├── linux-patch.yml
│   └── windows-patch.yml
└── README.md

3. 详细实施步骤

3.1 环境准备

控制节点安装
# 安装 Ansible
python3 -m pip install --user ansible# 安装 Windows 支持
python3 -m pip install --user pywinrm# 验证安装
ansible --version
被管节点配置

Linux 节点

  • 确保 SSH 服务运行
  • 配置 SSH 密钥认证

Windows 节点

  1. 配置 WinRM(以管理员身份运行 PowerShell):
# 快速配置(仅测试环境)
Enable-PSRemoting -Force
Set-Item WSMan:\localhost\Client\TrustedHosts * -Force
Restart-Service WinRM
  1. 详细生产环境配置参考:Ansible Windows 设置指南

3.2 库存(inventory)配置

inventory/production/linux_hosts:

[linux_servers]
web01.example.com ansible_user=admin
db01.example.com ansible_user=admin[linux_servers:vars]
ansible_python_interpreter=/usr/bin/python3

inventory/production/windows_hosts:

[windows_servers]
win01.example.com
win02.example.com[windows_servers:vars]
ansible_user=Administrator
ansible_password=SecurePass123!
ansible_connection=winrm
ansible_winrm_transport=ntlm
ansible_winrm_server_cert_validation=ignore

3.3 变量配置

group_vars/linux.yml:

---
# Linux 补丁管理配置
patch_reboot: true
patch_security_only: true
exclude_packages:- kernel*- *-devel
pre_patch_scripts:- "/usr/local/scripts/pre-patch.sh"
post_patch_scripts:- "/usr/local/scripts/post-patch.sh"

group_vars/windows.yml:

---
# Windows 补丁管理配置
windows_update_categories:- SecurityUpdates- CriticalUpdates
windows_reboot: true
windows_reboot_timeout: 1800  # 30分钟
blacklist_kbs:- KB1234567- KB8912345

3.4 角色开发

Linux 补丁角色

roles/linux-patching/tasks/main.yml:

---
- name: 检查是否为 root 用户assert:that: ansible_user_id == "root"fail_msg: "必须使用 root 用户运行补丁任务"- name: 执行预补丁脚本include_tasks: pre_reboot.ymlwhen: pre_patch_scripts is defined- name: 更新包缓存package:update_cache: yeswhen: ansible_os_family in ['Debian', 'RedHat']- name: 检查可用更新command: "yum check-update --security"register: yum_updateschanged_when: falseignore_errors: truewhen: ansible_os_family == 'RedHat' and patch_security_only- name: 安装安全更新 (RHEL/CentOS)yum:name: "*"security: yesexclude: "{{ exclude_packages }}"update_cache: yesstate: latestregister: yum_resultwhen: ansible_os_family == 'RedHat' and patch_security_only- name: 安装所有更新 (RHEL/CentOS)yum:name: "*"exclude: "{{ exclude_packages }}"update_cache: yesstate: latestregister: yum_resultwhen: ansible_os_family == 'RedHat' and not patch_security_only- name: 安装安全更新 (Debian/Ubuntu)apt:upgrade: distupdate_cache: yesautoremove: yesonly_upgrade: yesregister: apt_resultwhen: ansible_os_family == 'Debian' and patch_security_only- name: 执行后补丁脚本include_tasks: post_reboot.ymlwhen: post_patch_scripts is defined- name: 重启系统 (如果需要)reboot:msg: "应用系统更新后重启"connect_timeout: 5reboot_timeout: 600pre_reboot_delay: 30post_reboot_delay: 30when: patch_reboot and (yum_result.changed or apt_result.changed)
Windows 补丁角色

roles/windows-patching/tasks/main.yml:

---
- name: 检查 Windows 更新服务状态win_service:name: wuauservstate: started- name: 下载 Windows 更新win_updates:category_names: "{{ windows_update_categories }}"state: downloadedblacklist: "{{ blacklist_kbs }}"log_path: C:\Windows\Temp\ansible_wu.logregister: win_updates_result- name: 安装 Windows 更新win_updates:category_names: "{{ windows_update_categories }}"state: installedblacklist: "{{ blacklist_kbs }}"log_path: C:\Windows\Temp\ansible_wu.logregister: win_updates_installwhen: win_updates_result.found_update_count > 0- name: 执行预重启检查include_tasks: pre_reboot.ymlwhen: windows_reboot and win_updates_install is changed- name: 重启 Windows 服务器win_reboot:msg: "应用 Windows 更新后重启"connect_timeout: 5reboot_timeout: "{{ windows_reboot_timeout }}"pre_reboot_delay: 60post_reboot_delay: 30when: windows_reboot and win_updates_install is changed- name: 验证更新后状态win_updates:category_names: "{{ windows_update_categories }}"state: searchedregister: post_update_checkwhen: win_updates_install is changed- name: 执行后重启任务include_tasks: post_reboot.ymlwhen: windows_reboot and win_updates_install is changed

3.5 Playbook 开发

playbooks/linux-patch.yml:

---
- name: 应用 Linux 服务器补丁hosts: linux_serversserial: "30%"  # 滚动更新,每次 30% 主机become: yesgather_facts: yesroles:- linux-patchingpost_tasks:- name: 验证系统状态shell: uptimeregister: uptimechanged_when: false- name: 显示系统正常运行时间debug:msg: "系统正常运行时间: {{ uptime.stdout }}"

playbooks/windows-patch.yml:

---
- name: 应用 Windows 服务器补丁hosts: windows_serversserial: 2  # 每次更新 2 台主机gather_facts: yesroles:- windows-patchingpost_tasks:- name: 获取系统信息win_shell: systeminforegister: systeminfochanged_when: false- name: 显示最后启动时间debug:msg: "{{ (systeminfo.stdout | regex_search('系统启动时间:.*')) }}"

4. 执行补丁管理

4.1 测试运行

# 测试 Linux 补丁 (检查模式)
ansible-playbook -i inventory/production/linux_hosts playbooks/linux-patch.yml --check# 测试 Windows 补丁 (检查模式)
ansible-playbook -i inventory/production/windows_hosts playbooks/windows-patch.yml --check

4.2 实际执行

# 执行 Linux 补丁
ansible-playbook -i inventory/production/linux_hosts playbooks/linux-patch.yml \--limit "web_servers" \--extra-vars "patch_security_only=false"# 执行 Windows 补丁
ansible-playbook -i inventory/production/windows_hosts playbooks/windows-patch.yml \--extra-vars "windows_reboot_timeout=3600"

4.3 高级选项

# 使用标签执行特定任务
ansible-playbook -i inventory/production/linux_hosts playbooks/linux-patch.yml \--tags "pre_patch,update"# 从失败点继续执行
ansible-playbook -i inventory/production/linux_hosts playbooks/linux-patch.yml \--start-at-task "安装安全更新 (RHEL/CentOS)"

5. 报告与验证

5.1 生成报告

# 收集 Linux 补丁状态
ansible linux_servers -i inventory/production/linux_hosts -m shell -a \"yum history list | head -n 10 || apt list --upgradable"# 收集 Windows 补丁状态
ansible windows_servers -i inventory/production/windows_hosts -m win_shell -a \"Get-HotFix | Sort-Object InstalledOn -Descending | Select-Object -First 10"

5.2 使用回调插件

ansible.cfg 中启用插件:

[defaults]
callback_whitelist = mail, slack, junit

6. 最佳实践

  1. 测试环境先行:先在非生产环境测试补丁
  2. 分阶段部署:使用 Ansible 的 serial 关键字进行滚动更新
  3. 维护窗口:使用 --limit 参数控制补丁应用范围
  4. 备份策略:在应用补丁前确保系统备份
  5. 监控验证:补丁后验证关键服务状态
  6. 文档记录:记录每次补丁的变更和问题

7. 故障排除

常见 Linux 问题

问题:YUM/DNF 锁错误
解决

- name: 清理 YUM 锁file:path: /var/run/yum.pidstate: absentignore_errors: yes

常见 Windows 问题

问题:WinRM 连接失败
解决

# 在 Windows 主机上执行
winrm quickconfig
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
winrm set winrm/config/service/auth '@{Basic="true"}'

8. 扩展建议

  1. 与 CMDB 集成:动态获取 inventory
  2. 与监控系统集成:补丁前后检查系统指标
  3. 自动化审批流程:与 ITSM 工具集成
  4. 补丁合规报告:生成 PDF/HTML 报告
  5. 容器化 Ansible:使用 AWX 或 Ansible Tower

9. 参考资源

  1. Ansible 官方文档
  2. Windows 更新模块文档
  3. Linux 包管理模块
  4. Ansible 最佳实践指南

通过Ansible工具,您可以实现跨平台的自动化补丁管理,确保系统安全性和稳定性,同时减少人工干预和操作错误。

http://www.xdnf.cn/news/17666.html

相关文章:

  • 飞算JavaAI的“盾牌”计划:手撕Spring Security + JWT认证链
  • CNN卷积神经网络预测手写数字的Pytorch实现
  • C++ 优选算法 力扣 209.长度最小的子数组 滑动窗口 (同向双指针)优化 每日一题 详细题解
  • [系统架构设计师]架构设计专业知识(二)
  • python与JavaScript的区别
  • 三方相机问题分析六:【没用相机,诡异的手电筒不可使用】下拉状态栏,手电筒置灰,无法打开,提提示相机正在使用
  • 模型驱动的自动驾驶AI系统全生命周期安全保障
  • 论文Review 激光SLAM VoxelMap | RAL 2022 港大MARS出品!| 经典平面特征体素激光SLAM
  • .NET 应用程序 Linux下守护进程脚本编写
  • 基于.Net Framework4.5 Web API 引用Swagger
  • JavaWeb核心:HttpServletRequest与HttpServletResponse详解
  • mac环境下安装git并配置密钥等
  • 从行业场景到视觉呈现:3ds Max 与 C4D 效果图的本质分野
  • Pycharm现有conda环境有对应env,但是添加后没反应
  • 学习嵌入式的第十九天——Linux——文件编程
  • Spring Boot 使用 @NotBlank + @Validated 优雅校验参数
  • 疯狂星期四文案网第38天运营日记
  • TorchDynamo - API
  • 互联网大厂Java求职面试实录:Spring Boot到微服务与AI的技术问答
  • 【Unity开发】Unity核心学习(一)
  • 如何在 Ubuntu 24.04 LTS Noble Linux 上安装 FileZilla Server
  • MyBatis 中 XML 与 DAO 接口的位置关系及扫描机制详解
  • react与vue的对比,来实现标签内部类似v-for循环,v-if等功能
  • 万字详解C++11列表初始化与移动语义
  • 如何把ubuntu 22.04下安装的mysql 8 的 数据目录迁移到另一个磁盘目录
  • 基于深度学习的苹果品质智能检测算法研究
  • Kubernetes(K8S)中,kubectl describe node与kubectl top pod命令显示POD资源的核心区别
  • .net\c#web、小程序、安卓开发之基于asp.net家用汽车销售管理系统的设计与实现
  • Android Activity 的对话框(Dialog)样式
  • LaTeX(排版系统)Texlive(环境)Vscode(编辑器)环境配置与安装