Score:1

Permission denied (publickey) error when managing GCP machine via Ansible (running on GCP VM) despite successful SSH connection

gu flag

I deployed two machines to GCP via Terraform. Let's call them control-host and target-host. I want to manage the target-host via Ansible installed on the control-host. Unfortunately, I keep getting the following error no matter what I do:

10.128.100.3 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: [email protected]: Permission denied (publickey).",
    "unreachable": true
}

The problem perplexes me because it is possible to ssh from control-host to target-host without any problem. I would assume that if "raw" ssh is possible, so is using it via Ansible.

Here's the list of things I did and tried:

  1. Install Ansible on the control-host.
  2. Generate a key pair on the control host. Copy the public key.
  3. SSH on the target host. Open ./.ssh/authorized_keys. Paste the public key.
  4. Execute ansible all -vvv -m ping. I got the error mentioned above.
  5. Open /etc/ansible/ansible.cfg. Add the following:
[defaults]
remote_user = gcp_user
host_key_checking = False
ansible_ssh_common_args='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'
private_key_file = /home/gcp_user/.ssh/t_k

(t_k is the name of the key)

  1. Run ansible all -vvv -m ping again. Same error.
  2. Run ansible all -vvv -m ping --key-file=/home/gcp_user/.ssh/t_k. Same error.
  3. Open /etc/ansible/hosts. Add the following:
10.128.100.3 ansible_ssh_private_key_file=/home/gcp_user/.ssh/t_k

Same story.

SSH logs on the server side say the following: Connection closed by authenticating user gcp_user 10.128.100.2 port 34470 [preauth]

I followed the recommendations from the following threads and none of them helped:

  1. https://stackoverflow.com/questions/64681944/create-and-setup-gcp-vms-with-ansible-ssh-permission-denied-publickey
  2. https://stackoverflow.com/questions/55897136/ansible-failed-to-connect-to-the-host-via-ssh-permission-denied-publickey
  3. https://stackoverflow.com/questions/57424995/ansible-remote-user-root-ssh-permission-denied-publickey
  4. https://stackoverflow.com/questions/33280244/ssh-error-permission-denied-publickey-password-in-ansible
ec flag
**Welcome to the Ask Ubuntu community.** Please edit your question to include the version of the Ubuntu OS that you're using (on GCP). This will make it easier to diagnose and resolve your issue. Thanks
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.