通过Ansible SSH进入无业游民的机器 [英] SSH into a Vagrant machine with Ansible
问题描述
通常,您可以使用vagrant ssh
ssh进入由Vagrant管理的VM.有两种选择:
Normally, you can ssh into a Vagrant-managed VM with vagrant ssh
. There are two options:
- 您可以使用由Vagrant生成的
insecure_private_key
来 认证. - 使用您自己的私钥-前提是
config.ssh.forward_agent
设置为true
,并且VM为 配置正确
- You can use an
insecure_private_key
generated by Vagrant to authenticate. - Use your own private key - provided that
config.ssh.forward_agent
is set totrue
, and the VM is configured correctly
我使用第二个选项. S当我运行vagrant ssh
时,我使用自己的自定义私钥将其SSH进入计算机.
I use the second option. S when I run vagrant ssh
, I ssh into the machine with my custom private key.
现在,我需要让Ansible SSH进入我的Vagrant计算机,并且我不想为此使用Vagrantfile
.
Now I need to let Ansible SSH into my Vagrant machine and I do not want to use Vagrantfile
for it.
所以我执行了:
ansible-playbook -i hosts/development --private-key=~/.ssh/id_rsa -u vagrant dev.yml
我返回了此错误:
致命:[192.168.50.5] => SSH错误:权限被拒绝(公钥). 而连接到192.168.50.5:22
fatal: [192.168.50.5] => SSH Error: Permission denied (publickey). while connecting to 192.168.50.5:22
hosts/inventory
文件仅保存我的Vagrant VM的IP(192.168.50.5).
The hosts/inventory
file holds just the IP of my Vagrant VM (192.168.50.5).
我不知道为什么Ansible无法ssh进入VM.它使用与执行vagrant ssh
时完全相同的用户(vagrant
)和键(id_rsa
).
I do not know why Ansible cannot ssh into the VM. It's using exactly the same user (vagrant
) and key (id_rsa
) as when executing vagrant ssh
.
但是,使用vagrant ssh
进行sshing不会出现问题,而上述操作将无法运行.
However, there is no problem sshing with vagrant ssh
while the above would not run.
任何建议将不胜感激.
推荐答案
问题可能出在您的hosts/inventory
文件中.您需要在其中添加Ansible的正确连接配置,然后保存并重新运行.
The problem probably lies within your hosts/inventory
file. You need to add the proper connection configuration for Ansible therein, save and re-run.
192.168.50.5 ansible_ssh_port=22 ansible_ssh_user=vagrant ansible_ssh_private_key_file=~/.ssh/id_rsa
如果不使用端口22
,请在主机文件中相应调整ansible_ssh_port
.
If you are not using port 22
, adjust the ansible_ssh_port
in your hosts file accordingly.
您还可能没有在Vagrant中设置您的公钥,因此这也将不起作用.要对此进行测试,请运行:
It is also a possibility that you have not setup your pubkey in Vagrant, hence this would also not work. To test this, run:
vagrant ssh-config | grep IdentityFile
# result should be your private key and not
# .vagrant/machines/default/virtualbox/private_key
如果您还没有将pubkey
放入Vagrant虚拟机中,则需要先添加它,然后才能尝试使用私钥.
If you have not put your pubkey
in the Vagrant vm, you will need to add that before you can try your private key.
参考: http://docs.ansible.com/ansible/intro_inventory .html#list-of-behavioral-inventory-parameters
参考: https://docs.vagrantup.com/v2/cli/ssh_config.html
这篇关于通过Ansible SSH进入无业游民的机器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!