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.