Windows: How Does It Work Windows:如何工作
正如已经知道的,Ansible默认使用SSH管理Linux/unix。
自从1.7版本开始,Ansible同样包含了对Windows机器管理的支持。这是用本地的powershell远程,而不是SSH。
Ansible仍然在Linux控制机上运行,并且使用“winrm” Python模块来与远端主机通信。尽管不被微软或者Ansible支持,Linux控制机可以是一个Windows subsystem for linux(WSL) bash shell 支持。
远端主机无需额外的软件安装,Ansible仍然保留了agentless的架构,以维持它在linux/Unix上的流行度。
注意,这里需要你对Ansible有一个基本的了解,如果你对写一个linux的playbook还不了解,也许你应该先去了解以下。
Installing on the Control Machine 控制机上的安装
在一台linux控制机上执行:
pip install "pywinrm>=0.2.2"
你也可以使用Windows控制机,但是这需要使用 Windows Subsystem for Linux (WSL) bash shell。
注意:
使用a Windows Subsystem for Linux (WSL) bash shell 运行Ansible不是项目的目标,不支持这个特性是因为它限制了技术、特性和我们在主项目中使用的代码。
Windows Subsystem for Linux (Beta) 不被微软或Ansible支持,并且不应该在生产环境上使用
如果你想要实验 Windows Subsystem for Linux (WSL),首先启用Windows Subsystem for Linux,按照these instructions。这需要重启
Ansible也可以运行在cygwin,但是安装太麻烦,且功能性还不能确定是否有用。
综上,最好是在linux主机上运行控制机。
OptionLocal AccountsActive Directory AccountsCredential DelegationBasic
Yes
No
No
Certificate
Yes
No
No
Kerberos
No
Yes
Yes
NTLM
Yes
Yes
No
CredSSP
Yes
Yes
Yes 你可以指定你希望使用的哪一个认证选项,通过设置 ansible_winrm_transport 变量。
Inventory 主机清单
Ansible的Windows支持依赖一些标准变量来指出 远程主机的 username, password, and connection type (windows)。
这些变量可以很容易的在inventory中设置。在Ansible中,这被用来代替SSH-Keys或者密码输入。
[windows]
winserver1.example.com
winserver2.example.com
在 group_vars/windows.yml,定义如下 inventory 变量:
# it is suggested that these be encrypted with ansible-vault:
# ansible
-vault edit group_vars/windows.yml
ansible_user: Administrator
ansible_password: SecretPasswordGoesHere
ansible_port:
5986
ansible_connection: winrm
# The following is necessary
for Python 2.7.9+ (or any older Python that has backported SSLContext, eg, Python 2.7.5 on RHEL7) when using default WinRM self-signed certificates:
ansible_winrm_server_cert_validation: ignore
Windows System Prep Windows 系统前期准备
为了可以Ansible管理你的Windows机器,你必须启用并配置PowerShell远程。
为了自动化设置WinRM,你可以在远端机器上执行 examples/scripts/ConfigureRemotingForAnsible.ps1脚本,以管理员权限打开PowerShell控制台执行脚本。
这个例子脚本接收一些参数,管理员可以选择稍微修改默认的配置,在某些情况下,这是合适的。
传递 -CertValidityDays 选项来自定义产生的证书过期日期:
为了故障排除,ConfigureRemotingForAnsible.ps1 将写入每个变化到Windows EventLog。(useful when run unattendedly)
额外的选项 -verbose 可以用来展示更多的信息到屏幕上,关于正在执行什么。
注意:
在Window 7和Window2008 R2机器,因为Windows 管理框架的BUG,有必要安装热修复补丁,http://support.microsoft.com/kb/2842230来避免内存和堆栈异常。新安装的Windows 2008 R2系统没有升级到最新版本的均存在这个问题。
Windows 8.1 and Server 2012 R2 不受影响是因为他们自身默认使用的是 Windows Management Framework 4.0。
PS 阿里云机器不会有这样的问题。
Getting to PowerShell 3.0 or higher 获取PowerShell 3.0或者更高版本
PowerShell 3.0 or higher对于大部分Windows模块都是需要的,并且也需要运行上面的脚本。注意,PowerShell 3.0仅支持 Windows 7SP1以及Windows Server 2008 SP1,以及后续版本
找到 Ansible 的checkout版本,复制 examples/scripts/upgrade_to_ps3.ps1 版本到远程主机,并且以管理员权限在PowerShell 控制台运行。就可以运行 PowerShell 3 并可以通过上面介绍的 win_ping 技术来测试连通性。
What modules are available 什么模块是可用的
大部分Ansible core中的模块是为了组合Linux/Unix机器和任意web服务而写的,尽管仍然有各种不同的Windows专用模块。
“windows” subcategory of the Ansible module index 可以查看Windows Ansible模块索引
此外,下面的core模块/action-plugins同样可以在Windows上工作。
- name: Remove Appx packages (and their hindering file assocations)
win_shell:
|
Get
-AppxPackage -name "Microsoft.ZuneMusic" | Remove-AppxPackage
Get
-AppxPackage -name "Microsoft.ZuneVideo" | Remove-AppxPackage
Developers: Supported modules and how it works 开发者:支持模块以及工作原理
开发Ansible模块在 later section of the documentation描述,这关注点在于linux/unix。如果要写windows模块该怎么办?
对于windows来说,Ansible模块主要用PowerShell来实现,在继续之前,掠过linux/unix开发章节。windows模块
core和extras库中的windows模块在windows子目录中。自定义模块可以直接放到Ansible的library/中或者那些在ansible.cfg中添加的目录。文档存在一个.py文件中,使用相同的名字。例如,如果一个模块名为win_ping,那么就会嵌入文档在win_ping.py文件中,并且实际的PowerShell代码存在一个win_ping.ps1文件中。看一看源码,这样会更清晰。
Modules (ps1文件),应该如下开始:
#!powershell
#
<license>
# WANT_JSON
# POWERSHELL_COMMON
# code goes here, reading
in stdin as JSON and outputting JSON
- name: another raw module example demonstrating powershell one liner
hosts: windows
tasks:
- name: Move file on remote Windows Server from one location to another
win_command: Powershell.exe
"Move-Item C:\teststuff\myfile.conf C:\builds\smtp.conf"
牢记,使用 win_command or win_shell 总是会报告 “changed”,并且,你有责任来确保PowerShell可以按照预期恰当地处理幂等性(上面的move例子隐含就不会是幂等的),因此可能会使用到模块或者写一个模块。
以下是一个如何使用“win_stat”模块来测试文件存在性的例子。注意,由win_stat模块返回的数据和linux提供的相同模块的结果有些许不同:
- name: test stat module
hosts: windows
tasks:
- name: test stat module on file
win_stat: path
="C:/Windows/win.ini"
register: stat_file
Windows Contributions windows贡献
Ansible中的windows支持仍然是比较新的,欢迎贡献。无论是新的模块、现有模块的调整、文档、或者其他的什么。如果你愿意参与,请访问(stop by)Ansible开发邮件列表,say hi。
Developing Modules
List of Windows Modules