Tanay Suthar
Tanay Suthar

Reputation: 453

Failed to connect to host via SSH on Vagrant with Ansible Playbook

I was not able to find where the actual problem is. I executed below playbook with my private key:

---
- hosts: localhost
  gather_facts: false
  sudo: yes
  tasks:
    - name: Install package libpcre3-dev
      apt: name=libpcre3-dev state=latest

But I am getting the error below on Vagrant Ubuntu machine:

PLAY [localhost]   
*********************************************************************

TASK [Install package ] 
***************************************************
fatal: [vagrant]: UNREACHABLE! => {"changed": false, "msg": "Failed to
connect to the host via ssh: Permission denied (publickey,password).\r\n",
"unreachable": true}
        to retry, use: --limit @/home/vagrant/playbooks/p1.retry

PLAY RECAP
*********************************************************************
vagrant                    : ok=0    changed=0    unreachable=1    failed=0

What could be the possible suggestion?

Upvotes: 6

Views: 3778

Answers (1)

techraf
techraf

Reputation: 68439

You are running a playbook against a localhost with SSH connection (default in Ansible) and this fails. Most likely because you never configured the account on your machine to accept the key from itself. Using defaults, you'd need to add the ~/.ssh/id_rsa.pub to ~/.ssh/authorized_keys.

Instead, to run on locally add connection: local to the play:

---
- hosts: localhost
  connection: local
  tasks:
    - debug:

And it will give you a proper response:

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": "Hello world!"
}

Upvotes: 6

Related Questions