Ansible 无法使用自己的 ssh 配置连接到动态库存主机

Ansible 无法使用自己的 ssh 配置连接到动态库存主机

我的 Ansible 配置ansible.cfg

[defaults]
host_key_checking = False
remote_user = centos
roles_path = ./roles
pipelining = True

[ssh_connection]
ssh_args = -F /etc/ansible/ssh/ssh_config

ssh_config

Host bastion.host
  Hostname bastion.host
  User centos
  IdentityFile /etc/ansible/ssh/KEY.pem
  StrictHostKeyChecking no

Host 10.*.*.*
  User centos
  ProxyCommand ssh -W %h:%p bastion.host
  IdentityFile /etc/ansible/ssh/KEY.pem
  StrictHostKeyChecking no

当我尝试执行 Ansible 的剧本时

ansible -i /etc/ansible/inventories/aws-inventory/ec2.py tag_class_master -m ping

The authenticity of host 'bastion.host' can't be established.
ECDSA key fingerprint is SHA256:vwYBZHWHDXx2ehQTEhzpD4EAcTTnu5cJWsNFPOYt9Q0.
Are you sure you want to continue connecting (yes/no)? The authenticity of host 'bastion.host' can't be established.
ECDSA key fingerprint is SHA256:vwYBZHWHDXx2ehQTEhzpD4EAcTTnu5cJWsNFPOYt9Q0.
Are you sure you want to continue connecting (yes/no)? The authenticity of host 'bastion.host' can't be established.
ECDSA key fingerprint is SHA256:vwYBZHWHDXx2ehQTEhzpD4EAcTTnu5cJWsNFPOYt9Q0.
Are you sure you want to continue connecting (yes/no)? yes
Please type 'yes' or 'no': yes
Please type 'yes' or 'no': yes
Please type 'yes' or 'no': yes
Please type 'yes' or 'no': yes
10.0.1.178 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: Host key verification failed.\r\nssh_exchange_identification: Connection closed by remote host\r\n", 
    "unreachable": true
}
10.0.3.188 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: Connection timed out during banner exchange\r\n", 
    "unreachable": true
}
10.0.2.36 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: Connection timed out during banner exchange\r\n", 
    "unreachable": true
}

但是当我将 ssh_config 复制到主目录时,~/.ssh/config它开始工作。我花了几天时间调试它,并尝试了不同的解决方案,例如

ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p [email protected] -i /etc/ansible/ssh/KEY.pem"'

在特定的主机组中。为什么我需要将 ssh 配置文件放在我的主目录中而不是使用ssh_args = -F /etc/ansible/ssh/ssh_config参数?我的 ansible 版本:

ansible 2.5.1
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.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.15rc1 (default, Apr 15 2018, 21:51:34) [GCC 7.3.0]

Ansible 已 docer 化并且我以 root 身份运行它:

FROM ubuntu:bionic-20180426

RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get install -y ansible sshpass python-pip

# install boto python module for ec2 dynamic inventory
RUN pip install boto

ENTRYPOINT /bin/bash

从我的 ansible repo 里面

docker run -it --rm -v $(pwd):/etc/ansible/ ansible:ubuntu bash

答案1

和你的情况一样。我使用 -vvvv 运行 Ansible 进行 SSH 调试,发现它正在寻找 root/etc/.ssh 中的密钥。我的错误是使用 sudo 运行。如果你的密钥位于主目录中 - 你将收到此错误。

相关内容