wget http://download.baiyongjie.com/python/pip/pip-8.1.0.tar.gz
wget http://download.baiyongjie.com/python/pip/setuptools-33.1.1.zip
unzip setuptools-33.1.1.zip
cd setuptools-33.1.1
python setup.py install
tar zxvf pip-8.1.0.tar.gz
cd ../pip-8.1.0
python setup.py install
pip install --upgrade pip
pip --version
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple "pywinrm>=0.2.2" --ignore-installed requests
参考文档
配置winrm之前检查系统版本,以及powershell版本,如果是Server2008R2版本,则需要升级powershell至4.0版本。Server2012R2以上的版本不需要升级powershell。
get-host
下载地址:
https://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_setup.exe
下载地址:
https://download.microsoft.com/download/3/D/6/3D61D262-8549-4769-A660-230B67E15B25/Windows6.1-KB2819745-x64-MultiPkg.msu
注意: 先安装.NET Framework 4.5 ,然后安装powershell4.0,安装完成之后重启windows服务器
get-executionpolicy
set-executionpolicy remotesigned
winrm quickconfig
winrm enumerate winrm/config/listener
winrm set winrm/config/service/auth ‘@{Basic=“true”}’
winrm set winrm/config/service ‘@{AllowUnencrypted=“true”}’
vim /etc/ansible/hosts
添加如下连接配置
[t-windows]
192.168.0.33 ansible_ssh_user="administrator" ansible_ssh_pass="123456" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore
目标路径不能含关键词ansible,否则提示无效路径,源使用反斜杠结果将递归传输目录下所有文件,源不一反斜杠结尾将整个目录传输到目标目录下。
远程执行命令分为远程执行windows 原生自有命令通过raw 模块,如:“ipconfig "
远程执行ansible的win_command模块也可以执行命令,即ansible的扩展命令如"whoami”
ansible t-windows -m win_ping
ansible t-windows -m raw -a “ipconfig”
测试在windows主机执行远程创建目录
ansible t-windows -m win_file -a ‘dest=D:\test-mkdir2 state=directory’
ansible t-windows -m raw -a “md D:\ansible\test-mkdir”
测试将ansible主机上的/etc/hosts文件同步到windows主机的指定目录下
ansible t-windows -m win_copy -a ‘src=/etc/hosts dest=D:\t-ansible\hosts.txt’
删除文件
ansible t-windows -m win_file -a ‘dest=D:\t-ansible\wxd.txt state=absent’
移动文件
移动文件目标端也需要制定到文件,而不能只制定到所在目录位置
ansible windows -m raw -a “cmd /c ‘move /y D:\Ansible\product\DBFPlus.exe D:\Ansible\back\DBFPlus.exe’”
删除目录
ansible t-windows -m win_file -a ‘dest=D:\ansible\test-mkdir state=absent’
ansible t-windows -m win_shell -a ‘ipconfig’
ansible t-windows -m raw -a “ipconfig”
结束进程
ansible t-windows -m raw -a “taskkill /F /IM snmp.exe /T”
将.zip解压到远程Windows主机,远程主机上必须存在需要解压的源文件
ansible windows -m win_unzip -a"creates=no src=D:\Tomcat8620\webapps\PayChannels-8630.zip dest=D:\Tomcat8620\webapps"
解压到D盘
ansible windows -m win_unzip -a"creates=no src=D:\SupplierPay.zip dest=D:"
重启node.js(.bat命令)
ansible windows -m win_command -a “chdir=D:\SupplierPay .\http_restart.bat”
ansible t-windows -m win_reboot
ansible t-windows -m win_shell -a ‘shutdown -r -t 0’
ansible t-windows -m win_user -a “name=testuser1 passwd=123456”
Ansible命令格式:
ansible [远程主机IP地址] -m win_shell -a “net stop|start 服务名”
处理办法:
cp /usr/lib/python2.7/site-packages/winrm/protocol.py{,.20190509bak}
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
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