Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 644
Tanay Suthar Avatar asked Dec 29 '16 10:12

Tanay Suthar


1 Answers

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!"
}
like image 175
techraf Avatar answered Nov 16 '22 10:11

techraf