运行剧本时未建立 AWX SSH 连接

运行剧本时未建立 AWX SSH 连接

我正在尝试运行我的第一个剧本。在 CentOS8 上运行 AWX,尝试使用 SSH 连接到远程设备。在我的远程设备上,我已在设备上运行 ssh-genkey 并将 .pub 密钥添加到 authorized_keys 文件中。我还获取了 .pub 私钥并创建了凭证机器类型并添加了私钥。用户名和密码为空。我只能使用 root 用户连接到我的远程设备在此处输入图像描述

我的剧本是:

- name: use machine credentials(1)
  hosts: ACS
  connection: ssh
  gather_facts: false
  timeout: 10

  tasks:      
  - name: Get firmware version from host
    shell: "date"
    #shell: cat /firmware | grep ^VERSION | cut -d"=" -f2
    register: firmware_version
    tags: firmware_version

我正在尝试通过 SSH 连接到我的远程设备并从远程设备上的文件中读取固件详细信息。

当我运行剧本时,输出失败,我不相信它正在连接到远程设备。我可以从 Putty 会话手动从我的 AWX 主机通过 SSH 连接到远程设备。我尝试在 AWX 主机上使用私钥文件,并可以使用 Putty 中的密钥通过 SSH 连接到我的远程设备。

AWX 的输出:

Identity added: /runner/artifacts/196/ssh_key_data (/runner/artifacts/196/ssh_key_data)
ansible-playbook [core 2.14.2]
  config file = None
  configured module search path = ['/home/runner/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.9/site-packages/ansible
  ansible collection location = /runner/requirements_collections:/home/runner/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible-playbook
  python version = 3.9.16 (main, Dec  8 2022, 00:00:00) [GCC 11.3.1 20221121 (Red Hat 11.3.1-4)] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True
No config file found; using defaults
host_list declined parsing /runner/inventory/hosts as it did not pass its verify_file() method
Parsed /runner/inventory/hosts inventory source with script plugin
Skipping callback 'awx_display', as we already have a stdout callback.
Skipping callback 'default', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.

PLAYBOOK: acs_backup.yml *******************************************************
1 plays in acs_backup.yml

PLAY [use machine credentials(1)] **********************************************

TASK [Get firmware version from host] ******************************************
task path: /runner/project/acs_backup.yml:18
<10.162.29.138> ESTABLISH SSH CONNECTION FOR USER: root
<10.162.29.138> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="root"' -o ConnectTimeout=10 -o 'ControlPath="/runner/cp/4477df8717"' 10.162.29.138 '/bin/sh -c '"'"'echo ~root && sleep 0'"'"''
<10.162.29.138> (0, b'/home/root\\r\\n', b"Warning: Permanently added '10.162.29.138' (ED25519) to the list of known hosts.\\r\\n")
<10.162.29.138> ESTABLISH SSH CONNECTION FOR USER: root
<10.162.29.138> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="root"' -o ConnectTimeout=10 -o 'ControlPath="/runner/cp/4477df8717"' 10.162.29.138 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /home/root/.ansible/tmp `"&& mkdir "` echo /home/root/.ansible/tmp/ansible-tmp-1677124576.2755764-27-69248210230276 `" && echo ansible-tmp-1677124576.2755764-27-69248210230276="` echo /home/root/.ansible/tmp/ansible-tmp-1677124576.2755764-27-69248210230276 `" ) && sleep 0'"'"''
<10.162.29.138> (0, b'ansible-tmp-1677124576.2755764-27-69248210230276=/home/root/.ansible/tmp/ansible-tmp-1677124576.2755764-27-69248210230276\\r\\n', b'')
<ACS-10.162.29.138> Attempting python interpreter discovery
<10.162.29.138> ESTABLISH SSH CONNECTION FOR USER: root
<10.162.29.138> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="root"' -o ConnectTimeout=10 -o 'ControlPath="/runner/cp/4477df8717"' 10.162.29.138 '/bin/sh -c '"'"'echo PLATFORM; uname; echo FOUND; command -v '"'"'"'"'"'"'"'"'python3.11'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python3.10'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python3.9'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python3.8'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python3.7'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python3.6'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python3.5'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'/usr/bin/python3'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'/usr/libexec/platform-python'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python2.7'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'/usr/bin/python'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python'"'"'"'"'"'"'"'"'; echo ENDFOUND && sleep 0'"'"''
<10.162.29.138> (0, b'PLATFORM\\r\\nLinux\\r\\nFOUND\\r\\n/usr/bin/python3.5\\r\\n/usr/bin/python3\\r\\n/usr/bin/python2.7\\r\\n/usr/bin/python\\r\\n/usr/bin/python\\r\\nENDFOUND\\r\\n', b'')
<10.162.29.138> ESTABLISH SSH CONNECTION FOR USER: root
<10.162.29.138> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="root"' -o ConnectTimeout=10 -o 'ControlPath="/runner/cp/4477df8717"' 10.162.29.138 '/bin/sh -c '"'"'/usr/bin/python3.5 && sleep 0'"'"''
fatal: [ACS-10.162.29.138]: FAILED! => {
    "changed": false,
    "msg": "The shell action failed to execute in the expected time frame (10) and was terminated"
}

PLAY RECAP *********************************************************************
ACS-10.162.29.138          : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

有人能告诉我为什么我无法通过 SSH 连接到远程设备吗?

答案1

您通过 ssh 连接时遇到问题。

10.162.29.138 是私有地址。您是否从 VPC 上的机器运行 ansible 脚本?如果不是,那么这就是首要问题你需要解决。

如果您在同一个 VPC 上或有通向子网的隧道,请尝试运行nc -zv 10.162.29.138 22- 如果连接成功,请再次尝试您的 ansible 脚本。如果仍然失败为了同样的原因,然后尝试ssh -vv ....在命令行上使用描述 nc 和 ssh 命令输出的 connectnig。

答案2

我正在连接到 GITHUB 并同步本地 AWX 主机上的项目。AWX 和我尝试 ssh 连接的设备都位于同一个本地网络 10.162.29.x 上。

我运行了你的命令。我可以使用 NCAP 和 ssh 进行连接,但在 ssh 的详细模式下,它只是显示“正在连接到 10.162.29.138 [10.162.29.138] 端口 22,所以不确定为什么会这样,并且没有完成连接。

[localadmin@centOS8-awx ~]$ nc -zv 10.162.29.138 22 Ncat:版本 7.70(https://nmap.org/ncat)Ncat:已连接到 10.162.29.138:22。Ncat:0.01 秒内发送了 0 字节,接收了 0 字节。

[localadmin@centOS8-awx ~]$ ssh[电子邮件保护] 密码:

警告:不当使用 shell 命令可能会导致数据丢失、重要系统文件被删除或其他意外结果。输入 shell 命令时,请仔细检查语法。

[root@ACS8008-0520414440 ~]# exit与 10.162.29.138 的连接已关闭。

[localadmin@centOS8-awx ~]$ ssh -vv[电子邮件保护] OpenSSH_8.0p1,OpenSSL 1.1.1k FIPS 2021 年 3 月 25 日 debug1:正在读取配置数据 /etc/ssh/ssh_config debug1:正在读取配置数据 /etc/ssh/ssh_config.d/05-redhat.conf debug2:检查“final all”主机 10.162.28.138 是否匹配,最初为 10.162.28.138 debug2:未找到匹配项 debug1:正在读取配置数据 /etc/crypto-policies/back-ends/openssh.config debug1:配置请求最终匹配通过 debug2:resolve_canonicalize:主机名 10.162.28.138 是地址 debug1:正在重新解析配置 debug1:正在读取配置数据 /etc/ssh/ssh_config debug1:正在读取配置数据 /etc/ssh/ssh_config.d/05-redhat.conf debug2:检查“final all”主机是否匹配10.162.28.138 最初为 10.162.28.138 debug2:找到匹配项 debug1:正在读取配置数据 /etc/crypto-policies/back-ends/openssh.config debug2:ssh_connect_direct debug1:连接到 10.162.28.138 [10.162.28.138] 端口 22。

相关内容