经调研和测试,服务端可通过ansible控制各linux服务器(容器),进行各类操作,且支持远程控制windows服务器,但windows操作系统中,需安装.net及powershell3.0及以上版本。
高频应用为win2003 暂无法安装powershell 3.0及以上版本,无法通过ansible远程控制。
win2008及以上操作系统可以。
经测试可通过使用python 利用winrm远程控制win2003。
因此结合自动化运维工具ansible(也是基于python)和python脚本,应可实现测试环境所有虚拟机的远程控制,请各位探索学习和实践,组内发布实践成果。
0、ubuntu安装ansible
可使用任一台docker-pc服务器进行试验(pc2及之后),linux客户端无需任何操作。
或使用任意linux虚拟机,安装ansible工具即可。
ansible安装参考http://www.ansible.com.cn/docs/intro_installation.html
ubuntu安装:
Ubuntu 编译版可在PPA中获得: ` <https://launchpad.net/~ansible/+archive/ansible>`_.
配置PPA及安装ansible,执行如下命令:
$ sudo apt-get install software-properties-common $ sudo apt-add-repository ppa:ansible/ansible $ sudo apt-get update $ sudo apt-get install ansible
1、win2003需进行的操作为(使用python控制或ansible控制均需)
安装Microsoft_.NET_Framework_3.5_Service_Pack_1简体中文安装版.exe
安装powershell2.0 WindowsServer2003-KB968930-x86-ENG.exe (需.net>2.1 当前2.0)
powershell执行(win2008及之上仅需在powershell中执行)
set-executionpolicy remotesigned
get-host
winrm enumerate winrm/config/listener
winrm quickconfig
winrm e winrm/config/listener
winrm set winrm/config/service/auth '@{Basic="true"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
netstat -ano | findstr "5985"
2、调时间相关指令:
具体步骤
net stop w32time
w32tm /unregister
w32tm /register
net start w32time
w32tm /config /manualpeerlist:172.26.41.241 /syncfromflags:manual /update
w32tm /resync
3、控制端python脚本
安装包
使用Python的pip安装pywinrm及kerberos(可暂不安装)
apt-get install python-pip
pip install pywinrm==0.1.1
远程控制win2003同步时间
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Author : "David J"
import winrm
import sys
import argparse
import httplib2
host="172.26.41.36"
ntpserver="172.26.41.241"
username="administrator"
passwd="..."
s = winrm.Session('http://'+host+':5985/wsman',auth=('administrator','UatTester'))
def _runCommand(comm):
if(comm == "q"):
sys.exit()
r = s.run_cmd(comm)
print r.std_out
while 1:
cmd1='w32tm /register'
cmd2='net start w32time'
cmd3='w32tm /config /manualpeerlist:'+ntpserver+' /syncfromflags:manual /update'
cmd4='w32tm /resync'
cmd5="date /t && time /t"
_runCommand(cmd1)
_runCommand(cmd2)
_runCommand(cmd3)
_runCommand(cmd4)
_runCommand(cmd5)
# _runCommand(date \t')
_runCommand("q")