Add multiple SSH keys using ansible


I have written an ansible script to remove SSH keys from remote servers:

Adding each file as a different task is preposterous, so I have tried using with_fileglob:

But this fails with lines like this:

failed: [] =>
(item=/Users/adamatan/ansible/ => {“failed”: true,
“item”: “/Users/adamatan/ansible/”} msg: invalid key
specified: /Users/adamatan/ansible/

The same key file is successfully removed using a unique task, but fails when it’s a part of a fileglob.

How can I batch add or remove SSH keys using ansible?


I believe you are only getting the filenames using with_fileglob, but with_file retrieves the contents of the file. And the authorized_key module requires the actual key.

So you should still loop by using with_fileglob, but instead of sending the filename to the “key=” parameter, you should use the file lookup plugin).

Leave a Reply