7

I'm trying to run my Ansible playbook on a remote server using a provided ssh key.

I have added the following configuration to my inventory file:

all:
  hosts:
    server1:
      ansible_host: [email protected]
      dest_dir: /root
      sample_tree: sample_tree.txt
      private_key_file: ../config/id_rsa_tf

I have referenced it in my playbook using the following:

- name: "Nightly Deploy"
  hosts: server1
  remote_user: sysuser
  tasks:
    - name: Copy test from local to remote
      tags:
        - copy
        - all
      copy:
        src: "test.tgz"
        dest: "{{ dest_dir }}/test.tgz"

I am running the playbook with the following command:

ansible-playbook --tags="copy" -v -i inventories/nightly-build.yaml playbooks/nightly-build.yaml

The error I'm getting is the following:

fatal: [server1]: UNREACHABLE! => {"changed": false, "msg": "Failed to
connect to the host via ssh: Permission denied (publickey,gssapi- keyex,gssapi-with-mic,password).", "unreachable": true}

Is my private_key_file wrong in my inventory file or am I calling it wrong? and help would be great

4
  • 1
    might be a user issue as well. Are you passing the correct user, you can try passing it using -u. Commented Sep 13, 2019 at 7:56
  • Do I have to pass the user at run time, if I am specifying a remote_user in the playbook? Commented Sep 13, 2019 at 8:00
  • 1
    See ansibledaily.com/troubleshooting-ansible-connection-issues Commented Sep 13, 2019 at 8:11
  • Both comments really helped thanks. turns out, my issue was caused by not passing the host and passing the wrong var name in my inventory, it should be ansible_ssh_private_key_file Commented Sep 13, 2019 at 8:54

1 Answer 1

17

This error usually occurs when there is no valid public and private key generated and setup.

Try any of the following approaches:

  1. Create/edit your ansible.cfg file in your playbook directory and add a line for the full path of your private key to set it globally for all hosts in your playbook.
[defaults]
private_key_file = /Users/username/.ssh/private_key
  1. Add the private key to your playbook using the following line:
vars:
  ansible_ssh_private_key_file: "/home/ansible/.ssh/id_rsa"
  1. You can also define the private key to use directly in command line:
ansible-playbook -vvvv --private-key=/Users/you/.ssh/your_key playbookname.yml
Sign up to request clarification or add additional context in comments.

1 Comment

in the [defaults] section privatekeyfile should be change to private_key_file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.