一、前言
近期打算搞搞自動部署,因為是windows服務(wù)器,一些工具和系統(tǒng)支持都不是太好。最后發(fā)現(xiàn)ansible比較火,最重要的是他支持windows。本文主要就ansible 在windows使用環(huán)境搭建過程分享。
二、Ansible簡介
? ??ansible是新出現(xiàn)的自動化運維工具,基于Python開發(fā),集合了眾多運維工具(puppet、cfengine、chef、func、fabric)的優(yōu)點,實現(xiàn)了批量系統(tǒng)配置、批量程序部署、批量運行命令等功能。ansible是基于模塊工作的,本身沒有批量部署的能力。真正具有批量部署的是ansible所運行的模塊,ansible只是提供一種框架。主要包括:
(1)、連接插件connection plugins:負(fù)責(zé)和被監(jiān)控端實現(xiàn)通信;
(2)、host inventory:指定操作的主機,是一個配置文件里面定義監(jiān)控的主機;
(3)、各種模塊核心模塊、command模塊、自定義模塊;
(4)、借助于插件完成記錄日志郵件等功能;
(5)、playbook:劇本執(zhí)行多個任務(wù)時,非必需可以讓節(jié)點一次性運行多個任務(wù)。
三、Windows下Ansible工作模式
Ansible 從1.7+版本開始支持Windows,但前提是管理機必須為Linux系統(tǒng),遠程主機的通信方式也由SSH變更為PowerShell,同時管理機必須預(yù)安裝Python的Winrm模塊,方可和遠程Windows主機正常通信,但PowerShell需3.0+版本且Management Framework 3.0+版本,實測Windows 7 SP1和Windows Server 2008 R2及以上版本系統(tǒng)經(jīng)簡單配置可正常與Ansible通信。簡單總結(jié)如下:
(1) ? ?管理機必須為Linux系統(tǒng)且需預(yù)安裝Python Winrm模塊
(2) ? ?底層通信基于PowerShell,版本為3.0+,Management Framework版本為3.0+
(3) ? ?遠程主機開啟Winrm服務(wù)
四、Ansible管理機部署安裝
(1). 對管理主機的要求
? ??目前,只要機器上安裝了 Python 2.6 或 Python 2.7 (windows系統(tǒng)不可以做控制主機),都可以運行Ansible.
主機的系統(tǒng)可以是 Red Hat, Debian, CentOS, OS X, BSD的各種版本,等等.
? ? ? ?(2) 從源碼運行
? ??從項目的checkout中可以很容易運行Ansible,Ansible的運行不要求root權(quán)限,也不依賴于其他軟件,不要求運行后臺進程,也不需要設(shè)置數(shù)據(jù)庫.因此我們社區(qū)的許多用戶一直使用Ansible的開發(fā)版本,這樣可以利用最新的功能特性,也方便對項目做貢獻.因為不需要安裝任何東西,跟進Ansible的開發(fā)版相對于其他開源項目要容易很多.
從源碼安裝的步驟
$ git clone git://github.com/ansible/ansible.git --recursive$ cd ./ansible
使用 Bash:
$ source ./hacking/env-setup
如果沒有安裝pip, 請先安裝對應(yīng)于你的Python版本的pip:
$sudoeasy_install pip
以下的Python模塊也需要安裝:
$sudopipinstallparamiko PyYAML Jinja2 httplib2 six
一旦運行env-setup腳本,就意味著Ansible從源碼中運行起來了.默認(rèn)的inventory文件是 /etc/ansible/hosts。
配置hosts文件:
$? vim /etc/ansible/hosts
[windows]192.168.1.105ansible_ssh_user="Administrator"ansible_ssh_pass="123456"ansible_ssh_port=5985ansible_connection="winrm"ansible_winrm_server_cert_validation=ignore
192.168.1.105是windows服務(wù)器的IP。
至此,服務(wù)端配置完畢。
五、Windows系統(tǒng)配置
和Linux發(fā)版版稍有區(qū)別,遠程主機系統(tǒng)如為Windows需預(yù)先如下配置:
安裝Framework 3.0+
更改powershell策略為remotesigned
升級PowerShell至3.0+
設(shè)置Windows遠端管理,英文全稱WS-Management(WinRM)
(1)安裝Framework 3.0+
下載鏈接為:http://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_x86_x64.exe。 下載至本地后雙擊左鍵安裝即可,期間可能會多次重啟,電腦需正常連接Internet。
(2)更改powershell策略為remotesigned
? ??set-executionpolicy remotesigned
(3)升級PowerShell至3.0+
Window 7和Windows Server 2008 R2默認(rèn)安裝的有PowerShell,但版本號一般為2.0版本,所以我們需升級至3.0+,如下圖中數(shù)字1部分表示PowerShell版本過低需3.0+版本,數(shù)字2部分表示當(dāng)前PowerShell版本為2.0。
?下腳本保存至本地后,右鍵選擇“使用PowerShell運行”,執(zhí)行完畢重啟系統(tǒng)后,在PowerShell執(zhí)行Get-Host命令結(jié)果如下圖所示PowerShell版本為3.0為正常。?

1# Powershell script to upgrade a PowerShell 2.0 system to PowerShell 3.0 2# based on http://occasionalutility.blogspot.com/2013/11/everyday-powershell-part-7-powershell.html 3# 4# some Ansible modules that may use Powershell 3 features, so systems may need 5# to be upgraded.? This may be used by a sample playbook.? Refer to the windows 6# documentation on docs.ansible.com for details. 7# 8# - hosts: windows 9#? tasks:10#? ? - script: upgrade_to_ps3.ps11112# Get version of OS1314# 6.0 is 200815# 6.1 is 2008 R216# 6.2 is 201217# 6.3 is 2012 R2181920if($PSVersionTable.psversion.Major-ge3)21{22write-host"Powershell 3 Installed already; You don't need this"23? ? Exit24}2526$powershellpath="C:\powershell"2728functiondownload-file29{30param([string]$path, [string]$local)31$client= new-object system.net.WebClient32$client.Headers.Add("user-agent","PowerShell")33$client.downloadfile($path,$local)34}3536if(!(test-path$powershellpath))37{38New-Item -ItemType directory -Path$powershellpath39}404142# .NET Framework 4.0 is necessary.4344#if (($PSVersionTable.CLRVersion.Major) -lt 2)45#{46#? ? $DownloadUrl = "http://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_x86_x64.exe"47#? ? $FileName = $DownLoadUrl.Split('/')[-1]48#? ? download-file $downloadurl "$powershellpath\$filename"49#? ? ."$powershellpath\$filename" /quiet /norestart50#}5152#You may need to reboot after the .NET install if so just run the script again.5354# If the Operating System is above 6.2, then you already have PowerShell Version > 355if([Environment]::OSVersion.Version.Major-gt6)56{57write-host"OS is new; upgrade not needed."58? ? Exit59}606162$osminor= [environment]::OSVersion.Version.Minor6364$architecture=$ENV:PROCESSOR_ARCHITECTURE6566if($architecture-eq"AMD64")67{68$architecture="x64"69}? 70else71{72$architecture="x86"73} 7475if($osminor-eq1)76{77$DownloadUrl="http://download.microsoft.com/download/E/7/6/E76850B8-DA6E-4FF5-8CCE-A24FC513FD16/Windows6.1-KB2506143-"+$architecture+".msu"78}79elseif($osminor-eq0)80{81$DownloadUrl="http://download.microsoft.com/download/E/7/6/E76850B8-DA6E-4FF5-8CCE-A24FC513FD16/Windows6.0-KB2506146-"+$architecture+".msu"82}83else84{85# Nothing to do; In theory this point will never be reached.86? ? Exit87}8889$FileName=$DownLoadUrl.Split('/')[-1]90download-file$downloadurl"$powershellpath\$filename"9192Start-Process-FilePath"$powershellpath\$filename"-ArgumentList /quiet



(4)設(shè)置Windows遠端管理(WS-Management,WinRM)
winrm service 默認(rèn)都是未啟用的狀態(tài),先查看狀態(tài);如無返回信息,則是沒有啟動;
winrm enumerate winrm/config/listener
針對winrm service 進行基礎(chǔ)配置:
winrm quickconfig
查看winrm service listener:
winrm e winrm/config/listener
為winrm service 配置auth:
winrm set winrm/config/service/auth @{Basic="true"}
為winrm service 配置加密方式為允許非加密:
winrm set winrm/config/service @{AllowUnencrypted="true"}
好了,遠程Windows主機配置到此結(jié)束,我們驗證配置的是否有問題。
六、Windows下可用模塊測試
(1)win_ping —Windows系統(tǒng)下的ping模塊,常用來測試主機是否存活
ansible windows -m win_ping

(2)win_copy—拷貝文件到遠程Windows主機
傳輸/etc/passwd文件至遠程F:\file\目錄下
執(zhí)行命令:
ansible windows -m win_copy -a'src=/etc/passwd dest=F:\file\passwd'
返回結(jié)果:
1
2
3
4
5
6
7
192.168.1.105 | success >> {
????"changed":?true,
????"checksum":?"896d4c79f49b42ff24f93abc25c38bc1aa20afa0",
????"operation":?"file_copy",
????"original_basename":?"passwd",
????"size": 2563
}
部分返回結(jié)果詮釋:
“operation”: “file_copy”—執(zhí)行的操作為 file_copy;
“original_basename”: “passwd”—文件名為 passwd;
“size”: 2563—文件大小為 2563 bytes。
Playbook寫法如下:
1
2
3
4
5
6
---
- name: windows module example
??hosts: windows
??tasks:
?????- name: Move?file?on remote Windows Server from one location to another
???????win_file: src=/etc/passwd?dest=F:\file\passwd
(3)win_file —創(chuàng)建,刪除文件或目錄
刪除F:\file\passwd
執(zhí)行命令:
ansible windows -m win_file -a"path=F:\file\passwd state=absent"
返回結(jié)果:
1
2
3
192.168.1.105 | success >> {
????"changed":?true
}
七、總結(jié)
? ? 至此,環(huán)境搭建完成,可以在本地遠程控制windows服務(wù)器,如果想要自動部署,還需要碼powershell腳本來完成自動部署的相關(guān)功能。還未測試,待測試通過后再來分享。