由于blog各种垃圾评论太多,而且本人审核评论周期较长,所以懒得管理评论了,就把评论功能关闭,有问题可以直接qq骚扰我

ansible 管理 windows

自动化 西门飞冰 11541℃

前言

因为工作需求,有一批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 的任务计划,配置触发条件,运行程序,这样运行的程序就会跑在前台

 

转载请注明:西门飞冰的博客 » ansible 管理 windows

喜欢 (1)or分享 (0)