ansible/ansible — pull request #7077 by kustodian (2014-04-19 19:27:33+00:00)
Title: Added 'clear_keys' parameter to the 'authorized_keys' module

Added a new `clear_keys` parameter to the `authorized_keys` module which if it is set to yes (default: no), clears all keys from the authorized_keys file before adding a new key.

This parameter could be useful if you would like to be sure that the user's authorized_keys file only has the key(s) you would like it to have and no other. Because the `authorized_keys` module appends new keys and only deletes those whose state is put as `absent` it was very hard of deleting other keys which were not set by Ansible. `clear_keys=yes` could be an easy and fast solution for clearing all unneeded keys.

#### Note
If `clear_keys=yes` is used in a loop over keys, it will only leave the last key in the authorized_keys. For example:
```
- authorized_key: user="username" key="{{ item }}" clear_keys=yes
with_items:
- {{ key1 }}
- {{ key2 }}
- {{ key3 }}
```
will only leave `key3` in the authorized_keys file. The workaround for this is to either only set `clear_keys` for the first key, or just put all the keys in the same file separated by a new line (which is probably easier anyway).