在 Ansible 中,如何连接到 windows 主机?

In Ansible, How to connect to windows host?

我一直坚持使用 Ansible window 模块。我只是想 ping windows machine.But 我得到 'connect timeout'

主机

[windows]
192.168.1.13

group_vars/windows.yaml

ansible_user: raja
ansible_password: myPassword
ansible_port: 5986
ansible_connection: winrm
ansible_winrm_server_cert_validation: ignore

而我 运行 : ansible windows -vvv -i hosts -m win_ping

Using /etc/ansible/ansible.cfg as config file
<192.168.1.13> ESTABLISH WINRM CONNECTION FOR USER: raja on PORT 5986 TO 192.168.1.13
192.168.1.13 | UNREACHABLE! => {
"changed": false,
"msg": "ssl: HTTPSConnectionPool(host='192.168.1.13', port=5986): Max retries exceeded with url: /wsman (Caused by ConnectTimeoutError(<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7fcb12024a90>, 'Connection to 192.168.1.13 timed out. (connect timeout=30)'))",
"unreachable": true
}

但是我可以使用 ping 192.168.1.13

ping windows 机器

您需要为 PowerShell 远程管理准备您的 windows 机器,否则 ansible 将无法连接到它。要使大多数功能正常工作,您至少需要 PowerShell 3.0 installed (which is only supported on Windows 7 SP1 or Windows Server 2008 SP1 and later versions), and also run this script,这不仅会启用 WinRM,还会安装一些必要的证书以使连接正常工作。

下载 ConfigureRemotingForAnsible.ps1 文件后,命令行中的以下命令应该可以完成工作:

powershell.exe -File ConfigureRemotingForAnsible.ps1

如果您使用新的 azure vm 和 aws ec2 并面临问题

"changed": false,
    "msg": "ssl: HTTPSConnectionPool(host='IP Address', port=5986): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f9650462390>: Failed to establish a new connection: [Errno 111] Connection refused',))",
    "unreachable": true" "

解法: 只需在入站规则的 windows VM 中添加以下规则。

  • WINRM-HTTPS
  • ICMP4