Ansible WinRM:通过“[Errno 111] 连接被拒绝”来解决

use*_*618 3 windows winrm ansible

当我尝试通过 WinRM 模块连接到我的 Windows 主机时,我在 Ansible 中收到“连接被拒绝”的错误消息。

\n\n

ansible目录结构如下:

\n\n

命令:

\n\n
user@ansible:~/git/ansible-test$ ansible-playbook test.yml -i inventories/hosts \n\nPLAY [install vm] ***************************************************************************************************************************************************************************\n\nTASK [Gathering Facts] ***********************************************************************************************************************************************************************\nfatal: [10.10.10.10]: UNREACHABLE! => {"changed": false, "msg": "ssl: HTTPSConnectionPool(host=\'10.10.10.10\', port=5986): Max retries exceeded with url: /wsman (Caused by NewConnectionError(\'<urllib3.connection.VerifiedHTTPSConnection object at 0x7f6298184d50>: Failed to establish a new connection: [Errno 111] Connection refused\',))", "unreachable": true}\n        to retry, use: --limit @/home/user/git/ansible-test/test.retry\n\nPLAY RECAP ***********************************************************************************************************************************************************************************\n10.10.10.10               : ok=0    changed=0    unreachable=1    failed=0   \n
Run Code Online (Sandbox Code Playgroud)\n\n

ansible-test 直接层次结构:

\n\n
ansible-test$ tree\n.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 group_vars\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 all\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 inventories\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 hosts\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test.yml\n\n2 directories, 3 files\n
Run Code Online (Sandbox Code Playgroud)\n\n

测试.yaml:

\n\n
- name: test\n  hosts: vm\n  tasks:\n    - name: get ipconfig information\n      raw: ipconfg\n      register: ipconfig_out\n    - name: print stdout\n      debug: var=ipconfig_out.stdout_lines\n
Run Code Online (Sandbox Code Playgroud)\n\n

库存/主机

\n\n
[vm]\n10.10.10.10\n
Run Code Online (Sandbox Code Playgroud)\n\n

组变量/全部

\n\n
ansible_connection: winrm\nansible_user: Administrator\nansible_password: password\n
Run Code Online (Sandbox Code Playgroud)\n\n

但是如果我通过 WinRM 从其他 Windows 主机连接到目标 10.10.10.10:

\n\n
 Set-Item wsman:\\localhost\\Client\\TrustedHosts -value 10.10.10.10\n Enter-PSSession -ComputerName 10.10.10.10 -Credential (Get-Credential -UserName Administrator)\n
Run Code Online (Sandbox Code Playgroud)\n\n

一切都好!

\n\n

我在 Ansible 中做错了什么?

\n\n

PS\n目标主机是 Windows Server 2012

\n

use*_*618 5

我没有配置 WinRM。

使固定:

  1. https://docs.ansible.com/ansible/latest/user_guide/windows_setup.html#winrm-setup
  2. 添加ansible_winrm_server_cert_validation: ignore到我的 group_vars 文件中。