轉(zhuǎn)載:http://blog.51cto.com/mengix/1862959
一、必須安裝在ansible的Linux管控主機(jī)上安裝控制Windows的組件
??1.安裝pywinrm,kerberos
二、配置Windows主機(jī)
??1.安裝安裝Framework?4.5(最低3.0)
??2.修改注冊(cè)列表:設(shè)置powershell本地腳本運(yùn)行權(quán)限為remotesigned
??2.升級(jí)到powershell-3.0
??3.配置winrm
三、功能測(cè)試
下面開(kāi)始...
一、配置ansible管控機(jī)器
使用Python的pip安裝pywinrm及kerberos
$?sudopip?installhttp://github.com/diyan/pywinrm/archive/master.zip#egg=pywinrm
$?sudopip?installkerberos
在安裝kerberos之前需要安裝
1$?sudoapt-get?installlibkrb5-dev
否則報(bào)錯(cuò)為
$?sudopip?installkerberos
running?build_ext
building?'kerberos'extension
creating?build
creating?build/temp.linux-x86_64-2.7
creating?build/temp.linux-x86_64-2.7/src
gcc?-pthread?-fno-strict-aliasing?-DNDEBUG?-g?-fwrapv?-O2?-Wall?-Wstrict-prototypes?-fPIC?-I/usr/include/python2.7?-c?src/base64.c?-o?build/temp.linux-x86_64-2.7/src/base64.o?sh:?1:?krb5-config:?not?found
gcc:?error:?sh::?No?such?fileor?directory
gcc:?error:?1::?No?such?fileor?directory
gcc:?error:?krb5-config::?No?such?fileor?directory
gcc:?error:?not:?No?such?fileor?directory
gcc:?error:?found:?No?such?fileor?directory
error:?command'gcc'failed?with?exitstatus?1
----------------------------------------
Command?/usr/bin/python-c?"import?setuptools;__file__='/etc/ansible/scripts/build/kerberos/setup.py';exec(compile(open(__file__).read().replace('\r\n',?'\n'),?__file__,?'exec'))"install--single-version-externally-managed?--record?/tmp/pip-Q3eBF1-record/install-record.txt?failed?with?error?code?1
Storing?complete?log?in/home/ansible/.pip/pip.log
二、配置windows主機(jī)
查看系統(tǒng)中.Net版本及powershell版本
.Net版本為CLRVersion;powershell使用$host中Version
PS?C:\Users\ABC>?$psversiontable
Name???????????????????????????Value
----???????????????????????????-----
CLRVersion???????????????????????2.0.50727.5485
BuildVersion??????????????????????6.1.7601.17514
PSVersion????????????????????????2.0
WSManStackVersion????????????????????2.0
PSCompatibleVersions??????????????????1.0,?2.0}
SerializationVersion??????????????????1.1.0.1
PSRemotingProtocolVersion????????????????2.1
PS?C:\Users\ABC>?$host
Name???????????????:?ConsoleHost
Version??????????????:?2.0
InstanceId????????????:?586e26ae-8630-4add-aa4c-d864726f5384
UI????????????????:?System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture??????????:?zh-CN
CurrentUICulture?????????:?zh-CN
PrivateData????????????:?Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed?????????:?False
Runspace?????????????:?System.Management.Automation.Runspaces.LocalRunspace
1.安裝安裝Framework 4.5(最低3.0)
2.修改注冊(cè)列表:設(shè)置powershell本地腳本運(yùn)行權(quán)限為remotesigned
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft\PowerShell

2.升級(jí)到powershell-3.0及winrm
官網(wǎng)上說(shuō)下載并執(zhí)行下述腳本,但是一直不成功,原因是一直下載不下來(lái)安裝包(可能的情況是網(wǎng)絡(luò)不可達(dá),大家懂得)
https://github.com/cchurch/ansible/blob/devel/examples/scripts/upgrade_to_ps3.ps1
于是手動(dòng)找通過(guò)微軟官網(wǎng)找到下載地址win7_x64升級(jí)包如下
https://www.microsoft.com/en-us/download/confirmation.aspx?id=34595
下載powershell-3.0的更新補(bǔ)丁,此補(bǔ)丁同時(shí)集成WMF3.0,winrm等
地址:
(注意:這個(gè)更新包依賴(lài)于.net3.0以上版本,如果未安裝.net,會(huì)有提示“此更新不適應(yīng)于您的計(jì)算機(jī)”)
安裝完成之后需要重啟,重啟后檢驗(yàn)powershell版本
1>?get-host

3.配置winrm
3.1 下載下面腳本,使用powershell運(yùn)行,自動(dòng)配置好winrm
https://github.com/ansible/ansible/blob/devel/examples/scripts/ConfigureRemotingForAnsible.ps1
(注意:官網(wǎng)教程直到上面結(jié)束,但是一般執(zhí)行完之后檢驗(yàn)winrm是否已經(jīng)運(yùn)行,因?yàn)閍nsible管控機(jī)需要通過(guò)端口進(jìn)行訪問(wèn)winrm)
3.2 在計(jì)算機(jī)上運(yùn)行winrm服務(wù)
powershell 3.0中執(zhí)行
1>?winrm?qc

這個(gè)提示,請(qǐng)將網(wǎng)絡(luò)設(shè)置將工作網(wǎng)絡(luò) 改成 家庭網(wǎng)絡(luò)?就可以了,特殊情況可能需要重啟,

配置好網(wǎng)絡(luò)類(lèi)型重新啟動(dòng)服務(wù),結(jié)果如下:

開(kāi)啟后,需要查看配置是否已經(jīng)開(kāi)啟
3.3 ?主要是下圖Auth中Basic設(shè)置為true,service中AllowUnencrypted設(shè)置為true。
>?winrm?set?winrm/config/service?'@{AllowUnencrypted="true"}'
>?winrm?set?winrm/config/service/auth?'@{Basic="true"}'

三、功能測(cè)試
配置ansible控制機(jī)
配置方法有兩種:
第一種:
在/etc/ansible/hosts中
[windows]
192.168.1.11
[windows:vars]
ansible_ssh_user="Administrator"
ansible_ssh_pass="123456"
ansible_ssh_port=5986
ansible_connection="winrm"
$?ansible?windows?-m?win_ping
192.168.1.11?|?SUCCESS?=>?{
????"changed":?false,?
????"ping":?"pong"
}
第二種
在/etc/ansible/hosts中
[windows]
192.168.1.11?ansible_ssh_user="Administrator"ansible_ssh_pass="123456"ansible_ssh_port=5986?ansible_connection="winrm"
要注意的是 端口方面ssl即https方式的使用5986,http使用5985。
區(qū)別于控制Linux主機(jī),win主機(jī)的命令,需要加上win_,具體支持情況請(qǐng)見(jiàn)官網(wǎng)
http://docs.ansible.com/ansible/list_of_windows_modules.html
我這里做部分常用測(cè)試
傳文件
刪文件
創(chuàng)建用戶(創(chuàng)建用戶,更改密碼,創(chuàng)建可以進(jìn)行遠(yuǎn)程桌面登錄的用戶)
執(zhí)行cmd命令(重啟電腦)
1. 傳文件到windows系統(tǒng)
$?ansible?windows?-m?win_copy?-a?"src=/etc/passwd?dest=e:\share"
192.168.1.11?|?SUCCESS?=>?{
????"changed":?true,?
????"checksum":?"a82c2bd7de3d9a5c5adddd5e4fc7cbf1703720b7",?
????"operation":?"file_copy",?
????"original_basename":?"passwd",?
????"size":?1755
}

2. 刪除文件
$?ansible?windows?-m?win_file?-a?"dest=e:\share\passwd?state=absent"
192.168.1.11?|?SUCCESS?=>?{
????"changed":?true
}

3.創(chuàng)建用戶
$?ansible?windows?-m?win_user?-a?"name=aa?passwd=123456"
192.168.1.11?|?SUCCESS?=>?{
????"account_disabled":?false,?
????"account_locked":?false,?
????"changed":?true,?
????"description":?"",?
????"fullname":?"aa",?
????"groups":?[],?
????"name":?"aa",?
????"password_expired":?true,?
????"password_never_expires":?false,?
????"path":?"WinNT://WORKGROUP/ABC-PC/aa",?
????"sid":?"S-1-5-21-37586581-19248684-93829760-1006",?
????"state":?"present",?
????"user_cannot_change_password":?false
}
場(chǎng)景需求:
創(chuàng)建一個(gè)名叫user1的管理員用戶,要求能夠遠(yuǎn)程訪問(wèn)
$?ansible?windows?-m?win_user?-a?"name=user1?password=123?groups='Administrators,Remote?Desktop?Users'"
192.168.1.11?|?SUCCESS?=>?{
????"account_disabled":?false,?
????"account_locked":?false,?
????"changed":?true,?
????"description":?"",?
????"fullname":?"user1",?
????"groups":?[
????????{
????????????"name":?"Administrators",?
????????????"path":?"WinNT://WORKGROUP/ABC-PC/Administrators"
????????},?
????????{
????????????"name":?"Remote?Desktop?Users",?
????????????"path":?"WinNT://WORKGROUP/ABC-PC/Remote?Desktop?Users"
????????}
????],?
????"name":?"user1",?
????"password_expired":?false,?
????"password_never_expires":?false,?
????"path":?"WinNT://WORKGROUP/ABC-PC/user1",?
????"sid":?"S-1-5-21-375864581-19248684-93852960-1009",?
????"state":?"present",?
????"user_cannot_change_password":?false
}
4. 執(zhí)行cmd命令
重啟的第一種方式
1$?ansible?windows?-m?win_shell?-a?"shutdown?-r?-t?1"
第二種方式,不加參數(shù)等同于第一種方式
1$?ansible?windows?-m?win_reboot