qq_41990538 2024-05-27 11:06 采纳率: 0%
浏览 3
已结题

ansible连接设备报错

ansible连接设备时报错,这种应该如何解决。远端设备字符编码是gb2312,我不知道这个有没有影响
我使用的命令如下:
root@jms_celery:~# ansible all -i 192.168.16.201, -u admin --ask-pass -m raw -a "dis version" -vvvvvv -k

192.168.16.201 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Warning: Permanently added '192.168.16.201' (RSA) to the list of known hosts.\r\nShared connection to 192.168.16.201 closed.",
    "unreachable": true
}

root@jms_celery:~#  ansible all -i 192.168.16.201, -u maipu --ask-pass -m raw -a "dis version" -vvvvvv -k
ansible [core 2.14.1]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/opt/jumpserver/apps/libs/ansible/modules', '/etc/ansible/modules']
  ansible python module location = /opt/py3/lib/python3.11/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /opt/py3/bin/ansible
  python version = 3.11.9 (main, May 14 2024, 08:32:26) [GCC 10.2.1 20210110] (/opt/py3/bin/python)
  jinja version = 3.1.2
  libyaml = True
Using /etc/ansible/ansible.cfg as config file
SSH password: 
setting up inventory plugins
Parsed 192.168.16.201, inventory source with host_list plugin
Loading callback plugin minimal of type stdout, v2.0 from /opt/py3/lib/python3.11/site-packages/ansible/plugins/callback/minimal.py
Attempting to use 'default' callback.
Skipping callback 'default', as we already have a stdout callback.
Attempting to use 'junit' callback.
Attempting to use 'minimal' callback.
Skipping callback 'minimal', as we already have a stdout callback.
Attempting to use 'oneline' callback.
Skipping callback 'oneline', as we already have a stdout callback.
Attempting to use 'tree' callback.
<192.168.16.201> ESTABLISH SSH CONNECTION FOR USER: maipu
<192.168.16.201> SSH: ansible.cfg set ssh_args: (-C)(-o)(ControlMaster=auto)(-o)(ControlPersist=60s)
<192.168.16.201> SSH: ANSIBLE_HOST_KEY_CHECKING/host_key_checking disabled: (-o)(StrictHostKeyChecking=no)
<192.168.16.201> SSH: ANSIBLE_REMOTE_USER/remote_user/ansible_user/user/-u set: (-o)(User="maipu")
<192.168.16.201> SSH: ANSIBLE_TIMEOUT/timeout set: (-o)(ConnectTimeout=65)
<192.168.16.201> SSH: Set ssh_common_args: ()
<192.168.16.201> SSH: Set ssh_extra_args: ()
<192.168.16.201> SSH: found only ControlPersist; added ControlPath: (-o)(ControlPath="/root/.ansible/cp/f074e2c900")
<192.168.16.201> SSH: EXEC sshpass -d10 ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o 'User="maipu"' -o ConnectTimeout=65 -o 'ControlPath="/root/.ansible/cp/f074e2c900"' -tt 192.168.16.201 'dis version'
<192.168.16.201> (255, b'', b'OpenSSH_8.4p1 Debian-5+deb11u3, OpenSSL 1.1.1w  11 Sep 2023\r
debug1: Reading configuration data /root/.ssh/config\r
debug1: /root/.ssh/config line 1: Applying options for *\r
debug3: kex names ok: [diffie-hellman-group1-sha1]\r
debug1: Reading configuration data /etc/ssh/ssh_config\r
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files\r
debug1: /etc/ssh/ssh_config line 21: Applying options for *\r
debug2: resolve_canonicalize: hostname 192.168.16.201 is address\r
debug1: auto-mux: Trying existing master\r
debug1: Control socket "/root/.ansible/cp/f074e2c900" does not exist\r
debug2: ssh_connect_direct\r
debug1: Connecting to 192.168.16.201 [192.168.16.201] port 22.\r
debug2: fd 3 setting O_NONBLOCK\r
debug1: fd 3 clearing O_NONBLOCK\r
debug1: Connection established.\r
debug3: timeout: 64998 ms remain after connect\r
debug1: identity file /root/.ssh/id_rsa type -1\r
debug1: identity file /root/.ssh/id_rsa-cert type -1\r
debug1: identity file /root/.ssh/id_dsa type -1\r
debug1: identity file /root/.ssh/id_dsa-cert type -1\r
debug1: identity file /root/.ssh/id_ecdsa type -1\r
debug1: identity file /root/.ssh/id_ecdsa-cert type -1\r
debug1: identity file /root/.ssh/id_ecdsa_sk type -1\r
debug1: identity file /root/.ssh/id_ecdsa_sk-cert type -1\r
debug1: identity file /root/.ssh/id_ed25519 type -1\r
debug1: identity file /root/.ssh/id_ed25519-cert type -1\r
debug1: identity file /root/.ssh/id_ed25519_sk type -1\r
debug1: identity file /root/.ssh/id_ed25519_sk-cert type -1\r
debug1: identity file /root/.ssh/id_xmss type -1\r
debug1: identity file /root/.ssh/id_xmss-cert type -1\r
debug1: Local version string SSH-2.0-OpenSSH_8.4p1 Debian-5+deb11u3\r
debug1: Remote protocol version 1.99, remote software version IPSSH_5.1.0p1\r
debug1: no match: IPSSH_5.1.0p1\r
debug2: fd 3 setting O_NONBLOCK\r
debug1: Authenticating to 192.168.16.201:22 as \'maipu\'\r
debug3: hostkeys_foreach: reading file "/dev/null"\r
debug3: send packet: type 20\r
debug1: SSH2_MSG_KEXINIT sent\r
debug3: receive packet: type 20\r
debug1: SSH2_MSG_KEXINIT received\r
debug2: local client KEXINIT proposal\r
debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,diffie-hellman-group1-sha1,ext-info-c,kex-strict-c-v00@openssh.com\r
debug2: host key algorithms: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,ssh-ed25519-cert-v01@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,sk-ecdsa-sha2-nistp256@openssh.com,ssh-ed25519,sk-ssh-ed25519@openssh.com,rsa-sha2-512,rsa-sha2-256,ssh-rsa\r
debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,aes128-cbc\r
debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,aes128-cbc\r
debug2: MACs ctos: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1\r
debug2: MACs stoc: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1\r
debug2: compression ctos: zlib@openssh.com,zlib,none\r
debug2: compression stoc: zlib@openssh.com,zlib,none\r
debug2: languages ctos: \r
debug2: languages stoc: \r
debug2: first_kex_follows 0 \r
debug2: reserved 0 \r
debug2: peer server KEXINIT proposal\r
debug2: KEX algorithms: diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1,\r
debug2: host key algorithms: ssh-rsa,ssh-dss\r
debug2: ciphers ctos: aes128-ctr,aes192-ctr,aes256-ctr,\r
debug2: ciphers stoc: aes128-ctr,aes192-ctr,aes256-ctr,\r
debug2: MACs ctos: hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96,\r
debug2: MACs stoc: hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96,\r
debug2: compression ctos: none,zlib@openssh.com,zlib,\r
debug2: compression stoc: none,zlib@openssh.com,zlib,\r
debug2: languages ctos: \r
debug2: languages stoc: \r
debug2: first_kex_follows 0 \r
debug2: reserved 0 \r
debug1: kex: algorithm: diffie-hellman-group-exchange-sha256\r
debug1: kex: host key algorithm: ssh-rsa\r
debug1: kex: server->client cipher: aes128-ctr MAC: umac-64@openssh.com compression: zlib@openssh.com\r
debug1: kex: client->server cipher: aes128-ctr MAC: umac-64@openssh.com compression: zlib@openssh.com\r
debug3: send packet: type 34\r
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(2048<3072<8192) sent\r
debug3: receive packet: type 31\r
debug1: got SSH2_MSG_KEX_DH_GEX_GROUP\r
debug2: bits set: 1028/2048\r
debug3: send packet: type 32\r
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent\r
debug3: receive packet: type 33\r
debug1: got SSH2_MSG_KEX_DH_GEX_REPLY\r
debug1: Server host key: ssh-rsa SHA256:/COud+QRv/W+KMJYk215d4I3GJcflLoF/NWFpbLv1N0\r
debug3: hostkeys_foreach: reading file "/dev/null"\r
Warning: Permanently added \'192.168.16.201\' (RSA) to the list of known hosts.\r
debug2: bits set: 1057/2048\r
debug3: send packet: type 21\r
debug2: set_newkeys: mode 1\r
debug1: rekey out after 4294967296 blocks\r
debug1: SSH2_MSG_NEWKEYS sent\r
debug1: expecting SSH2_MSG_NEWKEYS\r
debug3: receive packet: type 21\r
debug1: SSH2_MSG_NEWKEYS received\r
debug2: set_newkeys: mode 0\r
debug1: rekey in after 4294967296 blocks\r
debug1: Will attempt key: /root/.ssh/id_rsa \r
debug1: Will attempt key: /root/.ssh/id_dsa \r
debug1: Will attempt key: /root/.ssh/id_ecdsa \r
debug1: Will attempt key: /root/.ssh/id_ecdsa_sk \r
debug1: Will attempt key: /root/.ssh/id_ed25519 \r
debug1: Will attempt key: /root/.ssh/id_ed25519_sk \r
debug1: Will attempt key: /root/.ssh/id_xmss \r
debug2: pubkey_prepare: done\r
debug3: send packet: type 5\r
debug3: receive packet: type 6\r
debug2: service_accept: ssh-userauth\r
debug1: SSH2_MSG_SERVICE_ACCEPT received\r
debug3: send packet: type 50\r
debug3: receive packet: type 51\r
debug1: Authentications that can continue: publickey,password\r
debug3: start over, passed a different list publickey,password\r
debug3: preferred gssapi-with-mic,publickey,keyboard-interactive,password\r
debug3: authmethod_lookup publickey\r
debug3: remaining preferred: keyboard-interactive,password\r
debug3: authmethod_is_enabled publickey\r
debug1: Next authentication method: publickey\r
debug1: Trying private key: /root/.ssh/id_rsa\r
debug3: no such identity: /root/.ssh/id_rsa: No such file or directory\r
debug1: Trying private key: /root/.ssh/id_dsa\r
debug3: no such identity: /root/.ssh/id_dsa: No such file or directory\r
debug1: Trying private key: /root/.ssh/id_ecdsa\r
debug3: no such identity: /root/.ssh/id_ecdsa: No such file or directory\r
debug1: Trying private key: /root/.ssh/id_ecdsa_sk\r
debug3: no such identity: /root/.ssh/id_ecdsa_sk: No such file or directory\r
debug1: Trying private key: /root/.ssh/id_ed25519\r
debug3: no such identity: /root/.ssh/id_ed25519: No such file or directory\r
debug1: Trying private key: /root/.ssh/id_ed25519_sk\r
debug3: no such identity: /root/.ssh/id_ed25519_sk: No such file or directory\r
debug1: Trying private key: /root/.ssh/id_xmss\r
debug3: no such identity: /root/.ssh/id_xmss: No such file or directory\r
debug2: we did not send a packet, disable method\r
debug3: authmethod_lookup password\r
debug3: remaining preferred: ,password\r
debug3: authmethod_is_enabled password\r
debug1: Next authentication method: password\r
debug3: send packet: type 50\r
debug2: we sent a password packet, wait for reply\r
debug3: receive packet: type 52\r
debug1: Enabling compression at level 6.\r
debug1: Authentication succeeded (password).\r
Authenticated to 192.168.16.201 ([192.168.16.201]:22).\r
debug1: setting up multiplex master socket\r
debug3: muxserver_listen: temporary control path /root/.ansible/cp/f074e2c900.K0EE9FahfYs3haSC\r
debug2: fd 4 setting O_NONBLOCK\r
debug3: fd 4 is O_NONBLOCK\r
debug3: fd 4 is O_NONBLOCK\r
debug1: channel 0: new [/root/.ansible/cp/f074e2c900]\r
debug3: muxserver_listen: mux listener channel 0 fd 4\r
debug2: fd 3 setting TCP_NODELAY\r
debug3: ssh_packet_set_tos: set IP_TOS 0x08\r
debug1: control_persist_detach: backgrounding master process\r
debug2: control_persist_detach: background process is 2404\r
debug2: fd 4 setting O_NONBLOCK\r
debug1: forking to background\r
debug1: Entering interactive session.\r
debug1: pledge: id\r
debug2: set_control_persist_exit_time: schedule exit in 60 seconds\r
debug1: multiplexing control connection\r
debug2: fd 5 setting O_NONBLOCK\r
debug3: fd 5 is O_NONBLOCK\r
debug1: channel 1: new [mux-control]\r
debug3: channel_post_mux_listener: new mux channel 1 fd 5\r
debug3: mux_master_read_cb: channel 1: hello sent\r
debug2: set_control_persist_exit_time: cancel scheduled exit\r
debug3: mux_master_read_cb: channel 1 packet type 0x00000001 len 4\r
debug2: mux_master_process_hello: channel 1 client version 4\r
debug2: mux_client_hello_exchange: master version 4\r
debug3: mux_client_forwards: request forwardings: 0 local, 0 remote\r
debug3: mux_client_request_session: entering\r
debug3: mux_client_request_alive: entering\r
debug3: mux_master_read_cb: channel 1 packet type 0x10000004 len 4\r
debug2: mux_master_process_alive_check: channel 1: alive check\r
debug3: mux_client_request_alive: done pid = 2406\r
debug3: mux_client_request_session: session request sent\r
debug3: mux_master_read_cb: channel 1 packet type 0x10000002 len 72\r
debug2: mux_master_process_new_session: channel 1: request tty 1, X 0, agent 0, subsys 0, term "xterm", cmd "dis version", env 1\r
debug3: mux_master_process_new_session: got fds stdin 6, stdout 7, stderr 8\r
debug2: fd 7 setting O_NONBLOCK\r
debug2: fd 8 setting O_NONBLOCK\r
debug1: channel 2: new [client-session]\r
debug2: mux_master_process_new_session: channel_new: 2 linked to control channel 1\r
debug2: channel 2: send open\r
debug3: send packet: type 90\r
debug3: receive packet: type 91\r
debug2: channel_input_open_confirmation: channel 2: callback start\r
debug2: client_session2_setup: id 2\r
debug2: channel 2: request pty-req confirm 1\r
debug3: send packet: type 98\r
debug1: Sending environment.\r
debug1: Sending env LANG = zh_CN.UTF-8\r
debug2: channel 2: request env confirm 0\r
debug3: send packet: type 98\r
debug1: Sending command: dis version\r
debug2: channel 2: request exec confirm 1\r
debug3: send packet: type 98\r
debug3: mux_session_confirm: sending success reply\r
debug2: channel_input_open_confirmation: channel 2: callback done\r
debug2: channel 2: open confirm rwindow 0 rmax 32768\r
debug1: mux_client_request_session: master session id: 2\r
debug3: receive packet: type 99\r
debug2: channel_input_status_confirm: type 99 id 2\r
debug2: PTY allocation request accepted on channel 2\r
debug3: receive packet: type 100\r
debug2: channel_input_status_confirm: type 100 id 2\r
debug2: channel 2: read failed\r
debug2: channel 2: chan_shutdown_read (i0 o0 sock -1 wfd 6 efd 8 [write])\r
debug2: channel 2: input open -> drain\r
debug2: channel 2: write failed\r
debug2: channel 2: chan_shutdown_write (i1 o0 sock -1 wfd 7 efd 8 [write])\r
debug2: channel 2: send eow\r
debug2: channel 2: output open -> closed\r
debug2: channel 2: ibuf empty\r
debug2: channel 2: send eof\r
debug3: send packet: type 96\r
debug2: channel 2: input drain -> closed\r
debug2: channel 2: send close\r
debug3: send packet: type 97\r
debug3: channel 2: will not send data after close\r
debug3: receive packet: type 97\r
debug2: channel 2: rcvd close\r
debug3: channel 2: will not send data after close\r
debug2: channel 2: is dead\r
debug2: channel 2: gc: notify user\r
debug3: mux_master_session_cleanup_cb: entering for channel 2\r
debug2: channel 1: rcvd close\r
debug2: channel 1: output open -> drain\r
debug2: channel 1: chan_shutdown_read (i0 o1 sock 5 wfd 5 efd -1 [closed])\r
debug2: channel 1: input open -> closed\r
debug2: channel 2: gc: user detached\r
debug2: channel 2: is dead\r
debug2: channel 2: garbage collecting\r
debug1: channel 2: free: client-session, nchannels 3\r
debug3: channel 2: status: The following connections are open:\r
  #1 mux-control (t16 nr0 i3/0 o1/0 e[closed]/0 fd 5/5/-1 sock 5 cc -1)\r
  #2 client-session (t4 r0 i3/0 o3/0 e[write]/21 fd -1/-1/8 sock -1 cc -1)\r
\r
debug2: channel 1: obuf empty\r
debug2: channel 1: chan_shutdown_write (i3 o1 sock 5 wfd 5 efd -1 [closed])\r
debug2: channel 1: output drain -> closed\r
debug2: channel 1: is dead (local)\r
debug2: channel 1: gc: notify user\r
debug3: mux_master_control_cleanup_cb: entering for channel 1\r
debug2: channel 1: gc: user detached\r
debug2: channel 1: is dead (local)\r
debug2: channel 1: garbage collecting\r
debug1: channel 1: free: mux-control, nchannels 2\r
debug3: channel 1: status: The following connections are open:\r
  #1 mux-control (t16 nr0 i3/0 o3/0 e[closed]/0 fd 5/5/-1 sock 5 cc -1)\r
\r
debug2: set_control_persist_exit_time: schedule exit in 60 seconds\r
debug3: mux_client_read_packet: read header failed: Broken pipe\r
debug2: Control master terminated unexpectedly\r
Shared connection to 192.168.16.201 closed.\r


  • 写回答

25条回答 默认 最新

  • 阿里嘎多学长 2024-05-27 11:06
    关注
    获得0.30元问题酬金

    以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:


    根据您提供的错误日志和命令行输出,我们可以逐步分析并尝试解决Ansible连接设备时遇到的问题。

    问题分析

    1. SSH连接失败: 错误信息 "Failed to connect to the host via ssh" 表明Ansible无法通过SSH连接到指定的设备。

    2. 已知主机列表更新: 错误日志中提到 "Warning: Permanently added '192.168.16.201' (RSA) to the list of known hosts." 这意味着Ansible在尝试连接时,将目标主机的RSA密钥添加到了已知主机列表中。这通常是首次连接到一个新主机时的行为。

    3. 共享连接关闭: "Shared connection to 192.168.16.201 closed." 表明SSH连接被意外关闭。

    可能的原因

    • SSH密钥问题: 如果是首次连接到设备,可能需要接受设备的SSH密钥。但是,日志显示密钥已经被添加。
    • SSH配置问题: 可能存在SSH配置问题,如端口、密钥文件路径等。
    • 网络问题: 网络连接不稳定或防火墙设置可能阻止了SSH连接。
    • 权限问题: 指定的用户可能没有足够的权限执行命令。

    解决步骤

    1. 检查SSH密钥: 确认/root/.ssh/known_hosts文件中是否已经有了该设备的条目。如果有,尝试删除后重新连接。

    2. 检查SSH配置: 检查Ansible配置文件/etc/ansible/ansible.cfg和用户的SSH配置文件~/.ssh/config,确认配置正确。

    3. 检查网络连接: 使用pingtelnet命令测试网络连接是否正常。

    4. 检查用户权限: 确保用户admin有权限通过SSH登录到设备,并且有执行dis version命令的权限。

    5. 使用SSH直接连接: 尝试直接使用SSH命令连接到设备,看是否能够成功,例如:

      ssh -vvv -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=admin -o ConnectTimeout=65 admin@192.168.16.201
      

      这可以帮助确定问题是否出在Ansible还是SSH配置。

    6. 检查Ansible playbook: 如果您使用的是Ansible playbook,检查其中的相关配置。

    代码实现

    如果您需要修改Ansible配置或SSH配置,可以参考以下示例:

    Ansible配置 (/etc/ansible/ansible.cfg):

    [defaults]
    host_key_checking = False
    

    SSH配置 (~/.ssh/config):

    Host 192.168.16.201
        User admin
        Port 22
        ControlMaster auto
        ControlPersist 60s
        StrictHostKeyChecking no
    

    参考资料

    请尝试上述步骤,并根据结果进一步调试。如果问题仍然存在,可能需要更详细的日志信息或考虑联系设备供应商获取支持。

    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 6月4日
  • 修改了问题 5月27日
  • 创建了问题 5月27日

悬赏问题

  • ¥15 office打开卡退(新电脑重装office系统后)
  • ¥300 FLUENT 火箭发动机燃烧EDC仿真
  • ¥15 【Hadoop 问题】Hadoop编译所遇问题hadoop-common: make failed with error code 2
  • ¥15 vb6.0+webbrowser无法加载某个网页求解
  • ¥15 RPA财务机器人采购付款流程
  • ¥15 计算机图形多边形及三次样条曲线绘制
  • ¥15 根据protues画的图用keil写程序
  • ¥200 如何使用postGis实现最短领规划?
  • ¥15 pyinstaller打包错误
  • ¥20 cesm的气溶胶排放文件