前言
本文主要介紹在如何使用Ansible管理Windows客戶端,Ansible官方提供了一個很方便的安裝指令碼,對於外網使用者來說安裝真的很輕鬆,可惜我遇到的問題是如何在內網部署,有相同煩惱的小夥伴不妨參考下
輕輕鬆鬆使用Ansible管理Windows客戶端
更新歷史
2018年05月21日 - 初稿
閱讀原文 - https://wsgzao.github.io/post/ansible-windows/
擴充套件閱讀
Ansible Windows Guides - http://docs.ansible.com/ansible/latest/user_guide/windows.html
Ansible Windows Support
Ansible在2.3版本之前對於Windows支援的並不算很友好,從2.4版本開始已經可以使用原生模組實現很多需求
Because Windows is a non-POSIX-compliant operating system, there are differences between how Ansible interacts with them and the way Windows works. These guides will highlight some of the differences between Linux/Unix hosts and hosts running Windows.
- Ansible’s supported Windows versions generally match those under current and extended support from Microsoft. Supported desktop OSs include Windows 7, 8.1, and 10, and supported server OSs are Windows Server 2008, 2008 R2, 2012, 2012 R2, and - 2016.
- Ansible requires PowerShell 3.0 or newer and at least .NET 4.0 to be installed on the Windows host.
- A WinRM listener should be created and activated. More details for this can be found below.
Ansible does not support managing Windows XP or Server 2003 hosts. The supported operating system versions are:
Windows Server 2008 Windows Server 2008 R2 Windows Server 2012 Windows Server 2012 R2 Windows Server 2016 Windows 7 Windows 8.1 Windows 10
- 在官方文件中已經提到了在Windows中使用Ansible的最要前提,WinRM
- WinRM依賴Powershell 3.0以上版本的支援,牽扯出PowerShell 2.0 to PowerShell 3.0/5.0的問題
- 而Powershell升級則帶來.Net Framework是否跟隨升級至4.6.2/4.7.2的選擇
- 關於WinRM的引數配置可以參考下面的連結Setting up a Windows Host
https://github.com/ansible/ansible/blob/devel/examples/scripts/upgrade_to_ps3.ps1 https://github.com/ansible/ansible/blob/devel/examples/scripts/ConfigureRemotingForAnsible.ps1
Setting up a Windows Host http://docs.ansible.com/ansible/latest/user_guide/windows_setup.html
.NET Framework 4.7.2/4.6.2 https://www.microsoft.com/net/download/dotnet-framework-runtime
Powershell 3.0 https://www.microsoft.com/en-us/download/details.aspx?id=34595
Powershell 5.1 https://www.microsoft.com/en-us/download/details.aspx?id=54616
我個人目前的建議是Win7/2008升級至Powershell 3.0,.Net Framework升級至4.6.2,其他情況需要可以參考官方文件後做決定
Ansible Windows Guides - http://docs.ansible.com/ansible/latest/user_guide/windows.html
客戶端
1.客戶端配置windows主機,以管理員身份開啟powershell, 並檢視當前ps版本 get-host 2.系統自帶的powershell版本是2.0,需要更新至powershell 3 以上版本 https://www.microsoft.com/net/download/dotnet-framework-runtime https://www.microsoft.com/en-us/download/details.aspx?id=34595 3.安裝完重啟伺服器檢視powershell版本
.NET Framework 4.6以上版本無法建立到信任根頒發機構的證書鏈 原因:系統缺少信任 Microsoft Root Certificate Authority 2011 根證書 下載:MicrosoftRootCertificateAuthority2011.cer http://go.microsoft.com/fwlink/?LinkID=747875&clcid=0x409 執行 certmgr.msc 匯入證書到“受信任的根證書頒發機構”
# 配置winrm
mkdir C:\temp
cd C:\temp
# 下載ConfigureRemotingForAnsible.ps1
https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1
# 開啟WinRM服務
powershell -ExecutionPolicy RemoteSigned .\ConfigureRemotingForAnsible.ps1 -SkipNetworkProfileCheck
複製程式碼
服務端
# 服務端使用pip安裝pywinrm
pip install pywinrm
# 功能測試,配置ansible控制機
vi /etc/ansible/hosts
[windows]
192.168.67.139
[windows:vars]
ansible_user=Administrator
ansible_password=Admin123
ansible_port=5986
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore
# 要注意的是埠方面ssl即https方式的使用5986,http使用5985
# 測試ping通訊
ansible windows -m win_ping
# 檢視ip地址
ansible windows -m win_command -a "ipconfig"
複製程式碼
網盤下載
Windows作為客戶端所需的軟體包我上傳到百度網盤,.Net Framework安裝失敗提示證書錯誤記得手動匯入MicrosoftRootCertificateAuthority2011.cer
https://pan.baidu.com/s/1JNV2pXjwUn14ojAtdEH_Sg
-
安裝 .Net Framework 4.6.2(NDP462-KB3151800-x86-x64-AllOS-ENU.exe)
-
升級 Windows 7 SP1 和 Windows 2008 R2 SP1 的 PowerShell版本從2.0至3.0(Windows6.1-KB2506143-x64.msu)
-
執行.\ConfigureRemotingForAnsible.ps1指令碼開啟WinRM遠端管理服務
powershell -ExecutionPolicy RemoteSigned .\ConfigureRemotingForAnsible.ps1 -SkipNetworkProfileCheck