ansible批量管理windows服务器

                           Windows下Ansible工作模式(亲测)

Ansible 从1.7+版本开始支持Windows,但前提是管理机必须为Linux系统,远程主机的通信方式也由SSH变更为PowerShell,同时管理机必须预安装Python的Winrm模块,方可和远程Windows主机正常通信,但PowerShell需4.0+版本且Management Framework 4.0+版本,实测Windows Server 2008 R2上版本系统经简单配置可正常与Ansible通信。简单总结如下:
(1)    管理机必须为Linux系统且需预安装Python Winrm模块
(2)    底层通信基于PowerShell,版本为4.0+,Management Framework版本为4.0+
(3)    远程主机开启Winrm服务
Ansible管理机部署安装
(1). 对管理主机的要求
    目前,只要机器上安装了 Python 2.6 或 Python 2.7 (windows系统不可以做控制主机),都可以运行Ansible.
主机的系统是 CentOS7.4版本       
安装ansible:
Yum -y install ansible
如果没有安装pip, 请先安装对应于你的Python版本的pip:
easy_install pip
以下的Python模块也需要安装:
pip install PyYAML
配置hosts文件:
vim /etc/ansible/hosts
[windows]
10.160.94.153 ansible_ssh_user="Administrator" ansible_ssh_pass="123456" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore
10.160.94.153是windows服务器的IP。
/etc/ansible/hosts 中看可添加多个windows服务器的信息 ,可集体一次性管理,分发任务。
至此,ansible服务端配置完毕。

Windows系统配置

首先必须要通过update把系统更新漏洞补丁到最新版本。

和Linux发版版稍有区别,远程主机系统如为Windows需预先如下配置:
安装Framework 4.0+
更改powershell策略为remotesigned
升级PowerShell至4.0+
设置Windows远端管理,英文全称WS-Management(WinRM)
(1)安装Framework 4.5
下载链接为:https://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_setup.exe
下载至本地后双击左键安装即可,期间可能会多次重启,电脑需正常连接Internet。

(2)更改powershell策略为remotesigned
    set-executionpolicy remotesigned
ansible批量管理windows服务器

(3)升级PowerShell至4.0+
Window 7和Windows Server 2008 R2默认安装的有PowerShell,但版本号一般为2.0版本,所以我们需升级至4.0+,如下图中数字1部分表示PowerShell版本过低需4.0+版本,数字2部分表示当前PowerShell版本为2.0。
ansible批量管理windows服务器

安装Windows PowerShell 4.0

安装Windows Management Framework 4.0的6.1内核版本安装文件:
https://download.microsoft.com/download/3/D/6/3D61D262-8549-4769-A660-230B67E15B25/Windows6.1-KB2819745-x64-MultiPkg.msu    
PowerShell 4.0 完全安装之后,仍需要重新启动计算机。
注意: 先安装.NET Framework 4.5 ,然后安装powershell4.0,安装完成之后重启windows服务器

 在PowerShell执行Get-Host命令结果如下图所示PowerShell版本为4.0为正常。 
 ansible批量管理windows服务器
(4)设置Windows远端管理(WS-Management,WinRM)
winrm service 默认都是未启用的状态,先查看状态;如无返回信息,则是没有启动;
winrm enumerate winrm/config/listener
 
针对winrm service 进行基础配置:
winrm quickconfig
输入 ‘y’回车
 
查看winrm service listener:
winrm e winrm/config/listener
ansible批量管理windows服务器
 
为winrm service 配置auth:
winrm set winrm/config/service/auth @{Basic="true"}
ansible批量管理windows服务器
执行上面设置时,会报错,解决办法,将上面的设置命令复制到CMD中执行即可解决。
ansible批量管理windows服务器

为winrm service 配置加密方式为允许非加密:
winrm set winrm/config/service @{AllowUnencrypted="true"}
ansible批量管理windows服务器
执行上面设置时,会报错,解决办法,将上面的设置命令复制到CMD中执行即可解决。
ansible批量管理windows服务器
在windows服务器的防火墙中开启连接的端口:5895
好了,远程Windows主机配置到此结束,我们验证配置的是否有问题。(我重启了一下服务器)
 

Windows下可用模块测试
(1)win_ping —Windows系统下的ping模块,常用来测试主机是否存活
ansible windows -m win_ping
ansible批量管理windows服务器
 连接成功。

(2)win_copy—拷贝文件到远程Windows主机
传输/etc/passwd文件至远程F:\file\目录下
执行命令:
ansible windows -m win_copy -a 'src=/etc/passwd dest=D:\passwd'
 
返回结果:
  ansible批量管理windows服务器
部分返回结果诠释:
“operation”: “file_copy”—执行的操作为 file_copy;
“original_basename”: “passwd”—文件名为 passwd;
“size”: 1368—文件大小为 1368 bytes。

Playbook写法如下:
1
2
3
4
5
6 ---

  • name: windows module example
      hosts: windows
      tasks:
         - name: Move file on remote Windows Server from one location to another
           win_file: src=/etc/passwd dest=D:\passwd
     

(3)win_file —创建,删除文件或目录

删除F:\file\passwd
执行命令:
ansible windows -m win_file -a "path=D:\passwd state=absent"

获取ip地址
ansible windows -m raw -a "ipconfig"

 

获取window主机信息:
ansible windows -m setup

创建文件夹:
ansible windows -m raw -a 'md D:\mulu'

移动文件:
ansible windows -m raw -a "cmd /c 'move /y D:\2\3\WinRAR_5.60_Beta5.exe D:\WinRAR_5.60_Beta5.exe'"
  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值