title: Using authorized_key module in a playbook to set up SSH key for new users
body: <p>Scenario:</p>
  
  <p>Based on the [clients] section of the <code>hosts</code> file do the following:</p>
  
  <ol>
  <li>Check if the SSH login of user "foo" fails and if yes</li>
  <li>Add SSH keys for user "foo" using authorized_key module</li>
  <li>Assuming that user "foo" already exists on remote machine and SSH public key has already been created on the local (ansible) host</li>
  </ol>
  
  <p>I am aware of <a href="https://stackoverflow.com/questions/26653944/authorized-key-module-for-initial-connection">this solution</a> using Ansible command line but I would like to be able to put this into a playbook. It's acceptable to make script interactive with user typing in password including sudo.</p>
  
  <p>Right now I figured out how to do what I want using 3-rd party role <code>GROG.authorized-key</code> but it still requires me to run playbook with -K switch. Is there something in Ansible (beside command line switches) that would only prompt for the password if it is needed?</p>
  
  <pre><code>- hosts: clients
    vars:
      authorized_key_list:
        - name: pdo
          authorized_keys:
           - key: "{{ lookup('file', '/home/pdo/.ssh/id_rsa.pub') }}"
             state: present
    roles:
      - { role: GROG.authorized-key }
  </code></pre>