2
votes

I am trying to copy an SSH public key to a newly created VM:

- hosts: vm1
  remote_user: root
  tasks:
    - name: deploy ssh key to account
      authorized_key: user='root' key="{{lookup('file','/root/.ssh/id_rsa.pub')}}"

But getting error:

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

So to establish SSH I need first to establish SSH?

How can I establish SSH for newly created KVM automatically, without manual key copy.

(host_key_checking = False in ancible.cfg)

2

2 Answers

5
votes

Assuming the target machine allows root-login with password (from the error message it seems it does), you must provide the credentials to your playbook:

ansible-playbook playbook.yml --extra-vars "ansible_ssh_user=root ansible_ssh_pass=password"
4
votes

Something I tried (and it worked) when I had this same issue:

ansible target-server-name -m command -a "whatever command" -k

The -k prompts you for the ssh password to the target server.

Add below changes to the /etc/ansible/hosts file:

[target-server-name]
target_server_ip

Example:

ansible target-server-name -m ping -k