# authorized\_keys module is deleting too many line when using state: absent

**URL:** <https://forum.ansible.com/t/authorized-keys-module-is-deleting-too-many-line-when-using-state-absent/33039>\
**Category:** Ansible Project\
**Created:** [May 28, 2020, 2:07pm UTC](https://forum.ansible.com/t/authorized-keys-module-is-deleting-too-many-line-when-using-state-absent/33039 "2020-05-28T14:07:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mario\_Garcia](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@Mario\_Garcia](https://forum.ansible.com/u/Mario_Garcia)\
**Post date:** [May 28, 2020, 2:07pm UTC](https://forum.ansible.com/t/authorized-keys-module-is-deleting-too-many-line-when-using-state-absent/33039/1 "2020-05-28T14:07:15Z")

</div>

Hello

I need to clean up a bit the authorized keys files on our infra

i created a simple palybook that goes and removed one key from the remote authorized\_keys files but when I run it in check/diff mode i see it tried to remove far too many lines

this is the playbook

`

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 28, 2020, 2:34pm UTC](https://forum.ansible.com/t/authorized-keys-module-is-deleting-too-many-line-when-using-state-absent/33039/2 "2020-05-28T14:34:22Z")

</div>

Hard to tell what is going on without more information.

\* What do the keys that are being removed look like?  
\* What does /path/to/totpubkey.pub look like?  
\* What do the authorized\_keys file(s) look like?

Could it be that the same pubkey is listed in your authorized\_keys  
several times but with different comments?

Dick

---

<div class="post-metadata">

**Author:** ![Mario\_Garcia](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@Mario\_Garcia](https://forum.ansible.com/u/Mario_Garcia)\
**Post date:** [May 28, 2020, 3:02pm UTC](https://forum.ansible.com/t/authorized-keys-module-is-deleting-too-many-line-when-using-state-absent/33039/3 "2020-05-28T15:02:40Z")

</div>

Hello

no the key are differents but i think that ansible modules only match the beginning of the line but not the whole file

i am not going to list all the public keys here but:

the public key that has to be removed and the ones that are wrongly being removed have the ‘same’ beginning::

ssh-rsa AAAAB3NzaC1yc2EAAAA…  
after this both lines in authorized keys differ

so does the authorized\_key module only looks for the beginning of the line?

the /path/to/totpubkey.pub is a normal regular ssh-rsa public key file are standard public file with the publick key and authorized key files are one key per line… nothing fancy

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 28, 2020, 4:01pm UTC](https://forum.ansible.com/t/authorized-keys-module-is-deleting-too-many-line-when-using-state-absent/33039/4 "2020-05-28T16:01:24Z")

</div>

The fact that multiple keys begin with AAAAB3NzaC1yc2EAAAA is because  
they share the same header - this is OK.  
The module takes the entire key string into account.

Can you maybe reproduce this in a way that we can look at it?  
Otherwise it will be impossible to tell what is wrong (other that  
guessing what it might be).

Dick

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 28, 2020, 4:24pm UTC](https://forum.ansible.com/t/authorized-keys-module-is-deleting-too-many-line-when-using-state-absent/33039/5 "2020-05-28T16:24:45Z")

</div>

You could try and anonymize the usernames and comments and if that has the same problems, post that file. Then you’d be only posting public keys but no user names etc?

---

<div class="post-metadata">

**Author:** ![Mario\_Garcia](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@Mario\_Garcia](https://forum.ansible.com/u/Mario_Garcia)\
**Post date:** [May 28, 2020, 5:31pm UTC](https://forum.ansible.com/t/authorized-keys-module-is-deleting-too-many-line-when-using-state-absent/33039/6 "2020-05-28T17:31:09Z")

</div>

I am working on it to provide you an use case… but.

is by any chance the authorizing\_file modules sanitizing aka removing duplicates entries on the remote authorized\_key file even if it was not in the key string passed to be removed

in that case there is probably no issue i must do some tests 🙂

i keep you posted.

regards

M

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 28, 2020, 7:39pm UTC](https://forum.ansible.com/t/authorized-keys-module-is-deleting-too-many-line-when-using-state-absent/33039/7 "2020-05-28T19:39:04Z")

</div>

No, there is no such sanitizing thing.  
There is the 'exclusive' option but that would remove everything else  
but your key. And you're not using that.

[https://docs.ansible.com/ansible/latest/modules/authorized\_key\_module.html#parameter-exclusive](https://docs.ansible.com/ansible/latest/modules/authorized_key_module.html#parameter-exclusive)

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [May 28, 2020, 7:51pm UTC](https://forum.ansible.com/t/authorized-keys-module-is-deleting-too-many-line-when-using-state-absent/33039/8 "2020-05-28T19:51:46Z")

</div>

Hi all,

> \>  
> \> I am working on it to provide you an use case.. but.  
> \>  
> \> is by any chance the authorizing\_file modules sanitizing aka  
> \> removing duplicates entries on the remote authorized\_key file even  
> \> if it was not in the key string passed to be removed
> 
> No, there is no such sanitizing thing.

I just looked at the code  
([https://github.com/ansible-collections/ansible.posix/blob/master/plugins/modules/authorized\_key.py](https://github.com/ansible-collections/ansible.posix/blob/master/plugins/modules/authorized_key.py)).  
It does indeed remove duplicates. It puts all lines of authorized\_keys  
into a dictionary, indexed by the actual key:  
[https://github.com/ansible-collections/ansible.posix/blob/master/plugins/modules/authorized\_key.py#L450-L461](https://github.com/ansible-collections/ansible.posix/blob/master/plugins/modules/authorized_key.py#L450-L461)

The value in the dictionary contains more information so that the file  
can be rebuilt - except that duplicate keys won't survive.

It's probably a good idea to mention that in the module docs. If  
someone wants to create a PR for that (it's a good start to trying PRs  
for collections!), feel free!

Cheers,  
Felix

---

<div class="post-metadata">

**Author:** ![Dan\_Idar](https://avatars.discourse-cdn.com/v4/letter/d/e9a140/32.png) [@Dan\_Idar](https://forum.ansible.com/u/Dan_Idar)\
**Post date:** [May 28, 2020, 11:56pm UTC](https://forum.ansible.com/t/authorized-keys-module-is-deleting-too-many-line-when-using-state-absent/33039/9 "2020-05-28T23:56:48Z")

</div>

I ran some tests

created on vagrant some ssh files and created a authorized\_keys with duplicates and tried to add/remove a line that is not a duplicate

the result is that if there is a line to be added/removed from the authorized\_keys files the duplicates are also removed if there are no lines found to be removed then nothing happens

illustration:

authorized keys files notice the publicates in line 1,4,5 line 4 has a different comment line 5 has no comment

`  
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJwjZJR1W1yJJ7QvfD8ym/tIyl0pLAlNGxjci4lsXjY4 vagrant@localhost.localdomain  
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHgAOaG6REJxdsfOQmyLhpQ8Q+j0qNyiUuqlYLk6/j5M vagrant@localhost.localdomain  
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMc8GxolEFe89BjWEnT3fHfqnL5eVMt8aw2ZJ54Iu6dX vagrant@localhost.localdomain  
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJwjZJR1W1yJJ7QvfD8ym/tIyl0pLAlNGxjci4lsXjY4 ansible  
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJwjZJR1W1yJJ7QvfD8ym/tIyl0pLAlNGxjci4lsXjY4

`

this is a playbook to add a new line… to remove is the same principle ;

`
