使用 os_server 模块和本地连接时获取 "winrm send_input failed"

Getting "winrm send_input failed" when using os_server module and local connection

我正在尝试为 Windows VM 编写剧本,该剧本还使用 os_server 模块创建 VM。 我从一个简单的 win_ping 开始,因为 VM 已经存在:

- name: Create instance
  hosts: all
  tasks:
    - name: Ping machine
      win_ping:

运行将其与 ansible-playbook site.yml --inventory=10.204.0.9, 结合 结果:

PLAY [Create instance] ************************************************************************

TASK [Gathering Facts] ************************************************************************
ok: [10.204.0.9]

TASK [Ping machine] ***************************************************************************
ok: [10.204.0.9]

PLAY RECAP ************************************************************************************
10.204.0.9                 : ok=2    changed=0    unreachable=0    failed=0   

现在我添加 os_server 任务:

- name: Create Windows Instance
  connection: local
  os_server:
    state: present
    region_name: "{{ os_region_name }}"
    auth: "{{ cloud.auth }}"
    name: "windows-{{ inventory_hostname }}"
    image: Windows 2012 R2 Datacenter
    key_name: vector_ops
    flavor: 1C-2GB-50GB
    floating_ips:
      - "{{ inventory_hostname }}"
- name: Ping machine
  win_ping:

我将 connection 设置为 local,因为我希望从控制机器执行此任务,以防尚未创建 VM。

当我再次使用 ansible-playbook site.yml --inventory=10.204.0.9, 运行 这个剧本时,我得到:

TASK [Create Windows Instance] ****************************************************************
 [WARNING]: FATAL ERROR DURING FILE TRANSFER: Traceback (most recent call last):   File
"/usr/lib/python2.7/dist-packages/ansible/plugins/connection/winrm.py", line 276, in
_winrm_exec     self._winrm_send_input(self.protocol, self.shell_id, command_id, data,
eof=is_last)   File "/usr/lib/python2.7/dist-packages/ansible/plugins/connection/winrm.py",
line 256, in _winrm_send_input     protocol.send_message(xmltodict.unparse(rq))   File
"/usr/local/lib/python2.7/dist-packages/winrm/protocol.py", line 207, in send_message
return self.transport.send_message(message)   File "/usr/local/lib/python2.7/dist-
packages/winrm/transport.py", line 202, in send_message     raise WinRMTransportError('http',
error_message) WinRMTransportError: (u'http', u'Bad HTTP response returned from server. Code
500')

fatal: [10.204.0.9]: FAILED! => {"msg": "winrm send_input failed"}

我有点疑惑为什么一个文件t运行sfer的时候会出错,所以我运行命令-vvv:

TASK [Create Windows Instance] ****************************************************************
task path: /home/ubuntu/basic-windows-example/trunk/playbooks/site.yml:8
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/cloud/openstack/os_server.py
<10.204.0.9> ESTABLISH WINRM CONNECTION FOR USER: Admin on PORT 5986 TO 10.204.0.9
EXEC (via pipeline wrapper)

尽管 connection: local,但 Ansible 似乎确实尝试建立 winrm 连接。从任务中删除 connection: local 会产生与上述相同的结果。 我希望任务 return 是一个简单的 "ok",因为 VM 已经存在。 我在这里错过了什么?

更新 2018-01-09,9:45 GMT:

所以我尝试了另一个实验:我从 var 文件中删除了所有 ansible_* 变量(见下文)只是为了看看 Ansible 在没有配置 WinRM 连接时对 os_server 任务做了什么。 运行 再次使用 ansible-playbook site.yml --inventory=10.204.0.9, -vvv 这次我得到了 os_server 任务:

TASK [Create Windows Instance] ****************************************************************
task path: /home/ubuntu/basic-windows-example/trunk/playbooks/site.yml:9
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/cloud/openstack/os_server.py
<10.204.0.9> ESTABLISH LOCAL CONNECTION FOR USER: ubuntu
<10.204.0.9> EXEC /bin/sh -c 'echo ~ && sleep 0'
<10.204.0.9> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/ubuntu/.ansible/tmp/ansible-tmp-1515490597.4-208015762064624 `" && echo ansible-tmp-1515490597.4-208015762064624="` echo /home/ubuntu/.ansible/tmp/ansible-tmp-1515490597.4-208015762064624 `" ) && sleep 0'
<rest cut off for brevity>

因此,现在确实建立了本地连接并且 os_server 任务成功完成。但当然,这不是答案,因为我需要为 Windows VM 配置的 WinRM 连接。

更新 2018-01-09,10:00 GMT:

根据建议将 gather_facts: false 添加到游戏中并 运行ning ansible-playbook site.yml --inventory=10.204.0.9,,我现在得到:

PLAY [Create instance] ************************************************************************
META: ran handlers

TASK [Create Windows Instance] ****************************************************************
task path: /home/ubuntu/basic-windows-example/trunk/playbooks/site.yml:10
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/cloud/openstack/os_server.py
<10.204.0.9> ESTABLISH WINRM CONNECTION FOR USER: Admin on PORT 5986 TO 10.204.0.9
EXEC (via pipeline wrapper)
 [WARNING]: FATAL ERROR DURING FILE TRANSFER: Traceback (most recent call last):   File
"/usr/lib/python2.7/dist-packages/ansible/plugins/connection/winrm.py", line 276, in
_winrm_exec     self._winrm_send_input(self.protocol, self.shell_id, command_id, data,
eof=is_last)   File "/usr/lib/python2.7/dist-packages/ansible/plugins/connection/winrm.py",
line 256, in _winrm_send_input     protocol.send_message(xmltodict.unparse(rq))   File
"/usr/local/lib/python2.7/dist-packages/winrm/protocol.py", line 207, in send_message
return self.transport.send_message(message)   File "/usr/local/lib/python2.7/dist-
packages/winrm/transport.py", line 202, in send_message     raise WinRMTransportError('http',
error_message) WinRMTransportError: (u'http', u'Bad HTTP response returned from server. Code
500')

fatal: [10.204.0.9]: FAILED! => {
    "msg": "winrm send_input failed"
}

还是一样的错误,Ansible还是尝试建立WinRM连接。

完整剧本(site.yml,已添加 gather_facts: false):

- name: Create instance
  hosts: all
  gather_facts: false
  tasks:
    - name: Create Windows Instance
      connection: local
      os_server:
        state: present
        region_name: Region1
        auth: "{{ cloud.auth }}"
        name: "windows-{{ inventory_hostname }}"
        image: Windows 2012 R2 Datacenter
        key_name: mykey
        flavor: 1C-2GB-50GB
        floating_ips:
          - "{{ inventory_hostname }}"
    - name: Ping machine
      win_ping:

group_vars/all 中的变量(在所有示例中使用):

cloud:
  auth:
    auth_url: https://cloud.internal:5000/v3/
    domain_name: Domain_01
    password: mypassword
    project_name: dev-project
    username: apiuser
os_region_name: Fra1
ansible_user: Admin
ansible_port: 5986
ansible_password: myvmpassword
ansible_connection: winrm
ansible_winrm_server_cert_validation: ignore

版本信息:

ansible --version
ansible 2.4.2.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/ubuntu/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.12 (default, Nov 20 2017, 18:23:56) [GCC 5.4.0 20160609]

如果我对 os_server 任务使用 delegate_to: localhost 而不是 connection: local,则本地连接会建立。 delegate_to 避免为该连接加载 WinRM 连接变量。

如果其他人遇到与 Ansible 相同的问题,请检查主机上的 WinRM 内存设置并确保它有足够的内存。

Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 1024