Ansible 小手冊系列 二十一(管理windows系統(tǒng))

windows 系統(tǒng)要求

  • PowerShell 3 或更高
  • 不支持Cygwin

在Windows 7和Server 2008 R2計算機上,由于Windows Management Framework 3.0中的錯誤,可能需要安裝此修補程序 http://support.microsoft.com/kb/2842230 以避免收到內(nèi)存和堆棧溢出異常。已知新安裝的Server 2008 R2系統(tǒng)與Windows更新不兼容,具有此問題。
Windows 8.1和Server 2012 R2不受Windows Management Framework 4.0附帶的此問題的影響。

inventory 示例文件

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

[windows:vars]
ansible_user=Administrator
ansible_password=123456
ansible_port=5986
ansible_connection=winrm
# The following is necessary for Python 2.7.9+ (or any older Python that has backported SSLContext, eg, Python 2.7.5 on RHEL7) when using default WinRM self-signed certificates:
ansible_winrm_server_cert_validation=ignore

inventory 可用變量

  • ansible_winrm_scheme:指定用于WinRM連接的連接方案(http或https)。https默認情況下可以使用,除非端口是5985。
  • ansible_winrm_path:指定WinRM端點的備用路徑。/wsman默認情況下可以使用。
  • ansible_winrm_realm:指定用于Kerberos身份驗證的領(lǐng)域。如果用戶名包含@,Ansible將@默認使用部分用戶名。
  • ansible_winrm_transport:以一個逗號分隔的列表指定一個或多個傳輸。默認情況下,kerberos,plaintext如果已kerberos安裝模塊并定義了一個域,則“ 可用”將會使用,否則plaintext。
  • ansible_winrm_server_cert_validation:指定服務(wù)器證書驗證模式(ignore或validate)。validatePython 2.7.9及更高版本的可選默認值將導(dǎo)致Windows自簽名證書的證書驗證錯誤。除非在WinRM偵聽器上配置了可驗證的證書,否則應(yīng)將其設(shè)置為ignore
  • ansible_winrm_kerberos_delegation:設(shè)置為true在遠程主機上使用Kerberos時啟用命令委派。
  • ansible_winrm_*:winrm.Protocol可以提供支持的任何其他關(guān)鍵字參數(shù)。

有什么模塊可以用的?

? add_host
? assert
? async
? debug
? fail
? fetch
? group_by
? include_vars
? meta
? pause
? raw
? script
? set_fact
? setup
? slurp
? template (also: win_template)

專門用作windows系統(tǒng)的模塊
http://docs.ansible.com/ansible/list_of_windows_modules.html

獲取 Windows Facts

ansible winhost.example.com -m setup

Windows Playbook示例

執(zhí)行powershell腳本
- name: test script module
  hosts: windows
  tasks:
    - name: run test script
      script: files/test_script.ps1

獲取ip地址信息
- name: test raw module
  hosts: windows
  tasks:
    - name: run ipconfig
      win_command: ipconfig
      register: ipconfig
    - debug: var=ipconfig

使用dos命令,移動文件
- name: another raw module example
  hosts: windows
  tasks:
     - name: Move file on remote Windows Server from one location to another
       win_command: CMD /C "MOVE /Y C:\teststuff\myfile.conf C:\builds\smtp.conf"

使用powershell命令,移動文件
- name: another raw module example demonstrating powershell one liner
  hosts: windows
  tasks:
     - name: Move file on remote Windows Server from one location to another
       win_command: Powershell.exe "Move-Item C:\teststuff\myfile.conf C:\builds\smtp.conf"

查看文件狀態(tài)
- 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"

本次實驗環(huán)境

windows os:Microsoft Windows Server 2008 R2 Enterprise with sp1 x64
ansible manager:centos 6.7 X64
ansible version: 2.2.1.0
python version:Python 2.6.6

windows 側(cè)配置

  1. 以管理員身份打開powershell


    Paste_Image.png
  2. 查看當(dāng)前ps版本
    $PSVersionTable

    Paste_Image.png

  3. 系統(tǒng)自帶的powershell版本是2.0,需要更新至powershell 3 以上版本
    a. 下載安裝Microsoft .NET Framework 4
    https://www.microsoft.com/en-us/download/details.aspx?id=17851
    b. 下載安裝Windows Management Framework 3.0
    https://www.microsoft.com/en-us/download/details.aspx?id=34595
    選擇 Windows6.1-KB2506143-x64.msu
    c.安裝完,重啟服務(wù)器,查看powershell版本
    $PSVersionTable

    Paste_Image.png

  4. 配置winrm

mkdir C:\work
cd C:\work
Invoke-WebRequest -Uri https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1 -OutFile ConfigureRemotingForAnsible.ps1
powershell -ExecutionPolicy RemoteSigned .\ConfigureRemotingForAnsible.ps1 -SkipNetworkProfileCheck
Paste_Image.png

ansible manager 配置

  1. 安裝ansible和pywinrm
yum -y install ansible
curl -sL https://bootstrap.pypa.io/get-pip.py | python
pip install pywinrm
  1. 配置hosts
[root@master ~]# cd /etc/ansible/
[root@master ansible]# cat win_hosts 
[windows]
192.168.77.137
[windows:vars]
ansible_user=Administrator
ansible_password=Q123.123
ansible_port=5986
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore
  1. 測試通信
    ansible -i win_hosts windows -m win_ping
    Paste_Image.png
  2. 查看ip地址
    ansible -i win_hosts windows -m win_command -a "ipconfig"
    Paste_Image.png

    ansible -i win_hosts windows -m raw -a "ipconfig"
    Paste_Image.png

修改上面的中文問題:
對命令輸出的信息進行utf-8編碼,修改winrm模塊的protocol.py

sed -i "s#tdout_buffer.append(stdout)#tdout_buffer.append(stdout.decode('gbk').encode('utf-8'))#g" /usr/lib/python2.6/site-packages/winrm/protocol.py
sed -i "s#stderr_buffer.append(stderr)#stderr_buffer.append(stderr.decode('gbk').encode('utf-8'))#g" /usr/lib/python2.6/site-packages/winrm/protocol.py
Paste_Image.png

修改完之后,重新運行命令,中文已正常顯示。


Paste_Image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、前言 如《第1章Ansible發(fā)展史》介紹,作為關(guān)注度最高的集中化管理工具,Ansible同樣支持Window...
    木納哥閱讀 3,902評論 0 3
  • 由于當(dāng)今社會人們的生活習(xí)慣,頸肩腰腿痛、運動損傷和關(guān)節(jié)退變越來越影響到人們的生活質(zhì)量。如何健康的使用關(guān)節(jié),又如何恢...
    張外外外閱讀 715評論 0 0
  • 體驗入: 能達到您理解的成功人士標準,他就是您值得您欣賞和學(xué)習(xí)的榜樣。如果他還能再次經(jīng)受風(fēng)雨,從頭開始那絕非一般的...
    能量在此閱讀 225評論 0 1
  • 鄰居二姐很會活 兒女住樓尊為佛 二姐不想這么活 回鄉(xiāng)養(yǎng)豬又養(yǎng)鵝 天天賽神佛 憧憬這么活 天天都聽歌
    旖旎i閱讀 155評論 0 6
  • 感恩健康讓我和家人快樂的活在世上,不用承受身體的痛苦,謝謝你我愛你健康 感恩我所擁有的金錢,感恩金錢從四面八方涌向...
    e61610af7098閱讀 214評論 0 0

友情鏈接更多精彩內(nèi)容