Ansible管理windows系统

来源:互联网 发布:短域名生成算法 编辑:程序博客网 时间:2024/04/30 06:33

一、实验环境

  • windows os: Windows 7 Service Pack 1
  • ansible manager: centos 7
  • ansible version: 2.3.1.0
  • python version: 2.7.5

二、配置ansible manager

  1. 如没有安装pip则需先安装pip
    [root@localhost ~]# easy_install pip
  2. 使用pip安装pywinrm及kerberos
    pip install http://github.com/diyan/pywinrm/archive/master.zip#egg=pywinrm
    pip install kerberos

    在安装kerberos之前需要安装
    yum install libkrb5-dev
    否则会报错

三、配置windows主机

  1. 以管理员身份打开powershell, 并查看当前ps版本
    这里写图片描述
  2. 系统自带的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(这里注意看一下System Requirements里面的Supported Operating System,即Windows 7 Service Pack 1, Windows Server 2008 R2 SP1, Windows Server 2008 Service Pack 2)
    c.安装完,重启服务器,查看powershell版本
    这里写图片描述
  3. 配置winrm

    mkdir C:\workcd C:\workInvoke-WebRequest -Uri https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1 -OutFile ConfigureRemotingForAnsible.ps1powershell -ExecutionPolicy RemoteSigned .\ConfigureRemotingForAnsible.ps1 -SkipNetworkProfileCheck

四、功能测试

  1. 配置ansible控制机

    [root@localhost ~]# vi /etc/ansible/hosts[windows]192.168.67.139[windows:vars]ansible_user=Administratoransible_password=123456ansible_port=5985ansible_connection=winrmansible_winrm_server_cert_validation=ignore  

    要注意的是 端口方面ssl即https方式的使用5986,http使用5985。

  2. 测试通信
    ansible windows -m win_ping
    这里写图片描述
  3. 查看ip地址
    ansible windows -m win_command -a "ipconfig"
    这里写图片描述
    修改上面的中文乱码问题
    对命令输出的信息进行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.7/site-packages/winrm/protocol.pysed -i "s#stderr_buffer.append(stderr)#stderr_buffer.append(stderr.decode('gbk').encode('utf-8'))#g" /usr/lib/python2.7/site-packages/winrm/protocol.py

    修改完之后,重新运行命令,中文已正常显示。
    这里写图片描述

    区别于控制Linux主机,win主机的命令,需要加上win_,具体支持情况请见官网
    http://docs.ansible.com/ansible/list_of_windows_modules.html

原创粉丝点击