使用 Ansible 通过 SSH 连接到 Vagrant 机器 [英] SSH into a Vagrant machine with Ansible

查看:32
本文介绍了使用 Ansible 通过 SSH 连接到 Vagrant 机器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,您可以使用 vagrant ssh SSH 到 Vagrant 管理的 VM.有两种选择:

Normally, you can ssh into a Vagrant-managed VM with vagrant ssh. There are two options:

  1. 您可以使用 Vagrant 生成的 insecure_private_key认证.
  2. 使用您自己的私钥 - 前提是config.ssh.forward_agent 设置为 true,VM 为正确配置
  1. You can use an insecure_private_key generated by Vagrant to authenticate.
  2. Use your own private key - provided that config.ssh.forward_agent is set to true, 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 (192.168.50.5) 的 IP.

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 ssh 是没有问题的,而上面的则无法运行.

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 vm 中,你需要先添加它,然后才能尝试你的私钥.

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 连接到 Vagrant 机器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆