如果要使用密钥,需要先创建秘钥对,然后在再主机管理页面进行选择。
Linux系统
测试密码或者密钥正确后,进行保存。
Windows系统
Windows需要支持winrm协议,可以参考Ansible官网文档进行设置:
https://docs.ansible.com/ansible/latest/user_guide/windows_setup.html
关闭UAC
控制面板/用户账号控制设置
设置winrm
以管理员身份运行powershell
设置执行策略
winrm invoke Restore winrm/Config
set-executionpolicy remotesigned
winrm quickconfig
winrm set winrm/config/service/auth '@{Basic="true"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
安装winrm内存修补程序
在powershell上执行下面的命令,如果不能自动下载补丁安装,请手动下载安装。
补丁官网:https://github.com/jborean93/ansible-windows/tree/master/scripts
$url = "https://raw.githubusercontent.com/jborean93/ansible-windows/master/scripts/Install-WMF3Hotfix.ps1"
$file = "$env:SystemDrive\temp\Install-WMF3Hotfix.ps1"
(New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)
powershell.exe -ExecutionPolicy ByPass -File $file -Verbose
关闭防火墙、添加防火墙信任规则、杀毒软件添加白名单
允许5985,5986端口通过,端口-TCP特定端口,起名ansbile。
如果是公有云服务器,安全组里添加容许5985/5986这2个tcp端口。
360等杀毒软件添加白名单
测试winrm,在work主机运行
创建ansible资产文件win,例子:
172.31.173.23(windows主机IP)-c winrm ansible_ssh_port=5985 ansible_winrm_server_cert_validation=ignore
进行测试
ansible -i win all -m raw -a "dir" -u Administrator -k
常见错误
问题一、PowerShell启用winrm失败:拒绝访问 0x80070005 -2147024891
结检查步骤如下:
1、确定 Windows Remote Management (WS-Management) 服务已经启动。
2、确定网络访问使用经典模式,设置为:
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa]
“forceguest”=dword:00000000
3、确定当前administrators组中的账户都设置了密码,而不是空密码,空密码就会报拒绝访问的错。
使用空密码只容许控制台登录登录,设置为:
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa]
“LimitBlankPasswordUse”=dword:00000001
4、重启服务器。
问题二、winrm or requests is not installed: No module named winrm
work和proxy没有安装python的winrm模块,安装
pip install pywinrm
二、Cisco IOS系统测试失败,可以手动测试,如:
手动测试,创建ansible资产文件cisco,例子:
192.168.50.40 ansible_connection=local ansible_ssh_port=22
进行测试
ansible -i cisco all -m raw -a "echo test"
ansible -i cisco all -m ios_command -a "commands='echo test'" -u admin -k
三、华为ce系统测试失败,手动测试,如:
创建ansible资产文件huawei,例子:
192.168.50.40 ansible_connection=local ansible_ssh_port=22
进行测试
ansible -i huawei all -m raw -a "echo test"
ansible -i huawei all -m ce_command -a "commands='echo test'" -u admin -k