title: Can Ansible deploy public SSH key asking password only once?
body: <p>I wonder how to copy my SSH public key to many hosts using Ansible.</p>
  
  <p>First attempt:</p>
  
  <pre><code>ansible all -i inventory -m local_action -a "ssh-copy-id {{ inventory_hostname }}" --ask-pass
  </code></pre>
  
  <p>But I have the error <strong><code>The module local_action was not found in configured module paths</code></strong>.</p>
  
  <p>Second attempt using a playbook:</p>
  
  <pre><code>- hosts: all
    become: no
    tasks:
    - local_action: command ssh-copy-id {{ inventory_hostname }}
  </code></pre>
  
  <p>Finally I have entered my password for each managed host:</p>
  
  <pre><code>ansible all -i inventory --list-hosts | while read h ; do ssh-copy-id "$h" ; done
  </code></pre>
  
  <p><strong>How to fill password only once while deploying public SSH key to many hosts?</strong></p>
  
  <hr>
  
  <hr>
  
  <p><strong>EDIT:</strong> &nbsp; I have succeeded to copy my SSH public key to multiple remote hosts using the following playbook from the <a href="https://stackoverflow.com/a/44708604/938111">Konstantin Suvorov's answer</a>.</p>
  
  <pre><code>- hosts: all
    tasks:
    - authorized_key:
        key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
  </code></pre>
  
  <p>The field <code>user</code> should be mandatory according to the <a href="http://docs.ansible.com/ansible/authorized_key_module.html" rel="noreferrer">documentation</a> but it seems to work without. Therefore the above generic playbook may be used for any user when used with this command line:</p>
  
  <pre><code>ansible-playbook -i inventory authorized_key.yml -u "$USER" -k
  </code></pre>