ansible 管理 windows

2023年 7月 16日 59.3k 0

前言

因为工作需求,有一批windows服务器需要管理,因为数量较多,所以研究了一下ansible自动化管理windows服务器,在此记录一下。

环境说明

ansible 服务器:centos 7.2

windows客户端:windows 7

ansible服务器配置

1、需要Python版本为2.7

2、安装pywinrm模块:ansible 通过windows 客户端启动的winrm端口进行管理,所以服务器端需要支持winrm

pip install "pywinrm>=0.2.2"

3、hosts配置
配置hosts,由于windows不支持ssh协议,所以需要配置明文密码

[root@c7-node1 ansible]# cat /etc/ansible/hosts
[windows]
192.168.202.65
[root@c7-node1 ansible]# cat /etc/ansible/group_vars/windows.yml
ansible_ssh_user: admin
ansible_ssh_pass: 123456
ansible_ssh_port: 5986
ansible_connection: winrm
ansible_winrm_server_cert_validation: ignore

windows 客户端配置

1、安装 Framework 3.0+

2、设置网络为工作网络

3、设置 PowerShell 本地脚本运行权限为 remotesigned(Windows 系统默认不允许非 Adminitor 外的普通用户执行 SP 脚本,即使是管理员,如下开放 SP 脚本执行权限。)

打开powershell执行命令:set-executionpolicy -executionpolicy unrestricted

4、升级powershell到3.0版本(在powershell中执行upgrade_to_ps3.ps1脚本,执行完成需重启系统生效)

脚本获取:https://github.com/ansible/ansible/blob/devel/examples/scripts/upgrade_to_ps3.ps1

5、设置Windows远端管理(在powershell中执行ConfigureRemotingForAnsible.ps1脚本)

脚本获取:https://github.com/ansible/ansible/blob/devel/examples/scripts/ConfigureRemotingForAnsible.ps1

验证测试

配置完成后,在ansible服务器调用win_ping模块,验证能否正常管理windows服务器

[root@c7-node1 ansible]# ansible all -m win_ping
192.168.202.65 | SUCCESS => {
    "changed": false,
    "failed": false,
    "ping": "pong"
}

windows 常用模块介绍

win_copy:拷贝文件到远程windows主机

ansible windows -m win_copy -a ‘src=/tmp dest=C:\hehehe’

 

win_file:创建,删除文件或目录

ansible windows -m win_file -a ‘path=C:\hehehe state=absent’

 

win_command:执行shell命令

列出所有后台任务:ansible windows -m win_command -a “tasklist”

关闭cmd程序:ansible windows -m win_command -a “taskkill /im cmd.exe -f”

更多windows模块支持参考:http://docs.ansible.com/ansible/latest/modules/list_of_windows_modules.html

一些关键点说明

1、windows 服务器配置重启免密码登陆,否则windows服务器重启后,进行远程连接

2、ansible运行windows程序均为后台运行,但是一些程序如果必须前台运行的话,可以使用windows 的任务计划,配置触发条件,运行程序,这样运行的程序就会跑在前台

 

相关文章

对接alertmanager创建钉钉卡片(1)
手把手教你搭建OpenFalcon监控系统
无需任何魔法即可使用 Ansible 的神奇变量“hostvars”
openobseve HA本地单集群模式
基于k8s上loggie/vector/openobserve日志收集
openobseve单节点和查询语法

发布评论