SSH into a vagrant machine with Ansible

Typically, you can use ssh in a managed Vagrant VM using vagrant ssh . There are two options:

  • You can use the insecure_private_key generated by Vagrant for authentication.
  • Use your own private key - provided that config.ssh.forward_agent set to true and the VM is configured correctly

I am using the second option. S, when I run vagrant ssh , I ssh to the machine using my personal private key.

Now I need to provide Ansible SSH to my vagrant machine, and I don't want to use Vagrantfile for it.

So, I did:

ansible-playbook -i hosts/development --private-key=~/.ssh/id_rsa -u vagrant dev.yml

And I have this error:

fatal: [192.168.50.5] => SSH error: permission denied (publicickey). when connected to 192.168.50.5:22

The hosts/inventory contains only the IP of my Vagrant VM (192.168.50.5).

I do not know why Ansible cannot ssh in VM. It uses the exact same user ( vagrant ) and the key ( id_rsa ) as when executing vagrant ssh .

However, there is no problem with sshing with vagrant ssh , while the above will not work.

Any suggestions would be highly appreciated.

+7
vagrant ssh ansible
source share
1 answer

The problem probably lies in your hosts/inventory . You need to add the correct connection configuration for Ansible in it, save and restart.

 192.168.50.5 ansible_ssh_port=22 ansible_ssh_user=vagrant ansible_ssh_private_key_file=~/.ssh/id_rsa 

If you are not using port 22 , edit ansible_ssh_port in the hosts file accordingly.

It is also possible that you did not configure your panel in Vagrant, so this did not work either. To verify this, run:

 vagrant ssh-config | grep IdentityFile # result should be your private key and not # .vagrant/machines/default/virtualbox/private_key 

If you do not put your pubkey in Vagrant vm, you need to add this before you can try your private key.

Link: http://docs.ansible.com/ansible/intro_inventory.html#list-of-behavioral-inventory-parameters

Link: https://docs.vagrantup.com/v2/cli/ssh_config.html

+13
source share

All Articles