0

I need to spin up an ec2-instance from local machine and copy the public key of local machine to ec2-instance while the ec2-instance spuns up.

Edit

As suggested in the comments I have tried to use the authorized_keys module

- name: Deploy SSH Key
  authorized_key:
    user: ec2-user
    state: present
    key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
    path: /home/ec2-user/.ssh/authorized_keys

But I am getting the following error

Error: "msg": "Failed to lookup user ec2-user: 'getpwnam(): name not found: ec2-user'"}

Edit: @anyone, My ansible playbook spuns up ec2-instance and i need to copy the public key to this instance, the default user is ec2-user, so what will I do to resolve this. The authorized_keys module copies ssh keys from local instance and pastes in the home/ec2-user/.ssh/authorized_keys, location of local instance itself. it doesn't copy the public key to the newly created instance.

Ben
  • 1
  • 4
  • Did you try with the [`authorized_key` module](https://docs.ansible.com/ansible/latest/modules/authorized_key_module.html)? – Zeitounator Nov 27 '19 at 07:13
  • 1
    Please do not put follow-ups/ additional info in comments, edit your question, I did it for you this time but think about in the future. Your error seems to indicate that the `ec2-user` does not exist on your target server. – Zeitounator Nov 27 '19 at 09:36

0 Answers0