I’m trying to use the ansible_authorized keys to create VMs (with packer and terraform), adding my workspace key to VMs authorized keys.
I think it worked because if I execute ''ssh root@myVM", it connects without asking password.
But when I execute “ansible all -m ping -u root” to that same host, it fails with the error “sshh fails to connect to host via ssh. Permission denied”.
I’m really confused and struggling to understand that, since a raw ssh works and the ansible ssh doesn’t.
ssh-keygen creates the public and private keys. ssh-copy-id copies the local-host’s public key to the remote-host’s authorized_keys file. ssh-copy-id also assigns proper permission to the remote-host’s home, ~/.ssh, and ~/.ssh/authorized_keys.
Check host entry in /etc/host file
127.0.0.1 localhost
I manage to solve the problem. I need to pass the public key of a different user. In addition, I changed the way to connect to the VMs, since I am using openstack, I configured ansible to use the keypair to connect.
I see you found a solution, but this is what I'd recommend doing next time.
Add -vvv and read the resulting output carefully. You can splice the ssh command from Ansible back into the shell to work out what is missing or different to just running ssh@<foo> locally.
Usually this is because the username is different or you’re using a different ssh key than expected.
# ssh_config
Host *.i09.com www api beta couchdb cache rabbit vault
UseRoaming no
GSSAPIAuthentication no
KbdInteractiveAuthentication no
ServerAliveInterval 240
ControlMaster auto
ControlPath ~/.ssh/%r@%h:%p
ControlPersist 30m
KexAlgorithms curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256
SendEnv VAULT_TOKEN
port 2200
user ansible
ForwardAgent yes
Hi Fabio,
can you please clarify how you managed to solve it. I created user ansible, the public keys ID_RSA.pub are stored under /home/ansible/.ssh folders. the ssh-copy-id should copy to target server - client when I run
ssh-copy-id ansible@privateip.
I create same user name ‘ansible’ on remote server.
I get permission denied error.
It was pretty simple 'cause I was messing up the users.
Are you running your playbook with root? Which user you set on your hosts as ansible_user? Are you using become inside your playbooks?
Also, I stopped using ssh, instead I’m using openstack keypair.
And try Dave’s suggestion, it’s very useful:
Hi Fabio,
I see you found a solution, but this is what I’d recommend doing next time.
Add -vvv and read the resulting output carefully. You can splice the ssh command from Ansible back into the shell to work out what is missing or different to just running ssh@ locally.
Usually this is because the username is different or you’re using a different ssh key than expected.
Hi Fabio,
I am not using the playbook for this particular task, I am creating an ansible control server and client. I am trying to connect from control server to client after generating the ssh keys.
I used ssh-keygen on ubuntu server using ansible user. I am setting a passwordless connection from the ansible control server /localhost to client.