title: Add multiple SSH keys using ansible
body: <p>I have written an ansible script to remove SSH keys from remote servers:</p>
  
  <pre class="lang-yaml prettyprint-override"><code>---
  - name: "Add keys to the authorized_keys of the user ubuntu"
    user: ubuntu
    hosts: www
    tasks:
    - name: "Remove key #1"
      authorized_key: user=ubuntu key="{{ item }}" state=absent
      with_file:
       - id_rsa_number_one.pub
    - name: "Remove key #2"
      authorized_key: user=ubuntu key="{{ item }}" state=absent
      with_file:
       - id_rsa_number_two.pub
  ...
  </code></pre>
  
  <p>Adding each file as a different task is preposterous, so I have tried using <a href="http://docs.ansible.com/playbooks_loops.html#looping-over-fileglobs" rel="noreferrer"><code>with_fileglob</code></a>:</p>
  
  <pre class="lang-yaml prettyprint-override"><code>  - name: "Remove all keys at once"
      authorized_key: user=ubuntu key="{{ item }}" state=absent
      with_fileglob:
        - /Users/adamatan/ansible/id_rsa*.pub
  </code></pre>
  
  <p>But this fails with lines like this:</p>
  
  <blockquote>
    <p>failed: [www.example.com] =>
    (item=/Users/adamatan/ansible/id_rsa_one.pub) => {"failed": true,
    "item": "/Users/adamatan/ansible/id_rsa_one.pub"} msg: invalid key
    specified: /Users/adamatan/ansible/id_rsa_one.pub</p>
  </blockquote>
  
  <p>The same key file is successfully removed using a unique task, but fails when it's a part of a <code>fileglob</code>.</p>
  
  <p><strong>How can I batch add or remove SSH keys using ansible?</strong></p>