By using Ansible, I try to make sure that the .ssh/authorized_keys
files of our servers contain only a given set of ssh keys. No matter the arrangement.
- If one is missing, add it (no problem,
lineinfile
) - If someone else sneaked in an extra key (which is not in the "with_items" list), remove it and return some warning, or something. Well... "changed" could be acceptable too, but it would be nice to differentiate somehow the "missing" and "sneaked in" lines.
The first part is easy:
- name: Ensure ssh authorized_keys contains the right users
lineinfile:
path: /root/.ssh/authorized_keys
owner: root
group: root
mode: 0600
state: present
line: '{{ item }}'
with_items:
- ssh-rsa AABBCC112233... root@someserver.com
- ssh-rsa DDEEFF112233... user@anothersomeserver.com
But the second part looks more tricky. At least to get it done with short and elegant code.
Any ideas?