使用 Ansible ping EC2 时端口 22 超时

问题描述 投票:0回答:1

我正在使用 WSL2 和一个带有

RHEL-9.3.0_HVM-20231101-x86_64-5-Hourly2-GP2
AMI 的小型 EC2 实例。目的是学习 Ansible。

当从 WSL ssh 我的实例时,一切正常并建立连接

ssh -i "my_keys.pem" [email protected]

但是,跑步时:

ansible all --key-file ../my_keys.pem -i inventory -m ping

我收到错误:

xx.xx.xx.xx| UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: ssh: connect to host xx.xxx.xxx.xx port 22: Connection timed out",
    "unreachable": true
}

仔细检查以下内容:

  • Ansible 的配置
  • EC2 端口和允许的 IP

这是使用

-vvvv
的输出:

<xx.xxx.xxx.xx> ESTABLISH SSH CONNECTION FOR USER: None
<xx.xxx.xxx.xx> SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile="/home/xyz/xyz/my_keys.pem"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/xyz/.ansible/cp/1f201fc9f0 xx.xxx.xxx.xx'/bin/sh -c '"'"'echo ~ && sleep 0'"'"''
<xx.xxx.xxx.xx> (255, b'', b'OpenSSH_8.2p1 Ubuntu-4ubuntu0.11, OpenSSL 1.1.1f  31 Mar 2020\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files\r\ndebug1: /etc/ssh/ssh_config line 21: Applying options for *\r\ndebug2: resolve_canonicalize: hostname xx.xxx.xxx.xx is address\r\ndebug1: auto-mux: Trying existing master\r\ndebug1: Control socket "/home/xyz/.ansible/cp/1f201fc9f0" does not exist\r\ndebug2: ssh_connect_direct\r\ndebug1: Connecting to xx.xxx.xxx.xx [xx.xxx.xxx.xx] port 22.\r\ndebug2: fd 3 setting O_NONBLOCK\r\ndebug1: connect to address xx.xxx.xxx.xx port 22: Connection timed out\r\nssh: connect to host xx.xxx.xxx.xxx port 22: Connection timed out\r\n')



"msg": 
    "Failed to connect to the host via ssh: OpenSSH_8.2p1 Ubuntu-4ubuntu0.11, OpenSSL 1.1.1f  31 Mar 2020
    ndebug1: Reading configuration data /etc/ssh/ssh_config
    ndebug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files\r\ndebug1: /etc/ssh/ssh_config line 21: Applying options for *
    ndebug2: resolve_canonicalize: hostname xx.xx.xxx.xx is address
    ndebug1: auto-mux: Trying existing master
    ndebug1: Control socket \"/home/xyz/.ansible/cp/1f201fc9f0\" does not exist
    ndebug2: ssh_connect_direct\r\ndebug1: Connecting to xx.xxx.xxx.xx [xx.xxx.xxx.xx] port 22.
    ndebug2: fd 3 setting O_NONBLOCK
    ndebug1: connect to address xx.xxx.xx.xx port 22: Connection timed out
    nssh: connect to host xx.xxx.xxx.xx port 22: Connection timed out",

感谢您的建议!

amazon-ec2 ansible windows-subsystem-for-linux
1个回答
0
投票

inventory
文件,如果是 EC2,则需要
Public IPv4 DNS
格式。

ansible_user
也可以帮助跳过使用 cli
-u
参数传递它。

ec2-xx-xxx-xxx-xx.region-number.compute.amazonaws.com ansible_user=ec2-user
© www.soinside.com 2019 - 2024. All rights reserved.