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

Ansible for Windows hosts(ansible.windows 模块介绍)

Ansible 具有许多专为 Windows 操作系统设计的模块,它使得自动化 Windows 任务变得简单。下面我将介绍一些常用的 Ansible Windows 模块,以及如何配置 Ansible 以管理 Windows 主机。

更详细的用法请参考:Using Ansible and Windows — Ansible Community Documentation

配置 Ansible 以管理 Windows

在开始使用 Ansible 管理 Windows 主机之前,需要进行一些配置:

  1. 安装必要的 Python 库

    • 确保在你的控制节点上安装 pywinrmrequests-kerberosrequests-ntlm 库,用于远程管理 Windows 主机。
    pip install pywinrm requests-kerberos requests-ntlm
    
  2. 更新 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
    
  3. 配置 Windows 主机

    • 确保 Windows 主机上启用了 WinRM 服务,并且配置正确。这可以通过运行以下 PowerShell 脚本来完成:
    # Configure LCM for Ansible
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    Set-ExecutionPolicy RemoteSigned -Force
    
    ConfigureRemotingForAnsible.ps1
    

常用的 Windows 模块

文件和目录管理
  • win_file: 管理文件和目录的存在性、权限等属性。
- name: Ensure a file exists
  ansible.windows.win_file:
    path: C:\path\to\file.txt
    state: touch

- name: Ensure a directory is present
  ansible.windows.win_file:
    path: C:\path\to\directory
    state: directory
软件管理
  • win_package: 安装或卸载软件包。
- name: Install a package
  ansible.windows.win_package:
    name: "Google Chrome"
    path: "C:\\path\\to\\chrome_installer.exe"
    state: present

- name: Uninstall a package
  ansible.windows.win_package:
    name: "Google Chrome"
    state: absent
服务管理
  • win_service: 管理 Windows 服务的状态。
- name: Ensure a service is running
  ansible.windows.win_service:
    name: wuauserv
    state: started

- name: Ensure a service is stopped
  ansible.windows.win_service:
    name: wuauserv
    state: stopped
用户和组管理
  • win_user: 管理 Windows 系统中的用户。
- name: Create a new user
  ansible.windows.win_user:
    name: johndoe
    password: "SecurePassword123!"
    state: present

- name: Delete a user
  ansible.windows.win_user:
    name: johndoe
    state: absent
  • win_group: 管理 Windows 系统中的组。
- name: Create a new group
  ansible.windows.win_group:
    name: Admins
    state: present

- name: Add a user to a group
  ansible.windows.win_group_membership:
    name: johndoe
    groups: Admins
    state: present
注册表管理
  • win_regedit: 管理 Windows 注册表项和值。
- name: Add a registry key
  ansible.windows.win_regedit:
    path: HKLM:\Software\MyCompany
    name: TestKey
    state: present

- name: Remove a registry key
  ansible.windows.win_regedit:
    path: HKLM:\Software\MyCompany
    name: TestKey
    state: absent
系统和环境配置
  • win_environment: 管理 Windows 环境变量。
- name: Set a system environment variable
  ansible.windows.win_environment:
    name: PATH
    value: "C:\path\to\directory"
    state: present
    level: machine

- name: Remove a system environment variable
  ansible.windows.win_environment:
    name: OLD_VAR
    state: absent
    level: machine

示例:简单的 Windows 配置 Playbook

- name: Example playbook for managing Windows hosts
  hosts: windows
  tasks:
    - name: Ensure C:\temp directory exists
      ansible.windows.win_file:
        path: C:\temp
        state: directory

    - name: Install 7-Zip
      ansible.windows.win_package:
        name: 7-Zip
        path: C:\path\to\7zip_installer.exe
        state: present

    - name: Ensure Windows Update service is running
      ansible.windows.win_service:
        name: wuauserv
        start_mode: auto
        state: started

    - name: Set a system environment variable
      ansible.windows.win_environment:
        name: MY_ENV_VAR
        value: "MyValue"
        state: present
        level: machine

通过这些模块和配置方法,你可以使用 Ansible 轻松地管理和自动化 Windows 主机。


Good Good Study, Day Day UP!!


http://www.kler.cn/news/359603.html

相关文章:

  • HiveOnSpark环境下,Spark 挂了问题排查思路
  • linux:NAPI
  • AnaTraf | 深入探讨DNS流量分析:保障网络稳定性的关键
  • 从零学习大模型(一)-----GPT3(上)
  • FFmpeg 4.3 音视频-多路H265监控录放C++开发二 : 18.04ubuntu安装,linux 下build ffmpeg 4.3 源码 并测试
  • C++与C语言的排序算法对比(插入,希尔,归并)
  • 2022年4月自考《数据库系统原理》04735试题
  • AI 自学 Lesson2 - 回归(Regression)
  • 想要加密电脑文件?2024年必备的8款企业文件加密软件推荐!
  • 4. Node.js Path模块
  • 最懂国人的私有笔记与博客项目,极空间部署高颜值双链笔记『Blossom 』
  • Xilinx UltraScale系列FPGA纯verilog图像缩放,工程项目解决方案,提供2套工程源码和技术支持
  • (K)MP有限状态自动机
  • VSCode中使用 Live Server 扩展时设置默认浏览器
  • 【Java后端】之 ThreadLocal 详解
  • 内核调度hh
  • 搜维尔科技:xsens动作捕捉+manus VR数据手套,元宇宙数字人制作流程
  • 开源限流组件分析(二):uber-go/ratelimit
  • 【机器学习】支持向量机SVM|高斯核 讲解及代码实现
  • RAG进阶形态之GraphRAG