Normally, you can ssh into a Vagrant-managed VM with vagrant ssh
. There are two options:
- 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
I use the second option. S when I run vagrant ssh
, I ssh into the machine with my custom private key.
Now I need to let Ansible SSH into my Vagrant machine and I do not want to use Vagrantfile
for it.
So I executed:
ansible-playbook -i hosts/development --private-key=~/.ssh/id_rsa -u vagrant dev.yml
And I have this error returned:
fatal: [192.168.50.5] => SSH Error: Permission denied (publickey). while connecting to 192.168.50.5:22
The hosts/inventory
file holds just the IP of my Vagrant VM (192.168.50.5).
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
.
However, there is no problem sshing with vagrant ssh
while the above would not run.
Any suggestions would be much appreciated.
The problem probably lies within your
hosts/inventory
file. You need to add the proper connection configuration for Ansible therein, save and re-run.If you are not using port
22
, adjust theansible_ssh_port
in your hosts file accordingly.It is also a possibility that you have not setup your pubkey in Vagrant, hence this would also not work. To test this, run:
If you have not put your
pubkey
in the Vagrant vm, you will need to add that before you can try your private key.Reference: http://docs.ansible.com/ansible/intro_inventory.html#list-of-behavioral-inventory-parameters
Reference: https://docs.vagrantup.com/v2/cli/ssh_config.html