Ansible と Windows Server 間の接続タイムアウト

Ansible と Windows Server 間の接続タイムアウト

私はAnsibleの勉強を始めており、実際の仕事にそれを適用したいと考えていました。Windows Nano Server 2016を管理するためにUbuntuからAnsibleを実装しています。ユーザーアカウントにマップされた証明書を使用するように構成しています。このチュートリアルはここに基づいています。https://adamtheautomator.com/winrm-https-ansible/ より


エラー

win_ping モジュールを使用して接続をテストしたところ、次の出力が得られました。

**ansible windows -vvvv -i hosts -m win_ping**

NanoServer | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: OpenSSH_8.2p1 Ubuntu-4ubuntu0.1, OpenSSL 1.1.1f  31 Mar 2020
    debug1: Reading configuration data /etc/ssh/ssh_config
    debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
    debug1: /etc/ssh/ssh_config line 21: Applying options for 
    *debug1: auto-mux: Trying existing master
    debug1: Control socket \"/home/woliveira/.ansible/cp/6df2d837bc\" does not exist
    debug2: resolving \"nanoserver\" port 5986
    debug2: ssh_connect_direct
    debug1: Connecting to nanoserver [10.11.39.250] port 5986.
    debug2: fd 3 setting O_NONBLOCK
    debug1: fd 3 clearing O_NONBLOCK
    debug1: Connection established.
    debug3: timeout: 25000 ms remain after connect
    debug1: identity file /home/woliveira/.ssh/id_rsa type -1
    debug1: identity file /home/woliveira/.ssh/id_rsa-cert type -1
    debug1: identity file /home/woliveira/.ssh/id_dsa type -1
    debug1: identity file /home/woliveira/.ssh/id_dsa-cert type -1
    debug1: identity file /home/woliveira/.ssh/id_ecdsa type -1
    debug1: identity file /home/woliveira/.ssh/id_ecdsa-cert type -1
    debug1: identity file /home/woliveira/.ssh/id_ecdsa_sk type -1
    debug1: identity file /home/woliveira/.ssh/id_ecdsa_sk-cert type -1
    debug1: identity file /home/woliveira/.ssh/id_ed25519 type -1
    debug1: identity file /home/woliveira/.ssh/id_ed25519-cert type -1
    debug1: identity file /home/woliveira/.ssh/id_ed25519_sk type -1
    debug1: identity file /home/woliveira/.ssh/id_ed25519_sk-cert type -1
    debug1: identity file /home/woliveira/.ssh/id_xmss type -1
    debug1: identity file /home/woliveira/.ssh/id_xmss-cert type -1
    debug1: Local version string SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.1
    **Connection timed out during banner exchange",
    "unreachable": true**
}

次に、Ansible 構成ファイルでタイムアウトを増やそうとしましたが、ピアによる接続のリセットが発生しました。

NanoServer | UNREACHABLE! => {
        "changed": false,
        "msg": "Failed to connect to the host via ssh: OpenSSH_8.2p1 Ubuntu-4ubuntu0.1, OpenSSL 1.1.1f  31 Mar 2020
        debug1: Reading configuration data /etc/ssh/ssh_config
        debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
        debug1: /etc/ssh/ssh_config line 21: Applying options for 
        *debug1: auto-mux: Trying existing master
        debug1: Control socket \"/home/woliveira/.ansible/cp/6df2d837bc\" does not exist
        debug2: resolving \"nanoserver\" port 5986
        debug2: ssh_connect_direct
        debug1: Connecting to nanoserver [10.11.39.250] port 5986.
        debug2: fd 3 setting O_NONBLOCK
        debug1: fd 3 clearing O_NONBLOCK
        debug1: Connection established.
        debug3: timeout: 3600000 ms remain after connect
        debug1: identity file /home/woliveira/.ssh/id_rsa type -1
        debug1: identity file /home/woliveira/.ssh/id_rsa-cert type -1
        debug1: identity file /home/woliveira/.ssh/id_dsa type -1
        debug1: identity file /home/woliveira/.ssh/id_dsa-cert type -1
        debug1: identity file /home/woliveira/.ssh/id_ecdsa type -1
        debug1: identity file /home/woliveira/.ssh/id_ecdsa-cert type -1
        debug1: identity file /home/woliveira/.ssh/id_ecdsa_sk type -1
        debug1: identity file /home/woliveira/.ssh/id_ecdsa_sk-cert type -1
        debug1: identity file /home/woliveira/.ssh/id_ed25519 type -1
        debug1: identity file /home/woliveira/.ssh/id_ed25519-cert type -1
        debug1: identity file /home/woliveira/.ssh/id_ed25519_sk type -1
        debug1: identity file /home/woliveira/.ssh/id_ed25519_sk-cert type -1
        debug1: identity file /home/woliveira/.ssh/id_xmss type -1
        debug1: identity file /home/woliveira/.ssh/id_xmss-cert type -1
        debug1: Local version string SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.1
        kex_exchange_identification: read: **Connection reset by peer",
        "unreachable": true**
    }

環境設定。

アンシブル 2.9.6 にインストールされている Ubuntu 20.04.1 LTS仮想マシン

私の Windows サーバーは次のとおりです: OS 名: Microsoft Windows Server 2016 Standard Evaluation OS バージョン: 10.0.14393 N/A ビルド 14393

両方の VM には静的 IP があり、ドメイン内にはありません。次のチュートリアルに従います。 https://vnuggets.com/2019/08/08/ansible-certificate-authentication-to-windows/

ansibleoperator という Windows ローカル ユーザーを作成し、このユーザーをローカル管理者グループに追加しました。これが私の Ansible ホスト ファイルです:

[windows]
NanoServer

[windows:vars]
ansible_user=ansibleoperator
ansible_password=*********
ansble_connection=winrm
ansible_winrm_cert_pem=/home/woliveira/cert.pem
ansible_winrm_cert_key_pem=/home/woliveira/cert_key.pem
ansible_port=5986
ansible_winrm_scheme=https
ansible_winrm_server_cert_validation=ignore

Windows ファイアウォールは無効です。TrustedHosts は任意のホストに対して構成されています (値 = *)

確認したこと

別の Windows ホストが winrm を使用して接続できる場合: PS C:> Test-WSMan -ComputerName "NanoServer" -UseSSL

wsmid           : http://schemas.dmtf.org/wbem/wsman/identity/1/wsmanidentity.xsd
ProtocolVersion : http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd
ProductVendor   : Microsoft Corporation
ProductVersion  : OS: 0.0.0 SP: 0.0 Stack: 3.0

Linux 側では、次のコマンドを使用して Netcat 5986 ポートの接続をテストできます。

 nc -z -w1 NanoServer 5986;echo $?

出力は0です(コマンドは成功しました)

すでに他の Windows から接続しようとしましたが、今回は Ansible が使用するのと同じライブラリであるため、pywinrm スクリプトを使用します。次のスクリプトを実行します。

import winrm

cert_pem = 'C:/Users/woliveira.adm/Desktop/CertificadoAnsible/cert.pem'
cert_key_pem = 'C:/Users/woliveira.adm/Desktop/CertificadoAnsible/cert_key.pem'


session = winrm.Session('NanoServer:5986', auth=('ansibleoperator', '********'),
                        transport='certificate',
                        cert_pem=cert_pem, 
                        cert_key_pem=cert_key_pem,
                        server_cert_validation='ignore',
                        )
session.run_cmd('ipconfig')

そしてこれを受け取ります:

Traceback (most recent call last):

  File "<ipython-input-56-b902c730fb9c>", line 1, in <module>
    runfile('C:/Users/woliveira.adm/Desktop/testewinrm.py', wdir='C:/Users/woliveira.adm/Desktop')

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/woliveira.adm/Desktop/testewinrm.py", line 19, in <module>
    session.run_cmd('ipconfig')

  File "C:\ProgramData\Anaconda3\lib\site-packages\winrm\__init__.py", line 39, in run_cmd
    shell_id = self.protocol.open_shell()

  File "C:\ProgramData\Anaconda3\lib\site-packages\winrm\protocol.py", line 166, in open_shell
    res = self.send_message(xmltodict.unparse(req))

  File "C:\ProgramData\Anaconda3\lib\site-packages\winrm\protocol.py", line 243, in send_message
    resp = self.transport.send_message(message)

  File "C:\ProgramData\Anaconda3\lib\site-packages\winrm\transport.py", line 323, in send_message
    response = self._send_message_request(prepared_request, message)

  File "C:\ProgramData\Anaconda3\lib\site-packages\winrm\transport.py", line 328, in _send_message_request
    response = self.session.send(prepared_request, timeout=self.read_timeout_sec)

  File "C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)

  File "C:\ProgramData\Anaconda3\lib\site-packages\requests\adapters.py", line 529, in send
    raise ReadTimeout(e, request=request)

ReadTimeout: HTTPConnectionPool(host='10.11.39.250', port=5986): Read timed out. (read timeout=30)

これをデバッグする方法がわかりません。何を確認すればよいでしょうか?

私の英語が下手で申し訳ありません。何か不明な点がありましたら、お知らせください。

答え1

vars ファイルにタイプミスがあります:

ansble_connection=winrm

これは次のようになります:

ansible_connection=winrm

i欠けているものがあります)。

そのため、Ansible は WinRM ではなく SSH 経由で接続しようとします。

関連情報