Ansible 具有许多专为 Windows 操作系统设计的模块,它使得自动化 Windows 任务变得简单。下面我将介绍一些常用的 Ansible Windows 模块,以及如何配置 Ansible 以管理 Windows 主机。
更详细的用法请参考:Using Ansible and Windows — Ansible Community Documentation
配置 Ansible 以管理 Windows
在开始使用 Ansible 管理 Windows 主机之前,需要进行一些配置:
-
安装必要的 Python 库:
- 确保在你的控制节点上安装
pywinrm
和requests-kerberos
或requests-ntlm
库,用于远程管理 Windows 主机。
pip install pywinrm requests-kerberos requests-ntlm
- 确保在你的控制节点上安装
-
更新 Ansible 配置文件:
- 在
ansible.cfg
文件中,添加以下内容以配置 Ansible 使用 WinRM 连接到 Windows 主机:
[defaults] inventory = hosts remote_user = your_user_name[inventory] enable_plugins = host_list, script, yaml, ini, auto, toml[winrm] transport = ntlm
- 在
-
配置 Windows 主机:
- 确保 Windows 主机上启用了 WinRM 服务,并且配置正确。这可以通过运行以下 PowerShell 脚本来完成:
# Configure LCM for Ansible [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 Set-ExecutionPolicy RemoteSigned -ForceConfigureRemotingForAnsible.ps1
常用的 Windows 模块
文件和目录管理
- win_file: 管理文件和目录的存在性、权限等属性。
- name: Ensure a file existsansible.windows.win_file:path: C:\path\to\file.txtstate: touch- name: Ensure a directory is presentansible.windows.win_file:path: C:\path\to\directorystate: directory
软件管理
- win_package: 安装或卸载软件包。
- name: Install a packageansible.windows.win_package:name: "Google Chrome"path: "C:\\path\\to\\chrome_installer.exe"state: present- name: Uninstall a packageansible.windows.win_package:name: "Google Chrome"state: absent
服务管理
- win_service: 管理 Windows 服务的状态。
- name: Ensure a service is runningansible.windows.win_service:name: wuauservstate: started- name: Ensure a service is stoppedansible.windows.win_service:name: wuauservstate: stopped
用户和组管理
- win_user: 管理 Windows 系统中的用户。
- name: Create a new useransible.windows.win_user:name: johndoepassword: "SecurePassword123!"state: present- name: Delete a useransible.windows.win_user:name: johndoestate: absent
- win_group: 管理 Windows 系统中的组。
- name: Create a new groupansible.windows.win_group:name: Adminsstate: present- name: Add a user to a groupansible.windows.win_group_membership:name: johndoegroups: Adminsstate: present
注册表管理
- win_regedit: 管理 Windows 注册表项和值。
- name: Add a registry keyansible.windows.win_regedit:path: HKLM:\Software\MyCompanyname: TestKeystate: present- name: Remove a registry keyansible.windows.win_regedit:path: HKLM:\Software\MyCompanyname: TestKeystate: absent
系统和环境配置
- win_environment: 管理 Windows 环境变量。
- name: Set a system environment variableansible.windows.win_environment:name: PATHvalue: "C:\path\to\directory"state: presentlevel: machine- name: Remove a system environment variableansible.windows.win_environment:name: OLD_VARstate: absentlevel: machine
示例:简单的 Windows 配置 Playbook
- name: Example playbook for managing Windows hostshosts: windowstasks:- name: Ensure C:\temp directory existsansible.windows.win_file:path: C:\tempstate: directory- name: Install 7-Zipansible.windows.win_package:name: 7-Zippath: C:\path\to\7zip_installer.exestate: present- name: Ensure Windows Update service is runningansible.windows.win_service:name: wuauservstart_mode: autostate: started- name: Set a system environment variableansible.windows.win_environment:name: MY_ENV_VARvalue: "MyValue"state: presentlevel: machine
通过这些模块和配置方法,你可以使用 Ansible 轻松地管理和自动化 Windows 主机。
Good Good Study, Day Day UP!!