使用Ansible连接Vagrant虚拟机的ssh失败

3
我是Ansible的新手。我使用Vagrant设置了一个Ubuntu虚拟机。我可以通过ssh vagrant@172.16.23.228命令登录到虚拟机。我已经创建了一个具有与vm相同密码的ssh密钥,并将其添加到代理中,并在我的hosts文件中指定了路径。
按照这里的说明后,当运行此命令(ansible all --inventory-file=hosts.ini --module-name ping -u vagrant -vvvv)时,我开始收到以下错误:
不确定我缺少什么配置,还需要检查什么?
<172.16.23.228> ESTABLISH CONNECTION FOR USER: vagrant
<172.16.23.228> REMOTE_MODULE ping
<172.16.23.228> EXEC ssh -C -tt -vvv -o ControlMaster=auto -o     ControlPersist=60s -o ControlPath="/Users/user/.ansible/cp/ansible-ssh-%h-%p-%r" - o Port=22 -o IdentityFile="~Users/user/.ssh/onemachine_rsa" -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=vagrant -o ConnectTimeout=10 172.16.23.228 /bin/sh -c 'mkdir -p     $HOME/.ansible/tmp/ansible-tmp-1451080871.59-247915080664557 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1451080871.59-247915080664557 && echo $HOME/.ansible/tmp/ansible-tmp-1451080871.59-247915080664557'
172.16.23.228 | FAILED => SSH Error: tilde_expand_filename: No such user Users
while connecting to 172.16.23.228:22
It is sometimes useful to re-run the command using -vvvv, which prints SSH  debug output to help diagnose the issue.

My hosts file looks like: 
[testserver]
172.16.23.228 ansible_ssh_port=22 ansible_ssh_user=vagrant    ansible_ssh_private_key_file=~Users/user/.ssh/onemachine_rsa
3个回答

2

你现在的做法可以行得通,但我强烈建议你使用Vagrant内置的Ansible provisioner。这不仅会让你的生活更轻松,同时还会提高你的Vagrant技能。如果需要执行任何shell脚本,请使用shell provisioner


1
为了方便像我这样的人后来者,提供此答案。最新的Vagrant安装将私钥安装在本地目录中,而不是为每个虚拟机使用明显不安全的私钥。您需要创建一个类似于这个的ansible_hosts文件:
[vagrantboxes]
jessie ansible_ssh_port=2222 ansible_ssh_host=127.0.0.1

[vagrantboxes:vars]
ansible_ssh_user=vagrant
ansible_ssh_private_key_file=.vagrant/machines/default/virtualbox/private_key

关键在于最后一行,提供了一个路径,该路径指向实际私钥,该私钥用于从此特定目录启动的虚拟机中。


1
谢谢!你帮我省了很多时间,现在我会回去阅读更多关于vars的ansible文档。我把所有变量都移到[bagrantboxes:vars]下面,这样所有与vagrant有关的东西都在一个地方。 再次感谢! - Tudor

0

ansible_ssh_private_key_file 的路径不正确。请尝试使用 ansible_ssh_private_key_file=~/.ssh/onemachine_rsa。在这种情况下,波浪号会扩展到您在运行 ansible 的本地机器上的用户的主目录。


1
嗨,@Dave,我无法使用SSH密钥使其工作。我唯一能够让它工作的方法是使用vagrant密钥文件。/Users/me/Documents/Vagrant/one/.vagrant/machines/default/vmware_fusion/private_key - Jimi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接