前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >讲真,Ansible 可以管理Windows?

讲真,Ansible 可以管理Windows?

作者头像
魏新宇
发布2018-04-24 14:40:10
2.8K0
发布2018-04-24 14:40:10
举报

前言:

本文是我和李尧老师一起实验。李尧是红帽高级培训讲师,目前负责红帽中国区员工内部技术培训与认证。

一、Ansible能对windows做什么操作?

Ansible真能管Windows么?

真能!

参照http://docs.ansible.com/ansible/latest/modules/list_of_windows_modules.html,我们看一下ansible能够对windows做的操作,目前有81项。

  • win_acl - Set file/directory/registry permissions for a system user or group
  • win_acl_inheritance - Change ACL inheritance
  • win_audit_policy_system - Used to make changes to the system wide Audit Policy.
  • win_audit_rule - Adds an audit rule to files, folders, or registry keys
  • win_certificate_store - Manages the certificate store
  • win_chocolatey - Manage packages using chocolatey
  • win_command - Executes a command on a remote Windows node
  • win_copy - Copies files to remote locations on windows hosts
  • win_defrag - Consolidate fragmented files on local volumes
  • win_disk_facts - Show the attached disks and disk information of the target host
  • win_disk_image - Manage ISO/VHD/VHDX mounts on Windows hosts
  • win_dns_client - Configures DNS lookup on Windows hosts
  • win_domain - Ensures the existence of a Windows domain.
  • win_domain_controller - Manage domain controller/member server state for a Windows host
  • win_domain_group - creates, modifies or removes domain groups
  • win_domain_membership - Manage domain/workgroup membership for a Windows host
  • win_domain_user - Manages Windows Active Directory user accounts
  • win_dotnet_ngen - Runs ngen to recompile DLLs after .NET updates
  • win_dsc - Invokes a PowerShell DSC configuration
  • win_environment - Modify environment variables on windows hosts
  • win_eventlog - Manage Windows event logs
  • win_eventlog_entry - Write entries to Windows event logs
  • win_feature - Installs and uninstalls Windows Features on Windows Server
  • win_file - Creates, touches or removes files or directories.
  • win_file_version - Get DLL or EXE file build version
  • win_find - Return a list of files based on specific criteria
  • win_firewall - Enable or disable the Windows Firewall
  • win_firewall_rule - Windows firewall automation
  • win_get_url - Fetches a file from a given URL
  • win_group - Add and remove local groups
  • win_group_membership - Manage Windows local group membership
  • win_hotfix - install and uninstalls Windows hotfixes
  • win_iis_virtualdirectory - Configures a virtual directory in IIS.
  • win_iis_webapplication - Configures IIS web applications
  • win_iis_webapppool - configures an IIS Web Application Pool
  • win_iis_webbinding - Configures a IIS Web site binding.
  • win_iis_website - Configures a IIS Web site.
  • win_lineinfile - Ensure a particular line is in a file, or replace an existing line using a back-referenced regular expression.
  • win_mapped_drive - maps a network drive for a user
  • win_msg - Sends a message to logged in users on Windows hosts.
  • win_msi - Installs and uninstalls Windows MSI files
  • win_nssm - NSSM - the Non-Sucking Service Manager
  • win_owner - Set owner
  • win_package - Installs/uninstalls an installable package
  • win_pagefile - Query or change pagefile configuration
  • win_path - Manage Windows path environment variables
  • win_ping - A windows version of the classic ping module
  • win_power_plan - Changes the power plan of a Windows system
  • win_product_facts - Provides Windows product information (product id, product key)
  • win_psexec - Runs commands (remotely) as another (privileged) user
  • win_psmodule - Adds or removes a Powershell Module.
  • win_rabbitmq_plugin - Manage RabbitMQ plugins
  • win_reboot - Reboot a windows machine
  • win_reg_stat - returns information about a Windows registry key or property of a key
  • win_regedit - Add, change, or remove registry keys and values
  • win_region - Set the region and format settings
  • win_regmerge - Merges the contents of a registry file into the windows registry
  • win_robocopy - Synchronizes the contents of two directories using Robocopy
  • win_route - Add or remove a static route.
  • win_say - Text to speech module for Windows to speak messages and optionally play sounds
  • win_scheduled_task - Manage scheduled tasks
  • win_scheduled_task_stat - Returns information about a Windows Scheduled Task
  • win_security_policy - changes local security policy settings
  • win_service - Manage and query Windows services
  • win_share - Manage Windows shares
  • win_shell - Execute shell commands on target hosts.
  • win_shortcut - Manage shortcuts on Windows
  • win_stat - returns information about a Windows file
  • win_tempfile - Creates temporary files and directories.
  • win_template - Templates a file out to a remote server.
  • win_timezone - Sets Windows machine timezone
  • win_toast - Sends Toast windows notification to logged in users on Windows 10 or later hosts
  • win_unzip - Unzips compressed files and archives on the Windows node
  • win_updates - Download and install Windows updates
  • win_uri - Interacts with webservices
  • win_user - Manages local Windows user accounts
  • win_user_right - Manage Windows User Rights
  • win_wait_for - Waits for a condition before continuing
  • win_wakeonlan - Send a magic Wake-on-LAN (WoL) broadcast packet
  • win_webpicmd - Installs packages using Web Platform Installer command-line
  • win_whoami - Returns information about the current user and process

由于篇幅有限,本文将调用一下四个module验证ansible对windows的配置和管理。

二、Ansible与Windows如何连接?

Ansible管理Linux,使用openssh,那么如何连接和管理Windows?

通过WinRM。

Windows 远程管理 (WinRM) 是 WS-Management 协议的 Microsoft 实现。该协议是基于简单对象访问协议 (SOAP) 的、防火墙友好的标准协议,使来自不同供应商的硬件和操作系统能够互操作。WS-Management 协议由硬件和软件制造商群体开发,作为一种公共标准,可用于与实现该协议的任何计算机设备远程交换管理数据。使用PowerShell对服务器远程管理,要在远程服务器上启用WinRM。

那么,ansible连接windows的认证方式都有哪些?

一共有四种方式(本文后续实验都是基于AD的操作,因此调用的是Kerberos的认证方式):

Basic认证方式是最基本的密码方式认证;Certificate认证的方式类似于ssh的认证方式。

接下来,我们通过实验方式,展示Ansible对windows的几个操作。

三、准备工作

首先,我们需要在windows的机器上配置winrm

然后,在ansible管理端安装所需的软件:

安装pywinrm,用于和windows机器通讯:

安装完毕后,确认可以ping通windows,证明ansible可以给windows发送ping module:

四、通过Ansible为windows安装AD

我们创建一个安装windows AD的role:

主任务如下:

这个playbook调用参数my_ad_vars.yml后,执行 win_ad_install这个role。

我们看一下参数文件的内容,Domain Name和AD Admin User变量从下面文件中获取:

AD Safe Password和AD Admin Password从inventory中获取:/etc/ansible/hosts

查看win_ad_install task的yaml:

我们分析一下这个task,以上图红框内容为例。

win_feature代表的是anisble的module,这在ansible官网有对应的用法和描述,它的作用是:Installs and uninstalls Windows Features on Windows Server

(http://docs.ansible.com/ansible/latest/modules/win_feature_module.html?highlight=win_feature)

我们看一下社区上的例子:

而在实验中,我们是要通过win_feature这个module,做AD-Domain-Services的安装。安装的时候,需要将AD管理工具装上( include_management_tools: yes),并且把AD的子功能模块也装上( include_sub_features: yes),而AD的子安装模块,对应类似内容如下:

都配置完成以后,执行ad.yml:

安装成功:

五、为Ansible节点配置kerberos认证

首先在ansible节点上安装kerberos模块,以便ansible可以通过kerberos和windows AD进行认证:

然后配置配置kerberos的配置文件,里面是要连接的AD信息:

首先确认可以通过/etc/ansible/host中的密码,可以对windows的administrator进行认证:

kinit是初始化kerberos票据:

查看票据是否获取成功以及票据的有效期等信息:

登录成功,说明ansible能通通过kerberos方式登录AD:

六、通过Ansible为windows配置OpenSSH

创建playbook的执行文件:

我们可以看到,做配置的主机是windows( hosts: windows),进行操作的时候,传参的文件是./ssh_var.yml,执行的role是:win_service_config

win_service_config这个role的task yaml文件如下:

我们把这个yaml中的红框部分内容拿出来分析一下:

win_chocolatey指的是ansible module,它的功能是: Manage packages using chocolatey

(http://docs.ansible.com/ansible/latest/modules/win_chocolatey_module.html?highlight=win_chocolatey)

在社区上可以看到它的用例:

而在这个红框中,我们需要的做的事情是:用win_chocolatey这个模块安装package,而package的名字是个变量( package_name ),在变量文件中。安装 package的时候,需要指定的参数也是变量: parameters;安装最新版本。

接下来,我们再看一下变量文件的内容:

执行playbook:

七、通过Ansible为windows创建AD组和用户

需要创建的AD用户和组列表如下:

首先创建一个playbook:

这个playbook是调用./ad_user_vars.yml文件的参数,执行win_ad_user这个role。

我们看一下win_ad_user这个role的内容:

我们分析下红框的内容。

win_domain_group是ansible的module,它的作用是:creates, modifies or removes domain groups。

我们在社区中可以看到它的用法:

在实验中,我们通过win_domain_group这个模块,创建一个AD组,名称为变量:item.group_name,范围是变量:item.group_scope,状态则需要这个组为present。

接下来,我们看一下变量文件:

我们帖一行出来:

user_info:- { name: 'james', firstname: 'James', surname: 'Jockey', password: 'redhat@123', group_name: 'dev', group_scope: 'domainlocal'}

可以看到,参数文件中包含多个变量的复制,group_name、group_scope都是有的,和main.yml中的内容可以对应起来。

执行playbook:

成功以后,通过ssh进行验证,可以通过新创建的AD用户,远程ssh登录windows。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-04-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 大魏分享 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档