How to delete SSH public (authorized_keys) keys with Ansible

Written by - 0 comments

Published on - Listed in Ansible Linux


When managing SSH keys with Ansible, the authorized_keys module is a very handy tool.

Managing SSH keys in Ansible

By using the lookup function, Ansible identifies all the SSH (public) keys in one file and verifies the keys are present in the user's ~.ssh/authorized_keys file on the target server.

  # Deploy SSH key
  - name: USER - Deploy SSH key
    authorized_key:
      user: claudio
      key: "{{ lookup('file', '/srv/ansible/setup-files/authorized_keys/claudio.pub') }}"

The local key file (here claudio.pub on the Ansible server) can contain one or more keys. The task can also be used with a with_items/loop which can be used to manage keys for multiple users in the same task.

But how to delete a key?

But what if one of the keys inside the file should be removed? There can be many reasons for this:

  • the private key was lost
  • the private key was stolen/leaked
  • the password of the private key got forgotten
  • when using a SSH user for a whole team, one of the team members doesn't work for the team anymore
  • ...

Interestingly, it's not enough to simply delete the public key in the local file. I expected Ansible to parse through each key and:

a) if not present on the target server, add the key on the target server
b) if not present in the local file, remove the key on the target server

But b) isn't happening. I realized this, when I removed a couple of keys lately and there was no "changed" shown for the task.

By default, Ansible lets all the already existing SSH keys remain on the target server; it only makes sure that the keys from the local file are present. The documentation mentions:

exclusive (boolean): Whether to remove all other non-specified keys from the authorized_keys file. [...]

If you want to make sure all the keys on the target server are the same as in the local file, you need to add exclusive: true to the task:

  # Deploy SSH key
  - name: USER - Deploy SSH key
    authorized_key:
      user: claudio
      key: "{{ lookup('file', '/srv/ansible/setup-files/authorized_keys/claudio.pub') }}"
      exclusive: true

After running the playbook again, the task was set to "changed" and the old SSH keys were removed on the target server.


Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.

RSS feed

Blog Tags:

  AWS   Android   Ansible   Apache   Apple   Atlassian   BSD   Backup   Bash   Bluecoat   CMS   Chef   Cloud   Coding   Consul   Containers   CouchDB   DB   DNS   Database   Databases   Docker   ELK   Elasticsearch   Filebeat   FreeBSD   Galera   Git   GlusterFS   Grafana   Graphics   HAProxy   HTML   Hacks   Hardware   Icinga   Influx   Internet   Java   KVM   Kibana   Kodi   Kubernetes   LVM   LXC   Linux   Logstash   Mac   Macintosh   Mail   MariaDB   Minio   MongoDB   Monitoring   Multimedia   MySQL   NFS   Nagios   Network   Nginx   OSSEC   OTRS   Office   PGSQL   PHP   Perl   Personal   PostgreSQL   Postgres   PowerDNS   Proxmox   Proxy   Python   Rancher   Rant   Redis   Roundcube   SSL   Samba   Seafile   Security   Shell   SmartOS   Solaris   Surveillance   Systemd   TLS   Tomcat   Ubuntu   Unix   VMWare   VMware   Varnish   Virtualization   Windows   Wireless   Wordpress   Wyse   ZFS   Zoneminder