Ansible 管理windwos服务器(一),ansiblewindwos


不废话了,开始吧

Ansible管理Windwos需要在windows上执行Powershell脚本,并且Powershell脚本还有版本要求有必要条件:
必要条件:
(1) 必须开启以及配置Powershell
https://github.com/ansible/ansible/blob/devel/examples/scripts/ConfigureRemotingForAnsible.ps1
(2)Powershell版本必须3.0 or higer
如果您的系统是window7或者Windows Server 2008,那么PowerShell 2.0已经内置了,可以升级为3.0,4.0
如果您的系统是Windows 8 或者Windows server 2012,那么PowerShell 3.0已经内置了,可以升级为4.0
如果您的系统为Windows 8.1或者Windows server 2012 R2,那默认已经是4.0了。

查看Powershell版本,powershell 界面下输入 $psversiontabl
  • Ansible控制机配置
    在Ansible控制机上执行下面指令安装pywinrm模块 pip install "pywinrm>=0.1.1" 我这里已经安装过,版本是0.2.2

[图片上传失败...(image-93b72f-1534605118005)]

  • 被管windows主机配置

开启以及配置Powershell

Ansible 官方提供初始化脚本

https://github.com/ansible/ansible/blob/devel/examples/scripts/ConfigureRemotingForAnsible.ps1

脚本主要完成如下操作:

  1. 检查最后安装证书的指纹
  2. 配置错误处理
  3. 检测Power shell版本
  4. 检查/启动WimRM服务
  5. 确保WinRM运行之后,检查有PS会话配置
  6. 确保有SSL监听
  7. 检查基本鉴权
  8. 配置防火墙允许WinRM HTTPS链接
  9. 本地测试通过网络方式连接是否正常

注意:如果提示系统中禁止执行脚本,可以在Powershell 命令行界面输入 set-ExecutionPolicy RemoteSigned 然后输入Y,在执行脚本就不会报。

在计算机上运行winrm服务

powershell 3.0中执行

winrm qc

开启后,需要查看配置是否已经开启

3.3 主要是下图Auth中Basic设置为true,service中AllowUnencrypted设置为true

> winrm set winrm/config/service '@{AllowUnencrypted="true"}'
> winrm set winrm/config/service/auth '@{Basic="true"}'

img_1e14023a5511e6f6b4eee1db802503fa.png wKioL1gFldTxKpQEAABQOm0lSnU531.png-wh_50

配置ansible控制机

配置方法有两种:

第一种:

在/etc/ansible/hosts中

[windows]
192.168.1.11
[windows:vars]
ansible_ssh_user="Administrator"
ansible_ssh_pass="123456"
ansible_ssh_port=5986
ansible_connection="winrm"
$ ansible windows -m win_ping
192.168.1.11 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

第二种

在/etc/ansible/hosts中

[windows]
192.168.1.11 ansible_ssh_user="Administrator" ansible_ssh_pass="123456" ansible_ssh_port=5986 ansible_connection="winrm"

要注意的是 端口方面ssl即https方式的使用5986,http使用5985。

区别于控制Linux主机,win主机的命令,需要加上win_,具体支持情况请见官网

http://docs.ansible.com/ansible/list_of_windows_modules.html


****简单测试****

执行cmd命令

重启的第一种方式

$ ansible windows -m win_shell -a "shutdown -r -t 1"

第二种方式,不加参数等同于第一种方式

$ ansible windows -m win_reboot

未完待续!!!!

相关内容