Permission is denied while connecting to server with ansible via ssh

November 3, 2017 4.7k views
Ansible
demiculus
By:
demiculus

I'm getting the error below when I'm writing this

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

I have gone through all the steps in this tutorial and now I'm stuck at step 3. https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-ansible-on-ubuntu-16-04

Any help is appreciated.

4 Answers

Hi there! What happens if you try to connect to your server via SSH?

ssh root@your_server_ip
  • It connects

    Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-98-generic x86_64)
    
     * Documentation:  https://help.ubuntu.com
     * Management:     https://landscape.canonical.com
     * Support:        https://ubuntu.com/advantage
    
      Get cloud support with Ubuntu Advantage Cloud Guest:
        http://www.ubuntu.com/business/services/cloud
    
    25 packages can be updated.
    0 updates are security updates.
    
    
    Last login: Sat Nov  4 06:10:48 2017 from 87.92.112.123
    

Did you work through the prerequisite tutorial listed at the beginning of the Ansible installation article?
Specifically step 4.

This one: Initial Server Setup with Ubuntu 16.04

It looks like you don't have your user's public SSH key present on the remote droplet that Ansible uses to authenticate? I'm not sure however.

When you start a new server, there are a few steps that you should take every time to add some basic security and give you a solid foundation. In this guide, we'll walk you through the basic steps necessary to hit the ground running with Ubuntu 16.04.

Since you can connect directly, your ansible is defaulting to a different key. Create/edit your ansible.cfg file in your playbook directory and add a line for the location of your key:

[defaults]
privatekeyfile = /Users/username/.ssh/private_key

This could happen even if you have made sure the passwordless ssh between System A and System B (say using either ssh-copy-id command or by manually copying the public key i.e content of the idrsa.pub file on System A to .ssh/authorizedkeys file on System B. If this is happening, one of the reason could be the user home directories.

On System A the user home directory is say /home/tester and on System B, it is /users/tester, then passwordless ssh might not work. Make sure both users have same home directory solves this issue. I observed this case in CentOS machines and on making sure the home directories for users same, the issue resolved.

Have another answer? Share your knowledge.