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

ops/2024/10/25 2:35:19/

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 -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!!


http://www.ppmy.cn/ops/128215.html

相关文章

使用预训练的BERT进行金融领域问答

获取更多完整项目代码数据集,点此加入免费社区群 : 首页-置顶必看 1. 项目简介 本项目旨在开发并优化一个基于预训练BERT模型的问答系统,专注于金融领域的应用。随着金融市场信息复杂性和规模的增加,传统的信息检索方法难以高效…

高效车辆管理:SpringBoot实现指南

1系统概述 1.1 研究背景 随着计算机技术的发展以及计算机网络的逐渐普及,互联网成为人们查找信息的重要场所,二十一世纪是信息的时代,所以信息的管理显得特别重要。因此,使用计算机来管理车辆管理系统的相关信息成为必然。开发合适…

【黑马Redis原理篇】Redis网络模型

来源视频 [16,27] 文章目录 1.用户空间和内核空间空间划分缓冲区 2.IO模型2.1 阻塞IO2.2 非阻塞IO2.3 IO多路复用2.3.1 阻塞和非阻塞的对比2.3.2 IO多路复用2.3.3 监听FD方式、通知的方式,有多种实现 2.4 信号驱动IO2.5 异步IO2.6 真正的同步和异步 3.Redis是单线程…

十六、行为型(责任链模式)

责任链模式(Chain of Responsibility Pattern) 概念 责任链模式是一种行为型设计模式,它使多个对象都有机会处理请求,从而避免请求的发送者与接收者之间的耦合。将这些对象连成一条链,并沿着这条链传递请求&#xff0…

docker 误删gitlab文件,另类的删库跑路,如何进行恢复?

缘起:由于看到linux服务器内存快满了,于是本着责任感,想着清理一下内存,结果在看到docker文件占了20多个G,于是想着,我们就三个容器,为啥这么大,肯定是有诈,于是就一个个…

freeswitch-esl动态控制录制音频(开始、停止)

场景描述:在控制freeswitch中使用ESL socket连接,其实类型连接TCP差不多。 当A和B在通话中,我想录制它们通话内容,录制格式为wav格式音频文件。代码如下#include <iostream> #include <string> #include <esl/esl.h><

使用docker-compose搭建redis7集群-3主3从

下面是一个用于搭建 Redis 集群的 docker-compose.yml 示例文件&#xff0c;它会启动 6 个 Redis 节点&#xff08;3 主节点 3 从节点&#xff09;来构成一个最小的 Redis 集群。 同一个容器内网通讯没问题&#xff0c;但是你要是需要暴露到外网你需要用第二个yml 内网的 v…

『完整代码』坐骑召唤

创建一个按钮 作为召唤/消失坐骑的开关 将预制体放入指定文件夹 命名为Mount01 创建脚本并编写&#xff1a;CallMount.cs using UnityEngine; using UnityEngine.UI; public class CallMount : MonoBehaviour{public Button callBtn;GameObject mountPrefab;GameObject mountIn…