Ssh – Can’t ping vagrant from ansible by ad-hoc command

ansiblesshvagrant

I am using vagrant now.

I can ping virtual machine from local terminal.

I want to use ansible to ping that vagrant virtual machine:

$ ansible 192.168.33.101 -m ping -u root

But got error:

192.168.33.101 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).\r\n",
    "unreachable": true
}

I added that IP in /etc/ansible/hosts file:

192.168.33.101

Both local and virtual machine have ssh installed. So why can't reach?

Best Answer

You need to configure your ssh connection. The Ansible ping module naming is confusing. The ping module actually builds up an ssh connection to verify the ability to login and that a usable python is configured.

So like the ping command can tell you that the network connection to a remote host is working the ping module tells you that Ansible is capable of running commands on the remote host.

Look at the official documentation how to setup ssh connection to your Vagrant box.

Basically you will need something like this:

ansible-playbook --private-key=~/.vagrant.d/insecure_private_key -u vagrant -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory playbook.yml

For ad-hoc commands something like this:

ansible --private-key=~/.vagrant.d/insecure_private_key -u vagrant ...