title: How to add an existing public key to authorized_keys file using Ansible and user module?
body: <p>I'm programming a simple task with Ansible to create a user and add an existing RSA public key. This is the code I wrote:</p>
  
  <pre class="lang-yml prettyprint-override"><code>- name: SYSTEM - Create test user
    tags: system-user
    user: 
          name: &quot;{{ test_user }}&quot;
          state: present
          createhome: yes
  
  - name: SYSTEM - Add existing pub key for test user
    tags: system-user
    copy: 
         content: &quot;{{ test_user_pubkey }}&quot;
         dest: &quot;/tmp/test_user_id_rsa.pub&quot;
         force: no
         owner: &quot;{{ test_user }}&quot;
         group: &quot;{{ test_user }}&quot;
         mode: 0600
  
  - name: SYSTEM - Set authorized key for test_user took from file
    tags: system-user
    authorized_key:
          user: &quot;{{ test_user }}&quot;
          state: present
          key: &quot;{{ lookup('file', '/tmp/test_user_id_rsa.pub') }}&quot;
  </code></pre>
  <p>The code I wrote is not elegant and I think that the best option will be to add the existing RSA public key with the user creation block in order to create and filled up the <code>authorized_keys</code> file.</p>
  <p>I've read the <a href="http://docs.ansible.com/ansible/latest/user_module.html" rel="nofollow noreferrer">Ansible user module</a> but ssh_key_file method does not include the possibility to echo the value of an existing pub key to the <code>authorized_keys</code> file (the end purpose is to be able to remote connect with ssh using the user and the private key).</p>
  <blockquote>
  <p>ssh_key_file = Optionally specify the SSH key filename. If this is a
  relative filename then it will be relative to the user's home
  directory.</p>
  </blockquote>
  <p>Is it possible with Ansible to manage this process within the user module?</p>