Windows客户端配置

Powershell请在标准镜像中进行预配置,以降低人员成本。

PowerShell 升级至4.0+

Windows机器需要安装或升级powershell4.0以上版本,升级PowerShell步骤:

  1. 检查powershell版本
    $PSVersionTable
  2. 下载并安装 PowerShell 4.0(提取码:c7kl ):
  • Microsoft .NET Framework 4.5
  • powershell4.0( Windows Management Framework 4.0 )

注意 :先安装.NET Framework 4.5 ,而后安装powershell4.0

关于Windows已内置的Powershell版本说明:

Powershell是运行在Windows机器上实现系统和应用程序管理自动化的命令行脚本环境。 需要.NET环境的支持,
同时支持.NET对象。当前PowerShell有5个版本,分别为1.0、2.0、3.0、4.0、5.0
如果系统是Windows 7或者Windows Server 2008 R2,那么PowerShell 2.0已经内置了,可以升级为3.0或4.0版本 
如果系统是Windows Server 2012,那么PowerShell 3.0已经内置了,可以升级为4.0版本
如果系统是Windows 8.1或者Windows server 2012 R2,那么默认已经是4.0版本
如果系统是Windows Server 2016或者Windows 10,那么默认为5.0版本

配置winrm,启用powershell远程管理

1
2
3
4
5
6
7
8
9
10
11
12
13
# 查看powershell版本
get-host
# 查看powershell执行策略
get-executionpolicy
# 更改powershell执行策略为remotesigned
set-executionpolicy remotesigned
# 配置winrm service并启动服务
winrm quickconfig
# 查看winrm service启动监听状态
winrm enumerate winrm/config/listener
# 修改winrm配置,启用远程连接认证
winrm set winrm/config/service/auth '@{Basic="true"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'

Ansible服务端配置

模块安装

安装request2.9版本依赖

1
sudo pip install -I requests==2.9

安装winrm

1
sudo pip install pywinrm

编写剧本测试连接Windows

编辑/etc/ansible/hosts,添加客户端主机信息

1
2
[windows]
172.16.10.23 ansible_ssh_user="Administrator" ansible_ssh_pass="123123" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore

需要的主机变量:

  • ansible_ssh_user:
  • ansible_ssh_password:
  • ansible_ssh_port: 5986
  • ansible_connection: winrm
  • ansible_winrm_server_cert_validation: ignore

测试ping探测windows客户主机是否存活

1
ansible 172.16.10.23 -m win_ping