使用Ansible管理Windows

前言

本文主要介紹在如何使用Ansible管理Windows客戶端,Ansible官方提供了一個很方便的安裝腳本,對於外網用戶來講安裝真的很輕鬆,惋惜我遇到的問題是如何在內網部署,有相同煩惱的小夥伴不妨參考下html

輕輕鬆鬆使用Ansible管理Windows客戶端

更新歷史

2018年05月21日 - 初稿git

閱讀原文 - https://wsgzao.github.io/post...github

擴展閱讀shell

Ansible Windows Guides - http://docs.ansible.com/ansib...windows


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.bash

  • 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.

image

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服務器

  1. 在官方文檔中已經提到了在Windows中使用Ansible的最要前提,WinRM
  2. WinRM依賴Powershell 3.0以上版本的支持,牽扯出PowerShell 2.0 to PowerShell 3.0/5.0的問題
  3. 而Powershell升級則帶來.Net Framework是否跟隨升級至4.6.2/4.7.2的選擇
  4. 關於WinRM的參數配置能夠參考下面的連接Setting up a Windows Host

https://github.com/ansible/an...
https://github.com/ansible/an...ide

Setting up a Windows Host
http://docs.ansible.com/ansib...post

.NET Framework 4.7.2/4.6.2
https://www.microsoft.com/net...測試

Powershell 3.0
https://www.microsoft.com/en-...

Powershell 5.1
https://www.microsoft.com/en-...

我我的目前的建議是Win7/2008升級至Powershell 3.0,.Net Framework升級至4.6.2,其餘狀況須要能夠參考官方文檔後作決定

Ansible Windows Guides - http://docs.ansible.com/ansib...

客戶端

1.客戶端配置windows主機,以管理員身份打開powershell, 並查看當前ps版本
get-host
2.系統自帶的powershell版本是2.0,須要更新至powershell 3 以上版本
https://www.microsoft.com/net...
https://www.microsoft.com/en-...
3.安裝完重啓服務器查看powershell版本

.NET Framework 4.6以上版本沒法創建到信任根頒發機構的證書鏈
緣由:系統缺乏信任 Microsoft Root Certificate Authority 2011 根證書
下載:MicrosoftRootCertificateAuthority2011.cer
http://go.microsoft.com/fwlin...
運行 certmgr.msc
導入證書到「受信任的根證書頒發機構」

image

# 配置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/1JNV2...

  1. 安裝 .Net Framework 4.6.2(NDP462-KB3151800-x86-x64-AllOS-ENU.exe)
  2. 升級 Windows 7 SP1 和 Windows 2008 R2 SP1 的 PowerShell版本從2.0至3.0(Windows6.1-KB2506143-x64.msu)
  3. 執行.ConfigureRemotingForAnsible.ps1腳本開啓WinRM遠程管理服務

    powershell -ExecutionPolicy RemoteSigned .ConfigureRemotingForAnsible.ps1 -SkipNetworkProfileCheck

相關文章
相關標籤/搜索