错误:无法通过ssh连接到主机

时间:2016-07-15 00:13:12

标签: vagrant ansible

我正在努力学习ansible,并且正在关注o'riley Ansible Up和正在运行的书。

在本书的入门部分,它要求我安装ansible,virtualbox和vagrant,然后通过CLI运行:

vagrant init ubuntu/trusty64

vagrant up

之后我可以通过vagrant ssh或通过:

进入VM
ssh vagrant@127.0.0.1 -p 2222 -i /Users/XXX/playbooks/.vagrant/machines/default/virtualbox/private_key

接下来是创建如下所示的hosts文件:

testserver ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 \ ansible_ssh_user=vagrant \ ansible_ssh_private_key_file=.vagrant/machines/default/virtualbox/private_key

最后运行此命令:

ansible testserver -i hosts -m ping

哪个让我:

testserver | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh.",
    "unreachable": true
}

添加-vvv让我:

No config file found; using defaults
<127.0.0.1> ESTABLISH SSH CONNECTION FOR USER: None
<127.0.0.1> SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o Port=2222 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/Users/XXX/.ansible/cp/ansible-ssh-%h-%p-%r 127.0.0.1 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1468541275.7-255802522359895 `" && echo ansible-tmp-1468541275.7-255802522359895="` echo $HOME/.ansible/tmp/ansible-tmp-1468541275.7-255802522359895 `" ) && sleep 0'"'"''
testserver | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh.",
    "unreachable": true
}

我尝试修改hosts文件中的ansible_ssh_private_key_file以指向私钥的完整路径,但仍然无效:

ansible testserver -i hosts -m ping -vvv
No config file found; using defaults
<127.0.0.1> ESTABLISH SSH CONNECTION FOR USER: None
<127.0.0.1> SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o Port=2222 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/Users/XXX/.ansible/cp/ansible-ssh-%h-%p-%r 127.0.0.1 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1468541370.61-137685863794569 `" && echo ansible-tmp-1468541370.61-137685863794569="` echo $HOME/.ansible/tmp/ansible-tmp-1468541370.61-137685863794569 `" ) && sleep 0'"'"''
testserver | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh.",
    "unreachable": true
}

这是我的Ansible版本:

ansible --version ansible 2.1.0.0   配置文件=   已配置的模块搜索路径=默认无覆盖

任何人都有任何想法为什么ansible没有连接到我的流浪汉?

1 个答案:

答案 0 :(得分:1)

我没有看到任何库存变量超过第一个在ssh命令中生效的库存变量。您的库存文件是否真的像这样?

testserver ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 \ ansible_ssh_user=vagrant \ ansible_ssh_private_key_file=.vagrant/machines/default/virtualbox/private_key

你不应该有反斜杠。直接重新格式化

testserver ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222  ansible_ssh_user=vagrant  ansible_ssh_private_key_file=.vagrant/machines/default/virtualbox/private_key

但是,从长远来看,你可能想要split these out into separate host_vars files