Ansible can’t connect with it’s own ssh config to dynamic inventory hosts

ansible

My Ansible configuration 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

when I try to execute Ansible's playbook

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
}

but when I will copy my ssh_config into home directory ~/.ssh/config it starts working. I have spent few days in debbuging it and tried different solutions like

ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p centos@bastion.host -i /etc/ansible/ssh/KEY.pem"'

in specific hosts groups. Why I need to put ssh configuration file in my home directory instead using ssh_args = -F /etc/ansible/ssh/ssh_config parameter? My ansible version:

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 is docerized and I run it as 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

from inside my ansible repo like

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

Best Answer

Had the same situation as you. I ran Ansible with -vvvv for SSH debug, and found that it was looking for a key inside root/etc/.ssh. My mistake was a running with sudo. If your key is located in home directory - you will get this error.