当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何使用 Ansible 管理 Windows 机器

作者:精品下载站 日期:2024-12-14 06:35:11 浏览:16 分类:玩电脑

如何使用 Ansible 管理 Windows 机器


Ansible 是流行的免费开源配置管理系统,主要用于管理 Linux 主机。本文介绍如何使用 Ansible 远程管理 Windows 服务器和工作站主机的配置。

为 Ansible 配置 Windows 主机

Ansible 允许您远程管理具有所有支持的操作系统版本的 Windows 主机,从 Windows 7/Windows Server 2008 开始到最新的 Windows 11/Windows Server 2022。在 Windows 上,您必须拥有 PowerShell 3.0(或更高版本)和 .NET 4.0 +已安装。

检查 Windows 上的 PowerShell 和 .Net Framework 的版本,并根据需要进行更新(请参阅如何更新 Windows 中的 PowerShell)。

Ansible 使用 WinRM 连接到 Windows 操作系统。因此,您需要在所有托管 Windows 主机上启用并配置 WinRM 侦听器。

Ansible 2.8 及更高版本有一个实验性选项,可以通过内置 OpenSSH 服务器远程管理 Windows 10/11 和 Windows Server 2019/2022 客户端。

  • 组策略可用于在 AD 中的域计算机上配置 WinRM;

  • 运行以下 PowerShell 命令以在独立 Windows 主机上启用 WinRM:

    Enable-PSRemoting -Force

如果您在 Windows 主机上启用并配置了 WinRM,请检查是否可以从管理 Ansible 服务器访问 TCP/5985 或 TCP/5986 端口(如果使用 HTTPS):

$ nc -zv 192.168.13.122 5985

[玩转系统] 如何使用 Ansible 管理 Windows 机器

然后您需要选择身份验证方法。这取决于您使用 Ansible 的环境。

  • 对于独立计算机或工作组环境,您可以使用 HTTPS for WinRM 以及自签名证书和使用具有管理员权限的本地 Windows 帐户进行身份验证。要快速配置 Windows 主机,您可以使用 ConfigureRemotingForAnsible.ps1 (https://github.com/ansible/ansible-documentation/blob/devel/examples/scripts/ConfigureRemotingForAnsible.ps1 );

  • 就我而言,所有 Windows 主机都加入到 Active Directory 域,因此我将使用我的 AD 帐户对 Ansible 进行身份验证。这需要在 Ansible 服务器上配置 Kerberos 身份验证(见下文)。

此时,我们假设 Ansible 已安装在您的 Linux 管理主机上。

安装 Kerberos 身份验证所需的软件包:

  • 对于 RHEL/Rocky Linux/CentOS,通过 yum/dnf 包管理器:

    $ sudo yum -y install python-devel krb5-devel krb5-libs krb5-workstation
  • 对于 Ubuntu/Debian:

    $ sudo apt-get -y install python-dev libkrb5-dev krb5-user

然后通过pip安装Python包:

$ sudo pip3 install requests-kerberos

在 Kerberos conf 文件中指定您的域的连接设置:

$ sudo mcedit /etc/krb5.conf
[logging]
 default = FILE:/var/log/krb5libs.log
 kdc = FILE:/var/log/krb5kdc.log
 admin_server = FILE:/var/log/kadmind.log
[libdefaults]
 dns_lookup_realm = false
 ticket_lifetime = 24h
 renew_lifetime = 7d
 forwardable = true
 rdns = false
default_realm = WOSHUB.LOC
[realms]
  WOSHUB.LOC = {
    admin_server = dc01.a-d.loc
    kdc = dc01.a-d.loc
  }
[domain_realm]
 a-d.loc = WOSHUB.LOC
 .WOSHUB.LOC = WOSHUB.LOC

检查您是否可以对 AD 域进行身份验证并获取 Kerberos 票证:

kinit -C [email 

输入您的 AD 用户密码,查看是否收到票据。

klist

[玩转系统] 如何使用 Ansible 管理 Windows 机器

使用 Ansible 管理 Windows 主机

接下来,将所有 Windows 主机添加到 Ansible 库存文件中:

$ sudo mcedit /etc/ansible/hosts
mun-rds1.a-d.loc
mun-dc02.a-d.loc
wks-test1.a-d.loc
[windows_all:vars]
ansible_port=5985
[email 
ansible_connection=winrm
ansible_winrm_transport=kerberos
ansible_winrm_scheme=http
ansible_winrm_server_cert_validation=ignore

检查您的所有 Windows 主机(我的列表包括两台 Windows Server 2019 和一台 Windows 11 计算机)是否可以从 Ansible 访问:

$ ansible windows_all -m win_ping

[玩转系统] 如何使用 Ansible 管理 Windows 机器

主机可能会返回错误:

"msg": "kerberos: Bad HTTP response returned from server. Code 500",
"unreachable": true

[玩转系统] 如何使用 Ansible 管理 Windows 机器

这是因为 WinRM 在此示例中使用 HTTP 而不是 HTTPS 进行连接。要忽略该错误,您必须允许 Windows 主机上的未加密流量

Set-Item -Path WSMan:\localhost\Service\AllowUnencrypted -Value true

现在,您可以使用 Ansible 在所有 Windows 主机上运行任意命令。例如,我想重置所有 Windows 计算机上的 DNS 缓存:

$ ansible windows_all -m win_shell -a "ipconfig /flushdns"

[玩转系统] 如何使用 Ansible 管理 Windows 机器

Windows 管理的 Ansible Playbook 示例

您的 Windows 主机现在已准备好运行 Ansible playbook。

例如,您需要使用 Ansible 在所有主机上运行 PowerShell 脚本(在本例中,我们将使用 PowerShell 获取主机上当前的 IP 或 DNS 设置)。创建一个剧本文件:

$ sudo mcedit /etc/ansible/playbooks/win-exec-powershell.yml
---
- name: win_powershell_exec
  hosts: windows_all
  tasks:
    - name: check DNS
      win_shell: |
        Get-DnsClientServerAddress -InterfaceIndex (Get-NetAdapter|where Status -eq "Up").ifindex -ErrorAction SilentlyContinue
      register: command_output
    - name: command output
      ansible.builtin.debug:

[玩转系统] 如何使用 Ansible 管理 Windows 机器

运行剧本:

$ ansible-playbook /etc/ansible/playbooks/win-exec-powershell.yml

在此示例中,该 playbook 已在所有 Windows 主机上成功执行,并返回了当前的 DNS 设置。

[玩转系统] 如何使用 Ansible 管理 Windows 机器

让我们看一下一些用于标准 Windows 主机管理任务的典型 Ansible 手册。

复制文件

  - name: Copy a single file
    win_copy:
      src: /home/sysops/files/test.ps1"
      dest: C:\Temp\test.ps1

创建文件:

- name: Create file
  win_file:
    path: C:\Temp\file.txt
    state: touch

删除文件:

- name: Delete file
  win_file:
    path: C:\Temp\file.txt
    state: absent

创建注册表参数

- name: Create reg dword 
  win_regedit: 
    path: HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\DataCollection
    name: AllowTelemetry
    data: 0
    type: dword

从 MSI 安装程序

- name: Install MSI package
  win_package:
    path: C:\Distr\adobereader.msi
    arguments:
      - /install
      - /passive
      - /norestart

运行 Windows 服务

- name: Run Windows Service
  win_service: 
    name: wuauserv
    state: started

[玩转系统] 如何使用 Ansible 管理 Windows 机器

安装 Windows Server 角色:

  - name: Install Windows Feature
    win_feature:
      name: SNMP-Service
      state: present

在 Windows Defender 防火墙中打开端口:

- name: Open SSH Port
  win_firewall_rule:
    name: port 22
    localport: 22
    action: allow
    direction: in
    protocol: tcp
    state: present
    enabled: yes

运行 PowerShell 脚本:

- name: Run PowerShell Script
  win_command: powershell.exe -ExecutionPolicy ByPass -File C:/temp/powershellscript.ps1

在本文中,您学习了如何通过 Ansible 管理 Windows 主机的配置。如果您的 Windows 主机未加入 Active Directory 域(它们位于工作组中),则使用 Ansible 对 Windows 主机进行远程配置管理可以成为使用域组策略进行配置的良好替代方案。

您还可以在适用于 Linux 的 Windows 子系统 (WSL) 中安装 Ansible。这将允许您运行 playbook,而无需为 Ansible 部署单独的 Linux 主机。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯