Bootstrap

Ansible 管理 Windows 机器配置过程。

Ansible 非常强大,这次尝试控制 Windows 机器做一些事情。

1 环境

1.1 ansible 管控机器

OS : CentOS 7.4

Ansible :2.4.1.0

1.2 Windows 机器

OS :windows_server_2008_r2_standard_sp1_x64 617598

framework :4.5

powershell :3.0

winrm

2 配置 ansible 管控机器

yum install ansible -y
pip install pywinrm 

如果管理的 Windows 机器有域控,那么还需要安装 kerberos

3 配置 Windows 机器

查看版本 打开 powershell (win+r ,输入 powershell 然后回车)

get-host

3.1 安装 framework

本次安装的是 framework 4.5,最低要3.0

http://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_x86_x64.exe

3.2 修改注册表

设置powershell本地脚本运行权限为remotesigned

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\ScriptedDiangnostics

修改该项下的 ExecutiomPolicy 的值为 remotesigned

3.3 升级 powershell

下载powershell-3.0的更新补丁,此补丁同时集成WMF3.0,winrm等

https://www.microsoft.com/en-us/download/details.aspx?id=34595

下载的时候选择 Windows6.1-KB2506143-x64.msu ,然后安装。安装完成后检查 powershell 的版本,可能需要重启机器。(注意:这个更新包依赖于.net3.0以上版本,如果未安装.net,会有提示“此更新不适应于您的计算机”)

3.4 配置 winrm

在 powershell 3.0 中执行

winrm qc 

会询问 “是否执行这些更改” 输入 y 确定执行更改。

设置 Auth 的 Basic 为 true ,service 中的 AllowUnencrypted 为 true

winrm set winrm/config/service '@{AllowUnencrypted="true"}'
winrm set winrm/config/service/auth '@{Basic="true"}'

查看配置使用 winrm get winrm/config

4 功能测试

配置ansible控制机配置方法有两种:第一种:在/etc/ansible/hosts中

[windows]
192.168.1.100
[windows:vars]
ansible_ssh_user="Administrator"
ansible_ssh_pass="paratera"
ansible_ssh_port=5985
ansible_connection="winrm"
ansible_winrm_server_cert_validation=ignore

第二种在/etc/ansible/hosts中

[windows]
192.168.1.100 ansible_ssh_user="Administrator" ansible_ssh_pass="paratera" ansible_ssh_port=5986 ansible_connection="winrm"

要注意的是 端口方面ssl即https方式的使用5986,http使用5985。

测试$ ansible windows -m win_ping192.168.1.100 | SUCCESS => {"changed": false,"ping": "pong"}

区别于控制Linux主机,windows主机的命令,都是使用了win_前缀,具体支持情况请见官网http://docs.ansible.com/ansible/list_of_windows_modules.html