一、Windons端配置
1.下載並安裝Microsoft .NET Framework 4.5
下載至本地后雙擊左鍵安裝即可,期間可能會多次重啟,電腦需正常連接Internet。
2. powershell策略修改:
(1)獲取/查看powershell策略:
get-executionpolicy //如果策略是remotesigned則不需要修改
(2)更改powershell策略為remotesigned:
set-executionpolicy remotesigned
3.升級PowerShell至3.0+(可以用get-host查看當前版本)
(1)下載文件upgrade_to_ps3.ps1,下載路徑:https://raw.githubusercontent.com/cchurch/ansible/devel/examples/scripts/upgrade_to_ps3.ps1
(2)右鍵選擇“使用PowerShell運行”,執行完畢重啟系統后,在PowerShell執行get-host命令PowerShell版本為3.0為正常。
4.設置Windows遠端管理(WS-Management,WinRM)
(1)winrm service 默認都是未啟用的狀態,先查看狀態: 執行命令“winrm e winrm/config/listener”,如返回錯誤信息,則是沒有啟動;
啟動winrm的命令是:winrm qc
(2)針對winrm service 進行基礎配置: winrm quickconfig
(3)查看winrm service listener: winrm e winrm/config/listener
(4)為winrm service 配置auth: winrm set winrm/config/service/auth ‘@{Basic="true"}’
(5)為winrm service 配置加密方式為允許非加密: winrm set winrm/config/service ‘@{AllowUnencrypted="true"}’
5.安裝修補程序:
在PowerShell v3.0上運行時,WinRM服務存在一個錯誤,它限制了WinRM可用的內存量。如果未安裝此修補程序,Ansible將無法在Windows主機上執行某些命令。
(1)下載安裝:
下載路徑:https://raw.githubusercontent.com/jborean93/ansible-windows/master/scripts/Install-WMF3Hotfix.ps1
右鍵選擇“使用PowerShell運行”,執行完畢重啟系統
注意:有些windows機器不能直接執行Install-WMF3Hotfix.ps1,需要轉到cmd命令下執行命令“powershell 文件目錄\Install-WMF3Hotfix.ps1”
二、linux端配置
1.安裝ansible
# yum install ansible -y
2.安裝pip
# easy_install pip
3.安裝pywinrm
# pip install pywinrm
4.配置hosts文件
# vim /etc/ansible/hosts [windows] 192.168.35.157 [windows:vars] ansible_ssh_user="admin" ansible_ssh_pass="sdts@1234" ansible_connection="winrm" ansible_ssh_port=5985 ansible_winrm_server_cert_validation=ignore
5. 測試是否能夠ping通
# ansible windows -m win_ping 192.168.35.157 | SUCCESS => { "changed": false, "ping": "pong" }
三、配置過程中踩過的坑:
(1)錯誤一:
# ansible windows -m win_ping 192.168.35.157 | UNREACHABLE! => { "changed": false, "msg": "plaintext: HTTPConnectionPool(host='192.168.35.155', port=5985): Max retries exceeded with url: /wsman (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x7f36188c7d10>, 'Connection to 192.168.35.157 timed out. (connect timeout=30)'))", "unreachable": true }
原因:
windows系統的防火牆開啟
解決方法:
關閉windows防火牆,Linux系統重新執行ansible腳本即可
(2)問題二:
# ansible windows -m win_ping An exception occurred during task execution. To see the full traceback, use -vvv. The error was: at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) 192.168.35.157 | FAILED! => { "changed": false, "msg": "Unhandled exception while executing module: Exception of type 'System.OutOfMemoryException' was thrown." }
原因:
沒有安裝修補程序
解決方法:
下載安裝Install-WMF3Hotfix.ps1,方法步驟如(一、5)所示