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

12
我卡在了 Ansible 的 Windows 模块上。我只是尝试对 Windows 机器进行 ping 测试,但是出现了“连接超时”的错误。 主机
[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 命令对该 Windows 计算机进行 ping 测试。

5个回答

20
你需要为 PowerShell 远程管理准备你的 Windows 机器,否则 ansible 将无法连接到它。大多数功能需要至少安装 PowerShell 3.0(仅支持 Windows 7 SP1 或 Windows Server 2008 SP1 及更高版本),同时运行此脚本将不仅启用 WinRM,还会安装一些必要的证书使连接正常工作。
下载 ConfigureRemotingForAnsible.ps1 文件后,从命令行执行以下命令即可:

powershell.exe -File ConfigureRemotingForAnsible.ps1


1
在某些情况下,使用版本化的链接可能会很有用,例如 https://github.com/ansible/ansible/blob/c99c3b2b5d396b2cc1d25ad9aa6816aa922ffbc7/examples/scripts/ConfigureRemotingForAnsible.ps1 。除此之外,我想知道,Ansible是否会自动运行这个脚本?人们通常使用Ansible(或其他基于WinRM的工具)来调用这个脚本吗? - blong

2
如果您正在使用全新的 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虚拟机中添加以下规则,即可在入站规则中实现。

  • WINRM-HTTPS
  • ICMP4

0

如果您正在使用最新的 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

这不够具体。请在何处以及如何添加这些规则。


0

0
尝试在你的剧本中使用这些。 那对我来说已经解决了。

win_ping.yml

---
- name: Test WinRM connection
  hosts: windows
  gather_facts: no
  tasks:
    - name: Test connection
      win_ping:
      vars:
        ansible_winrm_server_cert_validation: ignore

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接