title: Ansible authorized key module unable to read public key
body: <p>I'm trying to use ansible (version 2.1.2.0) to create named ssh access across our network of servers.  Running ansible from a jump box I'm creating a set of users and creating a private/public key pair with the users module</p>
  
  <pre><code>  - user:
        name: "{{ item }}"
        shell: /bin/bash
        group: usergroup
        generate_ssh_key: yes
        ssh_key_comment: "ansible-generated for {{ item }}"
      with_items: "{{ list_of_usernames }}"
  </code></pre>
  
  <p>From there I would like to copy across the public key to the authorized_users file on each remote server.  I'm using the <a href="http://docs.ansible.com/ansible/authorized_key_module.html" rel="nofollow noreferrer">authorized_key_module</a> to fetch and copy the user's generated public key as the authorized_key on the remote servers:</p>
  
  <pre><code>  - authorized_key:
        user: "{{ item }}"
        state: present
        key: "{{ lookup('file', '/home/{{ item }}/.ssh/id_rsa.pub') }}"
      with_items:
        - "{{ list_of_usernames }}"
  </code></pre>
  
  <p>What I'm finding is that the lookup will fail as it is unable to access the pub file within the user's .ssh folder.  It will run happily however if I run ansible as the root user (which for me is not an option I'm willing to take).</p>
  
  <pre><code>fatal: [&lt;ipAddress&gt;]: FAILED! =&gt; {"failed": true, "msg": "{{ lookup('file', '/home/testuser/.ssh/id_rsa.pub') }}: the file_name '/home/testuser/.ssh/id_rsa.pub' does not exist, or is not readable"}
  </code></pre>
  
  <p>Is there a better way to do this other than running ansible as root?</p>
  
  <p>Edit: Sorry I had forgotten to mention that I'm running with become: yes</p>
  
  <p>Edit #2:  Below is a condensed playbook of what I'm trying to run, of course in this instance it'll add the authorized_key to the localhost but shows the error I'm facing:</p>
  
  <pre><code>---
  - hosts: all
    become: yes
    vars:
      active_users: ['user1','user2']
    tasks:
  
    - group:
        name: "users"
  
    - user:
        name: "{{ item }}"
        shell: /bin/bash
        group: users
        generate_ssh_key: yes
      with_items: "{{ active_users }}"
  
    - authorized_key:
        user: "{{ item }}"
        state: present
        key: "{{ lookup('file', '/home/{{ item }}/.ssh/id_rsa.pub') }}"
      become: yes
      become_user: "{{ item }}"
      with_items:
        - "{{ active_users }}"
  </code></pre>