在centos7上面配置ansible管理Windows服务器
1.配置管理端centos
#yum install ansible -y #wget https://bootstrap.pypa.io/get-pip.py #python get-pip.py #pip install pywinrm
2.修改/etc/ansible/hosts 添加被管理主机
[root@ansible ansible]# cat hosts [all:vars] ansible_ssh_user=administrator ansible_ssh_pass=123456 ansible_ssh_port=5985 ansible_connection=winrm ansible_winrm_server_cert_validation=ignore ansible_winrm_transport=ntlm [windows_test] 62.18.8.206
4.客户端
windows(以管理员身份运行powershell) > winrm enumerate winrm/config/listener > winrm quickconfig > winrm set winrm/config/service/auth '@{Basic="true"}' > winrm set winrm/config/service '@{AllowUnencrypted="true"}'
5.此时便可以用ansible管理Windows服务器
[root@ansible ansible]# ansible windows_test -m win_ping 62.18.8.206 | SUCCESS => { "changed": false, "ping": "pong" }
6.ansible管理windows基本操作
(1)ping模块
#ansible windows -m win_ping
(2)指定配置文件获取win网卡信息
#ansible -i win_hosts windows_test -m raw -a "ipconfig"
(3)使用默认的配置文件获取网卡信息
#ansible windows_test -m raw -a "ipconfig"
(4)解决获取网卡乱码问题
#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
(5)拷贝文件到远程Windows主机
#ansible windows_test -m win_copy -a 'src=/windows/run.ps1 dest=C:\Users\Administrator\Desktop\' #ansible windows_test -m win_copy -a 'src=/etc/passwd dest=F:\file\passwd' #ansible windows_test -m win_copy -a "src=/usr/local/src/PayChannels20.35.zip dest=D:\Tomcat8630\webapps\PayChannels20.35.zip" #ansible windows_test -m win_copy -a "src=/usr/local/src/SupplierPay.zip dest=D:\SupplierPay.zip"
(6)将.zip解压到远程Windows主机,远程主机上必须存在需要解压的源文件
#ansible windows_test -m win_unzip -a"creates=no src=D:\Tomcat8620\webapps\PayChannels-8630.zip dest=D:\Tomcat8620\webapps"
(7)解压到D盘:
#ansible windows -m win_unzip -a"creates=no src=D:\SupplierPay.zip dest=D:"
(8)重启远程windows主机的服务
#ansible windows_test -m win_service -a "name=Tomcat8630 state=restarted"
(9)重启node.js(.bat命令)
#ansible windows_test -m win_command -a "chdir=D:\SupplierPay .\http_restart.bat"
(10)执行win_command模块命令
启动redis #ansible windows_test -m win_command -a "chdir=D:\Redis server-start.bat"
(11)远程执行bat脚本
#ansible windows_test -m win_command -a "chdir=C:\Users\Administrator\Desktop test.bat"
(11)删除文件或者目录
#ansible windows_test -m win_file -a "dest=D:\Tomcat8630\log\ state=absent"
#ansible windows_test -m win_file -a "dest=D:\Tomcat8630\logs\ state=absent"
(12)创建用户
#ansible windows_test -m win_user -a "name=aa passwd=123456"
(13)创建一个名叫user1的管理员用户,要求能够远程访问
#ansible windows_test -m win_user -a "name=user1 password=123 groups='Administrators,Remote Desktop Users'"
(14)重启的第一种方式
#ansible windows_test -m win_shell -a "shutdown -r -t 1"
(15)重启的第二种方式
#ansible windows_test -m win_reboot
(16)关闭nginx进程
#ansible windows_test -m raw -a "taskkill /F /IM nginx.exe /T"
(17)运行桌面上的run.ps1
#ansible windows_test -m win_command -a "PowerShell.exe -file C:\Users\Administrator\Desktop\run.ps1"