1

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
  • **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 – richbl May 22 '23 at 01:29

0 Answers0