Permission is denied while connecting to server with ansible via ssh

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.

Any help is appreciated.

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

I am guessing you used a password instead of ssh-key. So at the end of your command, add


Let’s say you’re running your playbook. Your command will become:

ansible-playbook playbook.yml --ask-pass
  • From the Ansible master run ssh to the remote target and see if that works:
ssh <user>@<target>

If this works ssh is enabled, move on to the next step.

  • Try the following from the Ansible master:
ansible-playbook -i inventory.yaml --private-key=/home/<user_id>/.ssh/id_rsa <playbook>.yaml -u <user_id>

If this fails with the following error, steps listed after the error should fix:

  TASK [Gathering Facts] *********************************************************
  fatal: []: UNREACHABLE! => {"changed": false, "msg": "Authentication failed.", "unreachable": true}
 	to retry, use: --limit 
  PLAY RECAP *********************************************************************   : ok=0    changed=0    unreachable=1    failed=0   
  • On the remote host, move/rename the ~/.ssh/authorized_keys file.

  • Then from the ansible master run the following:

ssh-copy-id -i ~/.ssh/ <user-id>@<x-host>

// ssh-copy-id — use locally available keys to authorise logins on remote machine

  • This creates an authorized_keys file in the remote server ~/.ssh folder for the <user-id> user.

  • Now run the ansible playbook and should work.

This error usually occurs when their is no valid public key set for the target server.

First check whether you have valid public and private key generated for the user which you are using. Keep public key in target server and private key in control server Get the path for private key and configure it in ansible.cfg file(check ansible docs for this)