【Absible学习】ansible管理windows系统

Ansible 从1.7+版本开始支持Windows,实测Windows 7 SP1和Windows Server 2008 R2及以上版本系统经简单配置可正常与Ansible通信。但需要满足以下几点:
1、管理机必须是linux系统,且原装Python Winrm模块
2、底层通信基于PowerShell,版本为3.0+,Management Framework版本为3.0+
3、远程windows主机开启Winrm服务

2.更改powershell策略为remotesigned
在命令行中输入 start powershell就可启动powershell
通过Get-ExecutionPolicy查看脚本执行策略;通过Set-ExecutionPolicy UnRestricted更改脚本执行策略

3.升级PowerShell至3.0+
Window 7和Windows Server 2008 R2默认安装的有PowerShell,但版本号一般为2.0版本,所以我们需升级至3.0+,Windows PowerShell 3.0使用的是 .netframework 4.0

下载upgrade_to_ps3.ps1,右击使用powershell运行后重启系统

或者使用Ansible 官方提供初始化脚本,脚本主要完成如下操作:
检查最后安装证书的指纹
配置错误处理
检测Power shell版本
检查/启动WimRM服务
确保WinRM运行之后,检查有PS会话配置
确保有SSL监听
检查基本鉴权
配置防火墙允许WinRM HTTPS链接
本地测试通过网络方式连接是否正常

注意:如果提示系统中禁止执行脚本,可以在Powershell 命令行界面输入 set-ExecutionPolicy RemoteSigned 然后输入Y,在执行脚本就不会报

4.设置Windows远端管理(WS-Management,WinRM)服务
注意以下操作在cmd中执行,而非powershell中
winrm 服务默认都是未启用的状态
winrm quickconfig
查看winrm service listener:winrm e winrm/config/listener
配置auth 为true(默认为false):winrm set winrm/config/service/auth @{Basic="true"}
配置允许非加密方式:winrm set winrm/config/service @{AllowUnencrypted="true"}

至此windows远端管理(WS-Management,WinRM)服务的环境配置完成!

  • 控制主机linux:
    如果没有安装pip, 先安装对应于你的Python版本的pip:

    [[email protected] svn]# easy_install pip    #wget https://bootstrap.pypa.io/get-pip.py;python get-pip.py
    Installed /usr/lib/python2.7/site-packages/pip-10.0.1-py2.7.egg
    Processing dependencies for pip
    Finished processing dependencies for pip
    [[email protected] svn]#
    [[email protected] svn]# pip install paramiko PyYAML Jinja2 httplib2 six  #pip install pywinrm paramiko PyYAML Jinja2 httplib2 six
    [[email protected] 118920]# tail -2 /etc/ansible/hosts
    [windows]
    10.15.97.100 ansible_ssh_user="administrator" ansible_ssh_pass="123123" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore
    [[email protected] ~]# 
  • 连通性
    win_ping:Windows系统下的ping模块,常用来测试主机是否存活。
[[email protected] ~]# ansible 10.15.97.100 -m win_ping
10.15.97.100 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
[[email protected] ~]#

* 远程执行命令

远程执行命令分为远程执行windows 原生自有命令通过raw 模块,如:"ipconfig "
远程执行ansible的win_command模块也可以执行命令,即ansible的扩展命令如"whoami"
默认是乱码,需要修改winrm模块文件

[[email protected] ~]# cp /usr/lib/python2.7/site-packages/winrm/protocol.py{,.20180718bak}
[[email protected] ~]# 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.py
[[email protected] ~]# sed -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
[[email protected] ~]#
  • 获取ip地址
[[email protected] ~]# ansible windows -m raw -a "ipconfig"
10.15.97.100 | SUCCESS | rc=0 >>

Windows IP Configuration

Ethernet adapter 本地连接:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::e9ce:231:8bc6:45ea%11
   IPv4 Address. . . . . . . . . . . : 10.15.97.100
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 10.15.97.254

Tunnel adapter isatap.{BB164424-6017-46EB-978A-5E7CFDF80A14}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : 

[[email protected] ~]# 
  • 获取身份
[[email protected] ~]# ansible windows -m win_command -a "whoami"
10.15.97.100 | SUCCESS | rc=0 >>
wthost\administrator

[[email protected] ~]#
  • 移动文件
[[email protected] ~]# ansible windows -m raw -a "cmd /c ‘move /y D:\Ansible\product\DBFPlus.exe D:\Ansible\back\‘"
ERROR! failed at splitting arguments, either an unbalanced jinja2 block or quotes: cmd /c ‘move /y D:\Ansible\product\DBFPlus.exe D:\Ansible\back\‘
[[email protected] ~]# ansible windows -m raw -a "cmd /c ‘move /y D:\Ansible\product\DBFPlus.exe D:\Ansible\back\DBFPlus.exe‘"
10.15.97.100 | SUCCESS | rc=0 >>
        1 file(s) moved.

[[email protected] ~]# 

移动文件目标端也需要制定到文件,而不能只制定到所在目录位置

[[email protected] ~]# ansible windows -m raw -a "cmd /c ‘move /y D:\Ansible\product\ D:\Ansible\back\‘"
ERROR! failed at splitting arguments, either an unbalanced jinja2 block or quotes: cmd /c ‘move /y D:\Ansible\product\ D:\Ansible\back\‘
[[email protected] ~]# ansible windows -m raw -a "cmd /c ‘move /y D:\Ansible\product\ D:\Ansible\back‘"
10.15.97.100 | FAILED | rc=1 >>
The system cannot find the file specified.
non-zero return code

[[email protected] ~]# ansible windows -m raw -a "cmd /c ‘move /y D:\Ansible\product D:\Ansible\back\‘"
ERROR! failed at splitting arguments, either an unbalanced jinja2 block or quotes: cmd /c ‘move /y D:\Ansible\product D:\Ansible\back\‘
[[email protected] ~]# ansible windows -m raw -a "cmd /c ‘move /y D:\Ansible\product D:\Ansible\back‘"
10.15.97.100 | SUCCESS | rc=0 >>
        1 dir(s) moved.

[[email protected] ~]#

移动文件夹源端和目标端目录都不能带反斜杠/。且将源的整个目录移到目的端目录里。

  • 创建文件夹
[[email protected] ~]# ansible windows -m raw -a "md d:\Ansible\justin"
10.15.97.100 | SUCCESS | rc=0 >>

    Directory: D:\Ansible

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         2018/7/18     20:13            justin                            

[[email protected] ~]# 
  • 删除文件或目录
[[email protected] ~]# ansible windows -m win_file -a "path=d:\Ansible\justin state=absent"
10.15.97.100 | SUCCESS => {
    "changed": true
}
[[email protected] ~]#
  • 结束某程序
[[email protected] ~]# ansible windows -m raw -a "taskkill /F /IM snmp.exe /T"
10.15.97.100 | SUCCESS | rc=0 >>
SUCCESS: The process with PID 1412 (child process of PID 548) has been terminated.

[[email protected] ~]#
  • 文件传输
[[email protected] ~]# ansible windows -m win_copy -a ‘src=/app/svn/127_Client/118919/zjcfg.zip dest=D:\soft\‘
10.15.97.100 | SUCCESS => {
    "changed": true,
    "checksum": "d797ae640e37a1de6bb02b1e7fb435d7919effec",
    "dest": "‘D:\\soft\\zjcfg.zip‘",
    "operation": "file_copy",
    "original_basename": "zjcfg.zip",
    "size": 131374,
    "src": "/app/svn/127_Client/118919/zjcfg.zip"
}
[[email protected] ~]# ansible windows -m win_copy -a ‘src=/app/svn/127_Client/118919/zjcfg.zip dest=D:\ansible\‘
10.15.97.100 | FAILED! => {
    "changed": false,
    "checksum": "d797ae640e37a1de6bb02b1e7fb435d7919effec",
    "dest": "‘D:\u0007nsible\\zjcfg.zip‘",
    "msg": "Get-AnsibleParam: Parameter ‘dest‘ has an invalid path ‘D:\u0007nsible\\‘ specified.",
    "operation": "file_copy",
    "original_basename": "zjcfg.zip",
    "size": 131374,
    "src": "/app/svn/127_Client/118919/zjcfg.zip"
}
[[email protected] ~]# ansible windows -m win_copy -a ‘src=/app/svn/127_Client/118919/zjcfg.zip dest=D:\‘
10.15.97.100 | SUCCESS => {
    "changed": true,
    "checksum": "d797ae640e37a1de6bb02b1e7fb435d7919effec",
    "dest": "‘D:\\zjcfg.zip‘",
    "operation": "file_copy",
    "original_basename": "zjcfg.zip",
    "size": 131374,
    "src": "/app/svn/127_Client/118919/zjcfg.zip"
}
[[email protected] ~]# ansible windows -m win_copy -a ‘src=/app/svn/127_Client/118919/ dest=D:\‘
10.15.97.100 | SUCCESS => {
    "changed": true,
    "dest": "D:\\",
    "operation": "folder_copy",
    "src": "/app/svn/127_Client/118919/"
}
[[email protected] ~]# 

目标路径不能含关键词ansible,否则提示无效路径,源使用反斜杠结果将递归传输目录下所有文件,源不一反斜杠结尾将整个目录传输到目标目录下。

  • 创建用户
[[email protected] ~]# ansible windows -m win_user -a "name=justin passwd=51cto groups=Administrators"
10.15.97.100 | SUCCESS => {
    "account_disabled": false,
    "account_locked": false,
    "changed": true,
    "description": "",
    "fullname": "justin",
    "groups": [
        {
            "name": "Administrators",
            "path": "WinNT://WORKGROUP/WTHOST/Administrators"
        }
    ],
    "name": "justin",
    "password_expired": true,
    "password_never_expires": false,
    "path": "WinNT://WORKGROUP/WTHOST/justin",
    "sid": "S-1-5-21-4260034264-4268704002-684640490-1001",
    "state": "present",
    "user_cannot_change_password": false
}
[[email protected] ~]# 
  • 执行windows下的bat
[[email protected] ~]# ansible windows -m win_command -a "chdir=D:\ .\xcopy.bat"
10.15.97.100 | SUCCESS | rc=0 >>

D:\>md d:\justin 

[[email protected] ~]#

先切换到bat所在目录,再执行bat

更多官方windows模块见:官网

原文地址:http://blog.51cto.com/ityunwei2017/2146957

时间: 2024-08-20 06:14:27

【Absible学习】ansible管理windows系统的相关文章

ansible自动化管理windows系统实战

一.简述 1.说明日常系统自动化运维过程中难免会有windows系列服务器,就开源软件来说目前大多的对windows批量管理兼容性不太好;不像Linux系统便捷,但现实中确实有些业务需要跑在windows上;搜索查找折腾一番后,发现python开发的ansible(已经被redhat收购)有比较好的解决方案,通过一番折腾,整理出来,以备忘交流; 2.实验环境服务器端:CentOS7.4_x64 自带python 2.7.5 ip:172.16.3.167源码安装ansible 被管理window

Ansible管理windows

Ansible管理windows 安装步骤 一.系统要求1,管理机必须是Linux系统且需预安装Python Winrm 模块(本次使用Linux版本是CentOS Linux release 7.3.1611 (Core)).2,底层通信认证一些基于Kerberos ,Windows使用的连接工具为PowerShell而非SSH,我这里测试的Windows版本是win7-32bit专业版.3,远程主机PowerShell版本为3.0+,Management Framework版本为3.0+.4

[转帖]Ansible管理windows集群

http://www.cnblogs.com/Dev0ps/p/10026908.html 写的挺好的 我关注点还是不够好呢 最近公司新项目需要安装400+windows server 2012系统的工作站,想着怎么能像linux下运用ansible批量管理,linux就很简单了有ssh服务 但是下却没这么简单,但还是有办法那就是Powershell. Ansible可用于管理Windows集群,不过管理节点需要部署在Linux机器上,而且需要预装python winrm模块. 同时,Windo

Sass学习笔记 -- 在Windows系统中安装Sass和Compass

Sass和Compass都是基于Ruby编程语言的命令行工具.要使用它们,你首先需要在电脑中安装Ruby,Windows系统并没有预置Ruby,因此如果你之前没有安装过Ruby,现在就需要进行安装.安 装Ruby只需要花费几分钟的时间. 直接百度搜索"ruby"或者点击http://rubyinstaller.org/downloads/ 下载,根据自己系统配置,如果是x64,则选择" Ruby 2.3.1 (x64)" 按照提示,点击下一步,注意这里需要全部勾选

Windows系统Unity3D中的快捷键

[狗刨学习网] Windows系统Unity3D中的快捷键[td] 组合键 键 功能 File 文件 Ctrl   N New Scene 新建场景 Ctrl   O Open Scene 打开场景 Ctrl   S Save Scene 保存 Ctrl Shift S Save Scene as 保存场景为 Ctrl Shift B Build Settings... 编译设置... Ctrl   B Build and run 编译并运行 Edit 编辑 Ctrl   Z Undo 撤消 C

Ansible关于windows的管理

Ansible 是基于python的常用自动化运维工具.之所以选ansible是因为它简单,不需要客户端,最关键的地方是其他的我没用过.Ansible 的管理主机必须要安装在linux环境下的,这个是官方的要求.系统是centos 7.4 ,python就不说了,系统默认必备的.Ansible 对于linux的管理我就不说了,网上大把的文章,我们来研究一下ansible对Windows方面管理. 环境搭建Ansible 安装方法:yum install ansible 对,你没看错,就这么简单,

[转帖]Ansible批量远程管理Windows主机(部署与配置)

2018-09-12 12:04:42 https://blog.51cto.com/7424593/2174156 一.测试环境介绍 Ansible管理主机: 系统:   CentOS6.8 IP Addr: 172.16.10.22 Linux管理服务器需安装pip.pywinrm插件 Windows客户端主机: 系统:   Server2008R2 SP1 IP Addr: 172.16.10.23 Windows机器需要安装或升级powershell4.0以上版本,Server2008R

MongoDB学习(1)—在Windows系统中安装MongoDB

概述 本文主要介绍在Windows系统安装MongoDB的方法. MongoDB官方网址:http://www.mongodb.org/,最新版本为2.6.7. 注意: 从2.2版本开始,MongoDB并不支持Windows XP.请使用最新版本的windows来使用最新版本的MongoDB. 如果您正在运行任何版本的Windows Server 2008 R2或Windows 7,请安装热修复补丁来解决一个在Windows上使用内存映射文件问题. 补丁下载地址:官方 360云盘(访问密码:5e

windows系统背景知识学习笔记

前言 对于每天都要在windows平台上进行逆向工程任务的我们而言,稍微了解一些系统底层的机制与实现原理,用这些背景知识来武装自己总是好的. 调试器,反汇编器,加包,解包器,虚拟机,等等,无一不是运行在windows系统上, 甚至对内核调试器如SoftICE,WinDbg等工具的使用,也要求了解一些系统的内幕. 最近抽空在阅读<深入解析windows操作系统第6版上册>这本书,因此将其中与软件逆向有关的重点总结出来,部分内容加上了个人的理解进行润色,并非断章取义,而是去芜存菁. 笔记会不定期更