Windows Support

windows下的執行方式

就如你剛所瞭解到的,Ansible預設是通過SSH協議來管理Linux/Unix伺服器.

從1.7版本開始,Ansible也開始支援Windows機器的管理.不過是通過本機的PowerShell來實現遠端管理,而不是SSH.

Ansible仍然通過一臺Linux系統機器來進行集中管理,使用Python的 “winrm” 模組來和遠端主機互動.

在管理的過程是 Ansible無需在遠端主機上安裝任何額外的軟體,Ansible仍然使用 agentless(非c/s架構) 來保證其在 Linux/Unix的流行度.

需要注意的是有開始這章前你最好對對 Ansible 有一個預先的瞭解,如果你還沒有寫過一個 Playbook, 那最好先跳到playbook的章節先了解熟悉再開始本章內容.

安裝管理機

On a Linux control machine:

pip install http://github.com/diyan/pywinrm/archive/master.zip#egg=pywinrm

如果你想通過活動目錄連線域帳戶進行釋出(相對本地帳戶在遠端主機上建立):

pip install kerberos

Kerberos 在 OS X 和許多 Linux 發行版中是預設安裝且配置好的.如果你的管理機上還沒有安裝,那你需要執行如上的命令.

Inventory

Ansible’s支援windows需要依賴於少量標準變數來表明遠端主機的username, password, and connection type (windows).這些變數大部分都很容易被設定好.在 Ansible 中通過用來代替 SSH-keys 或 密碼輸入:

[windows]
winserver1.example.com
winserver2.example.com

在 group_vars/windows.yml,定義如下 inventory 變數:

# it is suggested that these be encrypted with ansible-vault:
# ansible-vault edit group_vars/windows.yml

ansible_ssh_user: Administrator
ansible_ssh_pass: SecretPasswordGoesHere
ansible_ssh_port: 5986
ansible_connection: winrm

需要注意的是這裡的 ssh_port 不是真正的SSH協議的埠,but this is a holdover variable name from how Ansible is mostly an SSH-oriented system.(這句也沒看懂)再重複一遍,Windows 管理主機不是通過SSH協議.

如果你已經安裝了 kerberos 模組和 ansible_ssh_user 包括 @ (e.g. username@realm), Ansible會先嚐試Kerberos認證. * 這種方式主要用你通過Kerberos在遠端主機上的認證而不是 ansible_ssh_user * .如果上述辦法失敗了,要麼是因為你沒有在管理機上籤署(signed into)Kerberos,要麼是因為遠端主機上對應的域帳戶不可用,接著 Ansible 將返回原始(“plain”)username/password的認證方式.

當你使用 playbook 時,請不要忘記指定 –ask-vault-pass 提供密碼來解鎖檔案.

使用如下命令來測試你的配置,嘗試連線你的 Windows 節點.注意:這不是ICMP ping,只是利用 Windows 遠端工具來檢測 Ansible 的通道是否正常:

ansible windows [-i inventory] -m win_ping --ask-vault-pass

如果你還沒有在你的系統上做任何準備工作,那上面的命令是無法正常工作的. 在下面最近的章節將會介紹 “how to enable PowerShell remoting” - 如果有需要的話也將介紹 “how to upgrade PowerShell to a version that is 3 or higher” .

你可以稍後再執行該命令,以確保一切都能正常工作.

Windows System Prep

為了 Ansible 能管理你的windows機器,你將必須開啟並配置遠端機器上PowerShell.

為了能自動化設定 WinRM,你可以在遠端機器上執行 this PowerShell script

Admins有可能希望微調配置,例如延長證過期時間.

Note

Windows 7 和 Server 2008 R2 系統因為 Windows Management Framework 3.0的BUG,你必須安裝 hotfix http://support.microsoft.com/kb/2842230 來避免記憶體溢位(OOM)和堆棧異常. 新安裝的 Server 2008 R2 系統沒有升級到最新版本的均存在這個問題.

Windows 8.1 and Server 2012 R2 不受影響是因為他們自身預設使用的是 Windows Management Framework 4.0.

Getting to PowerShell 3.0 or higher

多數 Ansible Windows 模組需要 PowerShell 3.0 或更高版本,同時也需要在其基礎上執行安裝指令碼. 需要注意的是 PowerShell 3.0 只在 Windows 7 SP1 ,Windows Server 2008 SP1, 和更新的windows釋出版才被支援.

找到 Ansible 的checkout版本,複製 copy the examples/scripts/upgrade_to_ps3.ps1 指令碼到遠端主機同時以Administrator角色的帳戶執行 PowerShell 控制檯. 你就可以執行 PowerShell 3 並可以通過上面介紹的 win_ping 技術來測試連通性.

可用的windows模組

大多數 Ansible 模組尤其核心Ansible設計來組合 Linux/Unix 機器和任意 web services. 儘管 “windows” subcategory of the Ansible module index 列舉了各種各校的 Windows 模組.

瀏覽上面的索引檢視可用模組.

很多情況下, 其實沒有必要寫或者使用 Ansible 模組.

尤其, “script” 模組可以用來執行任意 PowerShell 指令碼,允許 Windows administrators 組所有使用者通過 PowerSehll 以非常本地化的方式做任何事情.就像如下的 playbook:

- hosts: windows
  tasks:
    - script: foo.ps1 --argument --other-argument

注意: 有一小部分 Ansible 模組不是以 “win” 開頭但依然是函數,包括 “slurp”,”raw”,和”setup”(fact 收集的工作原理).

開發者:支援的模組及工作原理

開發 ansible 模組主要在 later section of the documentation 介紹,專注於 Linux/Unix 平臺. 如果你想編寫 Windows 的 ansible 模組該怎麼辦呢?

Windows 平臺主要通過 PowerShell 模組實現. 開始之前可以先略過 Linux/Unix 模組開發章節.

Windows 模組在 Ansible “library/” 子目錄下的 “windows/” 子目錄下. 例如,如果一個模組命名為 “library/windows/win_ping”,那將會在 “win_ping” 檔案中嵌入一個文件,實際的 PowerShell 程式碼將存在 “win_ping.ps1” 檔案. 看下原始碼會有更深入的瞭解.

模組(ps1 files)檔案應該以如下格式開頭:

#!powershell
# <license>

# WANT_JSON
# POWERSHELL_COMMON

# code goes here, reading in stdin as JSON and outputting JSON

如上程式碼是為了告訴 ansible 合入一些程式碼並且 The above magic is necessary to tell Ansible to mix in some common code and also know how to push modules out. 常規程式碼包括好包裝例如雜湊資料結構,jason格式標準輸出,還有一些更有用的東西.常規 Ansible 有著重複利用 Python 程式碼的理念 - 這點 Windows 也是等同的. 你剛看到的 windows/ 模組只是一個開始. 附加模組已經被 git push 到 github上了.

提醒:控制機必須是Linux系統

Windows 控制機不是這個項目的目標. Ansible 不會開發這個功能,因為受限於技術,產品和我們未來主要項目使用的程式碼. 一臺Linux控制機是必須的,可以用來管理 Windows 機器. Cygwin 也是不被支援的,所以請不要要求 Ansible 基於 Cygwin 來執行.

Windows Facts

Just as with Linux/Unix, facts can be gathered for windows hosts, which will return things such as the operating system version. To see what variables are available about a windows host, run the following:

ansible winhost.example.com -m setup

Note that this command invocation is exactly the same as the Linux/Unix equivalent.

Windows Playbook Examples

Look to the list of windows modules for most of what is possible, though also some modules like “raw” and “script” also work on Windows, as do “fetch” and “slurp”.

Here is an example of pushing and running a PowerShell script:

- name: test script module
  hosts: windows
  tasks:
    - name: run test script
      script: files/test_script.ps1

Running individual commands uses the ‘raw’ module, as opposed to the shell or command module as is common on Linux/Unix operating systems:

- name: test raw module
  hosts: windows
  tasks:
    - name: run ipconfig
      raw: ipconfig
      register: ipconfig
    - debug: var=ipconfig

And for a final example, here’s how to use the win_stat module to test for file existence. Note that the data returned by the win_stat module is slightly different than what is provided by the Linux equivalent:

- name: test stat module
  hosts: windows
  tasks:
    - name: test stat module on file
      win_stat: path="C:/Windows/win.ini"
      register: stat_file

    - debug: var=stat_file

    - name: check stat_file result
      assert:
          that:
             - "stat_file.stat.exists"
             - "not stat_file.stat.isdir"
             - "stat_file.stat.size > 0"
             - "stat_file.stat.md5"

Again, recall that the Windows modules are all listed in the Windows category of modules, with the exception that the “raw”, “script”, and “fetch” modules are also available. These modules do not start with a “win” prefix.

Windows Contributions

Windows support in Ansible is still very new, and contributions are quite welcome, whether this is in the form of new modules, tweaks to existing modules, documentation, or something else. Please stop by the ansible-devel mailing list if you would like to get involved and say hi.

See also

Developing Modules
How to write modules
Playbooks
Learning ansible’s configuration management language
List of Windows Modules
Windows specific module list, all implemented in PowerShell
Mailing List
Questions? Help? Ideas? Stop by the list on Google Groups
irc.freenode.net
#ansible IRC chat channel