使用 Ansible 管理 Windows 虚机(一)

1,877 阅读3分钟

Windows 机器的运维素来是运维工程师的痛点,社区太封闭,自动化工具太少。而 Ansible 作为自动化运维的一大利器,也支持 Windows 机器的管理,并且可以满足日常的运维工作需要。本文将介绍如何使用 Ansible 管理 Windows 虚机。

写在最前

笔者公司大量使用 Microsoft 的技术,业务服务基于 .Net 开发,基础设施部署在 Azure Cloud 上,服务编排采用 Service Fabric ... 因此,在我们的项目中无论是 CI 或 CD pipeline 都是基于 Windows 操作系统的 VM Ware 虚拟机,因而 Windows 系统的运维就成了我们的一大痛点。 Windows 操作系统的社区不如 Linux 社区活跃,因而相关的工具也比较少,能够媲美 Linux 社区的优秀项目的管理工具更是凤毛麟角,而且大部分都是需要购买或者授权,给我们的运维带来了极大的不便。 非常幸运,Ansible 作为自动化运维的一大利器,也支持管理 Windows 机器,因此我们采用 Ansible 来管理自己的虚拟机。

了解 Ansible

Ansible 是 RedHat 公司资助的一个开源项目(也开发了商业版),其是基于 Python 开发的用于实现 IT 自动化的工具。Ansible 的功能十分强大,可以实现应用部署、配置管理、任务编排等功能。并且 Ansible 也十分简单易学,在远程机器上执行任务无需安装 Ansible agent,开发 playbook 的时候也不需要学习任何额外的编程语言,只需要遵守 Ansible 定义的格式编写 YAML 文件就足够了。

虽然 Ansible 不需要安装 Ansible agent,但这也并不意味着被管理的主机无需任何配置就能实现自动化管理。 Ansible 在与 Linux 主机通过 SSH 协议通信,因此需要在 Linux 主机上实现 SSH 通信相关的配置,比如用户、认证等等。 Ansible 在与 Windows 主机通过 WS-Management 协议通信,因此需要在 Windows 主机上安装 WinRM 和 Powershell 并实现相关的配置。

Ansible 管理 Windows 主机

Windows 主机的配置

  • 安装 Powershell Powershell 的安装可以参照 Installing PowerShell on Windows,建议选择最新的稳定版本安装。 如果 Windows 主机上已经安装了旧版本的 Powershell,可以通过以下 Powershell 脚本升级
$url = "https://raw.githubusercontent.com/jborean93/ansible-windows/master/scripts/Upgrade-PowerShell.ps1"
$file = "$env:temp\Upgrade-PowerShell.ps1"
$username = "Administrator"
$password = "Password"
$version = "5.1"

(New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force

&$file -Version $version -Username $username -Password $password -Verbose
$url = "https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1"
$file = "$env:temp\ConfigureRemotingForAnsible.ps1"

(New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)

powershell.exe -ExecutionPolicy ByPass -File $file

Linux 管理主机的配置(Ubuntu 系统)

  • 安装 Python3
sudo apt-get update -y
sudo apt-get install -y python3
  • 安装 python3-winrm
sudo apt-get update -y
sudo apt-get install -y python3-winrm
  • 安装 Kerberos 客户端
apt-get install krb5-user

编写 Ansible Inverntory 文件

Ansible 需要通过 Kerberos 协议与 Windows 主机上的 WinRM 服务通信,出于安全考虑,通信需要认证。

此部分内容与前文中 Windows 主机的 WinRM 配置相关,此部分内容仅为 WinRM 采用默认配置时的例子,使用时请酌情修正。

YAML 格式

all:
  children:
    windows:
      hosts:
        WINDOWS_HOST_NAME
      vars:
        ansible_connection: winrm
        ansible_port: 5986
        ansible_winrm_transport: kerberos
        ansible_user: WINDOWS_ADMIN_USER
        ansible_password: WINDOWS_ADMIN_PASSWORD
        ansible_become: false
        ansible_winrm_server_cert_validation: ignore

Ansible 格式

[windows]

[windows:children]
WINDOWS_HOST_NAME

[windows:vars]
ansible_connection=winrm
ansible_port=5986
ansible_winrm_transport=kerberos
ansible_user=WINDOWS_ADMIN_USER
ansible_password=WINDOWS_ADMIN_PASSWORD
ansible_become=false
ansible_winrm_server_cert_validation=ignore

至此,就可以开始编写 Ansible playbook 来实现管理 Windows 主机的具体任务了。

补充

一开始笔者尝试将 Windows 虚机作为管理主机,但是在安装 Ansible 的时候遇到了问题。Ansible 官方没有提供编译好的 Windows 系统下的 Ansible 安装包,但是提供了使用 pip 来安装 Ansible 的方式,因此我尝试使用 pip 来安装 Ansible,但是却遇到了如下的问题

error: can't copy 'lib\ansible\module_utils\ansible_release.py': doesn't exist or not a regular file

查询了很多资料,发现 Ansible 暂时不支持在 Windows 系统上安装运行,因此我很开心地选择了一台 Ubuntu 系统的机器作为管理主机。