Best approach to add or remove users to match list

I am using a list of users to define the exact set of user accounts I need on all of my managed compute instances.

For example:

users:

  • {name: randy, group: admin, shell: /bin/bash, uid: 1000, sshkey: /files/users/randy_rsa.pub, … }
  • {name: teresa, group: admin, shell: /bin/bash, uid: 1001, sshkey: /files/users/teresa_rsa.pub, … }
  • {name: mircea, group: admin, shell: /bin/bash, uid: 1002, sshkey: /files/users/mircea_rsa.pub, … }

I would like to be able to add or remove users from this list and have the user accounts modified on all systems to match this list.

What this means is:

  • If a /home/user-unwanted is found, it should be removed
  • if a /home/user-wanted is not found, it should be added

otherwise, the user accounts should not be touched.

What is the best practice approach to obtaining two lists (the list of /home/* directories) and the above list in a variable file, and making only the changes needed to make these list match each other?

Thanks – Randy

I have the similar requirement and started working on a role called user management. It is available from https://galaxy.ansibleworks.com/list#/roles/22 but it is far from complete. As a proof-of-concept it currently deals with user-accounts that should be removed (or locked / disabled) if they no longer exist in a given list.

I’m planing to enhance that role and if you were interested in joining forces, that would be great.