当前位置:首页>Linux>Linux工程师第一次管Windows服务器:我是怎么用Ansible做到"一边倒"的

Linux工程师第一次管Windows服务器:我是怎么用Ansible做到"一边倒"的

  • 2026-10-11 07:55:36
Linux工程师第一次管Windows服务器:我是怎么用Ansible做到"一边倒"的

Linux工程师第一次管Windows服务器:我是怎么用Ansible做到"一边倒"的


一个让人哭笑不得的场景

说个我听来的真事。

某公司技术团队扩张,新来了一个运维工程师强子。强子之前在另一家公司管了五年Linux服务器,SSH、Ansible、Shell脚本样样精通。

入职第一周,主管给他安排了一个活儿:公司新上了一批Windows服务器,需要做统一的安全加固——关闭不必要的端口、开启防火墙、安装杀毒软件、统一密码策略。

强子接了活儿,打开了他的"武器库"——SSH连上去、Playbook跑起来……

然后他发现,这批Windows服务器根本没有开SSH。

他问主管:Windows怎么连?

主管说:WinRM。

强子问:WinRM是什么?

……

后来强子花了三天时间,才把SSH的习惯切换到WinRM,把Linux思维转换成Windows思维,把ansible-playbook改成能跑在Windows上的版本。

这个故事告诉我们:Ansible不只是Linux的专利,Windows一样能管。但从Linux切到Windows,有些东西需要重新学。

这篇文章,就是写给有Linux基础、第一次要管Windows服务器的工程师看的。目标只有一个:让你少踩坑。


基础概念:Ansible怎么管Windows

在说具体操作之前,先把Ansible管Windows的基本原理说清楚。

Linux服务器上,Ansible通过SSH连接目标机器,然后在目标机器上通过SSH远程调用Python执行模块。

Windows的逻辑不一样。Windows没有原生的SSH服务器(Windows Server 2019之后才内置了OpenSSH服务器),所以Ansible用了另一套协议:WinRM(Windows Remote Management)。

WinRM是Windows自带的远程管理协议,基于SOAP协议工作,原理和SSH不太一样,但用途类似——都是"让Ansible控制端能够远程连接并执行命令"。

所以,用Ansible管Windows,第一步是确保Windows服务器开启了WinRM服务,并且Ansible控制节点能够通过WinRM连接到目标机器。


第一步:让Windows开启WinRM

这是最基础但也最容易出问题的一步。Windows默认不会开启WinRM服务,需要手动启用。

如果是Windows Server,可以用PowerShell远程启用:

在需要被管理的Windows服务器上,以管理员身份打开PowerShell,运行:

Enable-PSRemoting -Force

这条命令会开启WinRM服务,并配置防火墙规则允许远程连接。

如果是Windows 10/11,需要额外一步确认:

Set-Item WSMan:\localhost\Service\Auth\Basic -Value $true

这条命令开启了基本认证——因为Ansible默认用Basic认证方式连接WinRM。

注意:Basic认证在网络上明文传输用户名和密码,有安全风险。只在可信内网环境下使用,生产环境建议配合HTTPS和证书使用。


第二步:在Ansible控制端配置连接参数

控制端就是跑ansible命令的那台Linux机器。

在inventory文件里加上Windows主机的连接参数:

[windows] 192.168.1.101 ansible_user=administrator ansible_password=YourPassword ansible_connection=winrm ansible_port=5985

几个关键参数:

ansible_connection=winrm 告诉Ansible用WinRM协议连接,而不是SSH。

ansible_user 是Windows本地的管理员账户名,不是微软账号(Microsoft Account)。

ansible_port 默认是5985(HTTP),如果配置了HTTPS则用5986。


第三步:安装必要的Python库

Linux控制端需要安装pywinrm才能让Ansible通过WinRM连接Windows:

pip install pywinrm

如果遇到依赖问题,可能还需要:

pip install pywinrm[credssp] pywinrm[kerberos]

安装完成后,可以用一条简单的命令测试连接:

ansible windows -m win_ping

如果返回SUCCESS,说明Ansible已经能成功连接Windows主机了。


实战一:用Ansible管Windows更新

Windows Update是Windows服务器运维里最常做的操作之一。传统做法是远程桌面连上去,点开始菜单,检查更新,等待下载,等待安装,重启……

用Ansible可以一键搞定:

- name: Install Windows Updates   hosts: windows   tasks:     - name: Run Windows Update       win_updates:         category_names:           - SecurityUpdates           - CriticalUpdates         reboot: yes         reboot_timeout: 3600

这Playbook跑起来,Ansible会自动连接所有Windows服务器,检查可用更新,下载,安装,需要重启就重启。跑完之后会生成一份报告,告诉你每台机器更新了哪些补丁。


实战二:批量管理防火墙规则

Windows防火墙的规则管理是个体力活——GUI界面操作效率低,手动敲netsh命令又容易出错。

用Ansible的win_firewall模块可以批量配置:

- name: Configure Windows Firewall Rules   hosts: windows   tasks:     - name: Allow inbound RDP       win_firewall_rule:         name: Remote Desktop         enabled: yes         direction: in         protocol: tcp         local_port: 3389         action: allow      - name: Block inbound port 445       win_firewall_rule:         name: Block SMB         enabled: yes         direction: in         protocol: tcp         local_port: 445         action: block

这样写的好处是:规则是用代码定义的,Git可追溯,每次变更有记录,不存在"有人手工改了规则但没人知道"的问题。


实战三:批量安装软件

Linux上用yum install、apt install装软件很方便。Windows上没有原生的包管理器(winget功能还很有限),批量安装软件是个麻烦事。

用Ansible的win_chocolatey模块可以像Linux一样管理软件包:

- name: Install common software on Windows   hosts: windows   tasks:     - name: Install 7-Zip       win_chocolatey:         name: 7zip         state: present      - name: Install Google Chrome       win_chocolatey:         name: googlechrome         state: present      - name: Install Notepad++       win_chocolatey:         name: notepadplusplus         state: present

Chocolatey是Windows的包管理器,类似于Linux上的apt或yum。用Ansible调用Chocolatey,可以实现Windows软件安装的自动化。


实战四:管理Windows服务

重启服务、查看服务状态、修改启动类型——这些操作在Windows上经常要做。用win_service模块可以统一管理:

- name: Manage Windows Services   hosts: windows   tasks:     - name: Ensure Print Spooler service is stopped and disabled       win_service:         name: Spooler         state: stopped         start_mode: disabled      - name: Ensure Remote Registry service is running       win_service:         name: RemoteRegistry         state: started         start_mode: manual

批量管理服务的价值在于:安全加固时需要关闭很多不必要的服务,手工一台台操作既慢又容易出错。用Playbook,规则清晰、执行高效、结果可复现。


实战五:批量配置Windows安全策略

密码策略、审核策略、用户权限——这些安全配置在企业环境里往往要求统一标准。用Ansible的win_security_policy模块可以批量应用安全模板:

- name: Apply Windows Security Policy   hosts: windows   tasks:     - name: Set password complexity policy       win_security_policy:         section: SystemAccess         key: MinimumPasswordLength         value: 12      - name: Set lockout policy       win_security_policy:         section: AccountLockout         key: LockoutBadCount         value: 5

把安全策略写成Playbook,好处是能版本控制、能review、能审计。每次安全检查发现新问题,修改Playbook重新应用,比手工一台台改要靠谱得多。


常见问题:混合环境的运维思路

很多团队是Windows和Linux混跑的。一套Ansible要同时管两种系统。

有几个建议:

第一:分开管理,别混在一起。

Linux主机的Playbook和Windows主机的Playbook,最好分开维护。虽然Ansible支持混用,但两类系统的思维方式差异大,放在一起会增加复杂度。

第二:理解系统差异,不要套用Linux思维。

Linux上重启服务是systemctl restart nginx,Windows上是Restart-Service nginx。Linux上改配置是sed -i 's/old/new/g' file,Windows上没有sed,需要用win_lineinfile或者PowerShell的Replace运算符。

硬套Linux经验到Windows上,往往会踩坑。

第三:善用PowerShell。

Windows运维有很多操作没有现成的Ansible模块,比如AD用户管理、DHCP服务器配置。这时候可以用win_shell或win_command模块直接跑PowerShell命令。

- name: Get Windows Event Log   hosts: windows   tasks:     - name: Read last 10 security events       win_shell: Get-EventLog -LogName Security -Newest 10 | Format-Table -AutoSize       register: events      - debug: var=events.stdout_lines

PowerShell是Windows运维的基础,学好它比记住所有Ansible模块更有价值。


写在最后

用Ansible管Windows,技术上不难,门槛比想象中低。真正需要转变的,是思维习惯。

Linux工程师习惯了SSH、习惯了Shell、习惯了文本流。Windows是完全不同的体系——GUI是主界面、PowerShell是命令行、AD是用户中心、安全策略是另一套逻辑。

但工具是相通的。Ansible的Playbook语言,Windows一样适用。你已经会的那些东西——inventory管理、变量管理、Handlers、Tags——在Windows上同样能用。

换个系统,换套协议,但自动化运维的思路是一样的。

少踩坑的方法只有一个:多动手,多踩坑,踩完了记得写进Playbook里。


最新文章

随机文章