title: Is it possible to copy public key of a local instance to ec2-instance authorised key section, while ec2-instance spuns up, using playbook
body: <p>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.</p>
  
  <p>Edit</p>
  
  <p>As suggested in the comments I have tried to use the <a href="https://docs.ansible.com/ansible/latest/modules/authorized_key_module.html" rel="nofollow noreferrer"><code>authorized_keys</code> module</a></p>
  
  <pre><code>- 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
  </code></pre>
  
  <p>But I am getting the following error</p>
  
  <pre><code>Error: "msg": "Failed to lookup user ec2-user: 'getpwnam(): name not found: ec2-user'"}
  </code></pre>
  
  <p>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.</p>