测试环境介绍
Ansible 管理主机
[ CentOS 7.6 ]
[ 10.0.200.70 ]
Linux 管理服务器 需安装 pywinrm 插件
下载插件
网址: https://pypi.org/
pywinrm-0.4.2
ntlm-auth-1.1.0.tar.gz
requests_ntlm-1.1.0.tar.gz
xmltodict-0.12.0-py2.py3-none-any.whl
Windows 机器 需安装 或 升级 PowerShell 4.0 以上版本
注:升级 PowerShell 需要重新服务器才能生效
升级 PowerShell
检查 PowerShell 版本
get-host
版本高于 4.0 无需升级
如 低于 4.0
下载并安装 Microsoft .NET Framework 4.5
地址:https://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_setup.exe
下载并安装 PowerShell 4.0
地址:https://www.filehorse.com/download-windows-powershell-64/37429/
注:先安装 NET Framework 4.5 然后安装 PowerShell 4.0 安装完成之后重启 Windows
检查
get-host
Windows 客户端配置
配置 Winrm 启用 PowerShell 远程管理
打开 PowerShell 终端
按 以下步骤 执行命令
查看 PowerShell 执行策略
get-executionpolicy
更改 PowerShell 策略为 remotesigned
set-executionpolicy remotesigned
配置 Winrm service 并启动服务
winrm quickconfig
启动监听状态
winrm enumerate winrm/config/listener
修改 Winrm 配置 启动远程连接认证
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
winrm set winrm/config/service/auth '@{Basic="true"}'
查看 winrm 配置信息
winrm get winrm/config
添加防火墙信任规则
允许 5985 端口通过
防火墙高级设置 >> 入站规则 >> 新建规则
信任端口 5985
填写 新建 规则名称
Ansible 服务器配置
添加 Windows 客户端信息
vim ~/ansible/inventory
[windows]
10.0.111.41 ansible_ssh_user="Administrator" ansible_ssh_pass="P@ssw0rd" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore
ansible_winrm_transport=ntlm
测试 客户端 是否 存活
ansible 10.0.111.41 -m win_ping
测试 文件管理
测试在 Windows 主机 远程 创建目录 [ win_file ]
ansible windows -m win_file -a 'dest=c:\config_dir state=directory'
测试 将 Ansible 主机上的 /etc/hosts 文件同步到 Windows主机 的 指定目录 下
ansible windows -m win_copy -a 'src=/etc/hosts dest=c:\config_dir\hosts.txt'
删除 文件
ansible windows -m win_file -a 'dest=c:\config_dir\hosts.txt state=absent'
删除 目录
ansible windows -m win_file -a 'dest=c:\config_dir2 state=absent'
测试 CMD 命令
ansible windows -m win_shell -a 'ipconfig'
远程重启 Windows 客户端
ansible windows -m win_reboot
ansible windows -m win_shell -a 'shutdown -r -t 0'
创建用户
ansible windows -m win_user -a "name=testuser1 passwd=123456"
服务管理
ansible windows -m win_shell -a 'net stop|start 服务名'