前请提示:公司有一批新的windows机器,需要批量管理,于是想到了ansble,ansible是基于Python开发,实现了批量系统配置、批量程序部署、批量运行命令等功能,ansible可用于管理Windows集群,不过管理节点需要部署在Linux机器上,而且需要预装python winrm模块。同时,Windows机器上的powershell版本需要满足3.0+,且Management Framework也需要满足3.0+版本。
测试环境:
ansble主机_ip:192.168.21.241(centos7)
被管理的windows主机1_ip:192.168.21.235(windows 7)
被管理的windows主机2_ip:192.168.21.239(windows 7)
在windows机器上安装powershell
1、查看powershell版本
在运行界面输入powershell,可以看到windows自带的powershell版本为2.0,不符合,需要升级
2、安装.net4.0(powershell是依赖于.net的)
注意:一定要先安装.net
.net下载地址https://www.microsoft.com/en-us/download/details.aspx?id=17851
powershell下载地址https://www.microsoft.com/zh-CN/download/details.aspx?id=40855
安装好都重启电脑,再次查看powershell版本
get-host
配置winrm服务 (默认是关闭的)
winrm quickconfig #配置winrm service并启动服务
winrm set winrm/config/service/auth '@{Basic="true"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
winrm enumerate winrm/config/listener #查看winrm service启动监听状态
注意:我在配置winrm service并启动服务时遇到一个错误,提示我现有网络类型不安全
将公有网络改为工作网络即可
在centos7上安装ansible
yum install -y epel-release
yum install -y ansible
ansible --version
安装pywinrm
pip install pywinrm
报错ERROR: Cannot uninstall 'requests'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
解决办法
pip install --ignore-installed requests
再次安装,成功
编辑hosts文件
vim /etc/ansible/hosts
添加需要管理的主机
[win1]
192.168.21.235 ansible_ssh_user="administrator" ansible_ssh_pass="passwd" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore
[win2]
192.168.21.239 ansible_ssh_user="administrator" ansible_ssh_pass="passwd" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore
测试
ansible all -i /etc/ansible/hosts -m win_ping
返回状态成功
192.168.21.235 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.21.239 | SUCCESS => {
"changed": false,
"ping": "pong"
}
解决windows机器乱码问题
备份winrm模块配置文件
cd /usr/lib/python2.7/site-packages/winrm
cp protocol.py protocol_bak.py
替换配置文件内容
sed -i "s/tdout_buffer.append(stdout)/tdout_buffer.append(stdout.decode('gbk').encode('utf-8'))/g" protocol.py
sed -i "s/stderr_buffer.append(stderr)/stderr_buffer.append(stderr.decode('gbk').encode('utf-8'))/g" protocol.py