Ansible: Failed to connect to the host via ssh

在mac本机设置ansible环境,执行如下指令

ansible-playbook --connection=ssh --timeout=30 --extra-vars=ansible_user\=\'root\' --limit="all" --inventory-file=deploy/profiles/ha/hosts --extra-vars=\{\"docker_cleanup_legacy_resources\":true,\"cloudmgr_image_version\":\"2.2.0-SNAPSHOT-20190624.142052\",\"cloudmgr_deploy_profile\":\"ha\"\} --become -v deploy/deploy-demo.yml

报错日志:

TASK [Gathering Facts] ********************************************************************************************************************

fatal: [192.168.108.27]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: [email protected]: Permission denied (publickey).", "unreachable": true}

fatal: [192.168.108.31]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: [email protected]: Permission denied (publickey).", "unreachable": true}

fatal: [192.168.108.30]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: [email protected]: Permission denied (publickey).", "unreachable": true}

fatal: [192.168.108.32]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: [email protected]: Permission denied (publickey).", "unreachable": true}

fatal: [192.168.108.33]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: [email protected]: Permission denied (publickey).", "unreachable": true}

原因:默认使用当前用户home目录.ssh/id_rsa,而非代码目录下的deploy/keys/id_rsa

解决历程:

1. ansible-playbook 打印执行日志,找到出错指令,加-vvv查看详细执行过程

ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="root"' -o ConnectTimeout=30 -o ControlPath=/Users/qiaoxiaolin/.ansible/cp/aa117ff50a 192.168.108.31 '/bin/sh -c '"'"'echo ~root && sleep 0'"'"''

2. 在本地环境和远程主机环境比对执行出错指令,远程主机执行无问题

3. 确认私钥问题,在本地复制一份可用私钥,执行指令仍报错

4. 单独使用私钥ssh [email protected] -i ~/.ssh/id_rsa 可连接,确认私钥正确

5. 怀疑公钥问题。删除公钥~/.ssh/id_rsa.pub,测试指令可连接。

你可能感兴趣的:(Ansible: Failed to connect to the host via ssh)