devops toolsAnsibleansible

Ansible 小手册系列 二十一(管理windows系统)

2017-04-09  本文已影响4002人  lework

windows 系统要求

在Windows 7和Server 2008 R2计算机上,由于Windows Management Framework 3.0中的错误,可能需要安装此修补程序 http://support.microsoft.com/kb/2842230 以避免收到内存和堆栈溢出异常。已知新安装的Server 2008 R2系统与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 可用变量

有什么模块可以用的?

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

专门用作windows系统的模块
http://docs.ansible.com/ansible/list_of_windows_modules.html

获取 Windows Facts

ansible winhost.example.com -m setup

Windows Playbook示例

执行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"

查看文件状态
- 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"

本次实验环境

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 侧配置

  1. 以管理员身份打开powershell


    Paste_Image.png
  2. 查看当前ps版本
    $PSVersionTable

    Paste_Image.png
  3. 系统自带的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.安装完,重启服务器,查看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
上一篇 下一篇

猜你喜欢

热点阅读