一、前言
近期打算搞搞自動部署,因為是windows服務器,一些工具和系統支持都不是太好。最后發現ansible比較火,最重要的是他支持windows。本文主要就ansible 在windows使用環境搭建過程分享。
二、Ansible簡介
(1)、連接插件connection plugins:負責和被監控端實現通信;
(2)、host inventory:指定操作的主機,是一個配置文件里面定義監控的主機;
(3)、各種模塊核心模塊、command模塊、自定義模塊;
(4)、借助於插件完成記錄日志郵件等功能;
(5)、playbook:劇本執行多個任務時,非必需可以讓節點一次性運行多個任務。
三、Windows下Ansible工作模式
Ansible 從1.7+版本開始支持Windows,但前提是管理機必須為Linux系統,遠程主機的通信方式也由SSH變更為PowerShell,同時管理機必須預安裝Python的Winrm模塊,方可和遠程Windows主機正常通信,但PowerShell需3.0+版本且Management Framework 3.0+版本,實測Windows 7 SP1和Windows Server 2008 R2及以上版本系統經簡單配置可正常與Ansible通信。簡單總結如下:
(1) 管理機必須為Linux系統且需預安裝Python Winrm模塊
(2) 底層通信基於PowerShell,版本為3.0+,Management Framework版本為3.0+
(3) 遠程主機開啟Winrm服務
四、Ansible管理機部署安裝
(1). 對管理主機的要求
目前,只要機器上安裝了 Python 2.6 或 Python 2.7 (windows系統不可以做控制主機),都可以運行Ansible.
主機的系統可以是 Red Hat, Debian, CentOS, OS X, BSD的各種版本,等等.
(2) 從源碼運行
從項目的checkout中可以很容易運行Ansible,Ansible的運行不要求root權限,也不依賴於其他軟件,不要求運行后台進程,也不需要設置數據庫.因此我們社區的許多用戶一直使用Ansible的開發版本,這樣可以利用最新的功能特性,也方便對項目做貢獻.因為不需要安裝任何東西,跟進Ansible的開發版相對於其他開源項目要容易很多.
從源碼安裝的步驟
$ git clone git://github.com/ansible/ansible.git --recursive $ cd ./ansible
使用 Bash:
$ source ./hacking/env-setup
如果沒有安裝pip, 請先安裝對應於你的Python版本的pip:
$ sudo easy_install pip
以下的Python模塊也需要安裝:
$ sudo pip install paramiko PyYAML Jinja2 httplib2 six
一旦運行env-setup腳本,就意味着Ansible從源碼中運行起來了.默認的inventory文件是 /etc/ansible/hosts。
配置hosts文件:
$ vim /etc/ansible/hosts
[windows] 192.168.1.105 ansible_ssh_user="Administrator" ansible_ssh_pass="123456" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore
192.168.1.105是windows服務器的IP。
至此,服務端配置完畢。
和Linux發版版稍有區別,遠程主機系統如為Windows需預先如下配置:
- 安裝Framework 3.0+
- 更改powershell策略為remotesigned
- 升級PowerShell至3.0+
- 設置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

(3)升級PowerShell至3.0+
Window 7和Windows Server 2008 R2默認安裝的有PowerShell,但版本號一般為2.0版本,所以我們需升級至3.0+,如下圖中數字1部分表示PowerShell版本過低需3.0+版本,數字2部分表示當前PowerShell版本為2.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.ps1 11 12 # Get version of OS 13 14 # 6.0 is 2008 15 # 6.1 is 2008 R2 16 # 6.2 is 2012 17 # 6.3 is 2012 R2 18 19 20 if ($PSVersionTable.psversion.Major -ge 3) 21 { 22 write-host "Powershell 3 Installed already; You don't need this" 23 Exit 24 } 25 26 $powershellpath = "C:\powershell" 27 28 function download-file 29 { 30 param ([string]$path, [string]$local) 31 $client = new-object system.net.WebClient 32 $client.Headers.Add("user-agent", "PowerShell") 33 $client.downloadfile($path, $local) 34 } 35 36 if (!(test-path $powershellpath)) 37 { 38 New-Item -ItemType directory -Path $powershellpath 39 } 40 41 42 # .NET Framework 4.0 is necessary. 43 44 #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 /norestart 50 #} 51 52 #You may need to reboot after the .NET install if so just run the script again. 53 54 # If the Operating System is above 6.2, then you already have PowerShell Version > 3 55 if ([Environment]::OSVersion.Version.Major -gt 6) 56 { 57 write-host "OS is new; upgrade not needed." 58 Exit 59 } 60 61 62 $osminor = [environment]::OSVersion.Version.Minor 63 64 $architecture = $ENV:PROCESSOR_ARCHITECTURE 65 66 if ($architecture -eq "AMD64") 67 { 68 $architecture = "x64" 69 } 70 else 71 { 72 $architecture = "x86" 73 } 74 75 if ($osminor -eq 1) 76 { 77 $DownloadUrl = "http://download.microsoft.com/download/E/7/6/E76850B8-DA6E-4FF5-8CCE-A24FC513FD16/Windows6.1-KB2506143-" + $architecture + ".msu" 78 } 79 elseif ($osminor -eq 0) 80 { 81 $DownloadUrl = "http://download.microsoft.com/download/E/7/6/E76850B8-DA6E-4FF5-8CCE-A24FC513FD16/Windows6.0-KB2506146-" + $architecture + ".msu" 82 } 83 else 84 { 85 # Nothing to do; In theory this point will never be reached. 86 Exit 87 } 88 89 $FileName = $DownLoadUrl.Split('/')[-1] 90 download-file $downloadurl "$powershellpath\$filename" 91 92 Start-Process -FilePath "$powershellpath\$filename" -ArgumentList /quiet

(4)設置Windows遠端管理(WS-Management,WinRM)
winrm service 默認都是未啟用的狀態,先查看狀態;如無返回信息,則是沒有啟動;
winrm enumerate winrm/config/listener
針對winrm service 進行基礎配置:
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主機配置到此結束,我們驗證配置的是否有問題。
六、Windows下可用模塊測試
ansible windows -m win_ping

傳輸/etc/passwd文件至遠程F:\file\目錄下
執行命令:
ansible windows -m win_copy -a 'src=/etc/passwd dest=F:\file\passwd'
返回結果:
192.168.1.105 | success >> {
"changed": true,
"checksum": "896d4c79f49b42ff24f93abc25c38bc1aa20afa0",
"operation": "file_copy",
"original_basename": "passwd",
"size": 2563
}
部分返回結果詮釋:
- “operation”: “file_copy”—執行的操作為 file_copy;
- “original_basename”: “passwd”—文件名為 passwd;
- “size”: 2563—文件大小為 2563 bytes。
Playbook寫法如下:
--- - 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
刪除F:\file\passwd
執行命令:
ansible windows -m win_file -a "path=F:\file\passwd state=absent"
返回結果:
192.168.1.105 | success >> {
"changed": true
}