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:
- Install Ansible on the control-host.
- Generate a key pair on the control host. Copy the public key.
- SSH on the target host. Open
./.ssh/authorized_keys
. Paste the public key.
- Execute
ansible all -vvv -m ping
. I got the error mentioned above.
- 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)
- Run
ansible all -vvv -m ping
again. Same error.
- Run
ansible all -vvv -m ping --key-file=/home/gcp_user/.ssh/t_k
. Same error.
- 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:
- https://stackoverflow.com/questions/64681944/create-and-setup-gcp-vms-with-ansible-ssh-permission-denied-publickey
- https://stackoverflow.com/questions/55897136/ansible-failed-to-connect-to-the-host-via-ssh-permission-denied-publickey
- https://stackoverflow.com/questions/57424995/ansible-remote-user-root-ssh-permission-denied-publickey
- https://stackoverflow.com/questions/33280244/ssh-error-permission-denied-publickey-password-in-ansible