windows 系統(tǒng)要求
- PowerShell 3 或更高
- 不支持Cygwin
在Windows 7和Server 2008 R2計算機上,由于Windows Management Framework 3.0中的錯誤,可能需要安裝此修補程序 http://support.microsoft.com/kb/2842230 以避免收到內(nèi)存和堆棧溢出異常。已知新安裝的Server 2008 R2系統(tǒng)與Windows更新不兼容,具有此問題。
Windows 8.1和Server 2012 R2不受Windows Management Framework 4.0附帶的此問題的影響。
inventory 示例文件
[windows]
winserver1.example.com
winserver2.example.com
[windows:vars]
ansible_user=Administrator
ansible_password=123456
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
inventory 可用變量
- ansible_winrm_scheme:指定用于WinRM連接的連接方案(http或https)。https默認情況下可以使用,除非端口是5985。
- ansible_winrm_path:指定WinRM端點的備用路徑。/wsman默認情況下可以使用。
- ansible_winrm_realm:指定用于Kerberos身份驗證的領(lǐng)域。如果用戶名包含@,Ansible將@默認使用部分用戶名。
- ansible_winrm_transport:以一個逗號分隔的列表指定一個或多個傳輸。默認情況下,kerberos,plaintext如果已kerberos安裝模塊并定義了一個域,則“ 可用”將會使用,否則plaintext。
- ansible_winrm_server_cert_validation:指定服務(wù)器證書驗證模式(ignore或validate)。validatePython 2.7.9及更高版本的可選默認值將導(dǎo)致Windows自簽名證書的證書驗證錯誤。除非在WinRM偵聽器上配置了可驗證的證書,否則應(yīng)將其設(shè)置為ignore
- ansible_winrm_kerberos_delegation:設(shè)置為true在遠程主機上使用Kerberos時啟用命令委派。
- ansible_winrm_*:winrm.Protocol可以提供支持的任何其他關(guān)鍵字參數(shù)。
有什么模塊可以用的?
? add_host
? assert
? async
? debug
? fail
? fetch
? group_by
? include_vars
? meta
? pause
? raw
? script
? set_fact
? setup
? slurp
? template (also: win_template)
專門用作windows系統(tǒng)的模塊
http://docs.ansible.com/ansible/list_of_windows_modules.html
獲取 Windows Facts
ansible winhost.example.com -m setup
Windows Playbook示例
執(zhí)行powershell腳本
- name: test script module
hosts: windows
tasks:
- name: run test script
script: files/test_script.ps1
獲取ip地址信息
- name: test raw module
hosts: windows
tasks:
- name: run ipconfig
win_command: ipconfig
register: ipconfig
- debug: var=ipconfig
使用dos命令,移動文件
- name: another raw module example
hosts: windows
tasks:
- name: Move file on remote Windows Server from one location to another
win_command: CMD /C "MOVE /Y C:\teststuff\myfile.conf C:\builds\smtp.conf"
使用powershell命令,移動文件
- 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"
查看文件狀態(tài)
- name: test stat module
hosts: windows
tasks:
- name: test stat module on file
win_stat: path="C:/Windows/win.ini"
register: stat_file
- debug: var=stat_file
- name: check stat_file result
assert:
that:
- "stat_file.stat.exists"
- "not stat_file.stat.isdir"
- "stat_file.stat.size > 0"
- "stat_file.stat.md5"
本次實驗環(huán)境
windows os:Microsoft Windows Server 2008 R2 Enterprise with sp1 x64
ansible manager:centos 6.7 X64
ansible version: 2.2.1.0
python version:Python 2.6.6
windows 側(cè)配置
-
以管理員身份打開powershell
Paste_Image.png -
查看當(dāng)前ps版本
$PSVersionTable
Paste_Image.png -
系統(tǒng)自帶的powershell版本是2.0,需要更新至powershell 3 以上版本
a. 下載安裝Microsoft .NET Framework 4
https://www.microsoft.com/en-us/download/details.aspx?id=17851
b. 下載安裝Windows Management Framework 3.0
https://www.microsoft.com/en-us/download/details.aspx?id=34595
選擇 Windows6.1-KB2506143-x64.msu
c.安裝完,重啟服務(wù)器,查看powershell版本
$PSVersionTable
Paste_Image.png 配置winrm
mkdir C:\work
cd C:\work
Invoke-WebRequest -Uri https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1 -OutFile ConfigureRemotingForAnsible.ps1
powershell -ExecutionPolicy RemoteSigned .\ConfigureRemotingForAnsible.ps1 -SkipNetworkProfileCheck

ansible manager 配置
- 安裝ansible和pywinrm
yum -y install ansible
curl -sL https://bootstrap.pypa.io/get-pip.py | python
pip install pywinrm
- 配置hosts
[root@master ~]# cd /etc/ansible/
[root@master ansible]# cat win_hosts
[windows]
192.168.77.137
[windows:vars]
ansible_user=Administrator
ansible_password=Q123.123
ansible_port=5986
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore
- 測試通信
ansible -i win_hosts windows -m win_ping
Paste_Image.png - 查看ip地址
ansible -i win_hosts windows -m win_command -a "ipconfig"
Paste_Image.png
ansible -i win_hosts windows -m raw -a "ipconfig"
Paste_Image.png
修改上面的中文問題:
對命令輸出的信息進行utf-8編碼,修改winrm模塊的protocol.py
sed -i "s#tdout_buffer.append(stdout)#tdout_buffer.append(stdout.decode('gbk').encode('utf-8'))#g" /usr/lib/python2.6/site-packages/winrm/protocol.py
sed -i "s#stderr_buffer.append(stderr)#stderr_buffer.append(stderr.decode('gbk').encode('utf-8'))#g" /usr/lib/python2.6/site-packages/winrm/protocol.py

修改完之后,重新運行命令,中文已正常顯示。






