通过ansible 批量操作windows机器,部署windows_exporter-0.21.0-amd64.exe
1、需要检查的点:
1)、ansible登录用户名必须与系统组记录的成员名一致,要不然会一直报错,"ntlm: the specified credentials were rejected by the server"
2)、检查网络状态是否是专有网络
2、在被控windows电脑中,通过管理员方式打开powershell,执行如下
winrm quickconfig winrm set winrm/config/service/auth '@{Basic="true"}' winrm set winrm/config/service '@{AllowUnencrypted="true"}'
3、控制端在linux机器上,安装如下
pip install pywinrm>=0.2.2
4、ansible host可以如下
[wind] my_server ansible_host=192.168.61.153 ansible_ssh_user="aaa\bbb" ansible_ssh_pass="xxx" [wind:vars] ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore ansible_winrm_transport=ntlm
5、测试一下
[root@www ~]# ansible -i /etc/ansible/myhost wind -m win_ping my_server | SUCCESS => { "changed": false, "ping": "pong" }
6、开始批量操作
--- - hosts: wind gather_facts: no tasks: - name: create directory win_file: path: D:\ops_control state: directory - name: copy pkg win_copy: src: "{{item}}" dest: D:\ops_control\ force: yes with_items: - /etc/ansible/pkg/windows_exporter-0.21.0-amd64.exe - /etc/ansible/pkg/wind_exporter.exe - /etc/ansible/config/config.yml - /etc/ansible/pkg/start_win_exporter.bat #创建目录 ansible -i /etc/ansible/myhost wind -m win_file -a "path=D:\ops_control state=directory" 复制 ansible -i /etc/ansible/myhost wind -m win_copy -a "src=/etc/ansible/pkg/windows_exporter-0.21.0-amd64.exe dest=D:\ops_control\ force=yes" ansible -i /etc/ansible/myhost wind -m win_copy -a "src=/etc/ansible/config/config.yml dest=D:\ops_control\ force=yes" ansible -i /etc/ansible/myhost wind -m win_copy -a "src=/etc/ansible/pkg/start_win_exporter.bat dest=D:\ops_control\ force=yes" 执行 ansible -i /etc/ansible/myhost wind -m win_command -a "chdir=D:\ops_control .\start_win_exporter.bat" 重启 ansible -i /etc/ansible/myhost wind -m raw -a "taskkill /F /IM windows_exporter-0.21.0-amd64.exe /T"
启动操作,着实不太友好,如果哪位大佬有比较好的通过ansible 执行bat在后台启动的,可以赐教一下。
7、在windows的计划任务里面部署 wind_exporter.exe,健康检查与启动。
这个exe文件可以参考
golang win10 app存活状态简单监控
8、ansible部分参考
https://cloud.tencent.com/developer/article/1788393
标签:exporter,windows,win,etc,prometheus,ansible,监控,wind From: https://www.cnblogs.com/bill2014/p/17195775.html