It is easy to detect if a user is registered on a Linux system with Ansible getent module.
Example:
- name: Check if user exists
getent:
database: passwd
key: "{{ user_name }}"
fail_key: False
- debug: var=getent_password
Here we simply use user_name variable with whatever user we need to find. And the result goes to an auto-registered variable "getent_password". If the user we are looking for is not there, we get dictionary with null value. E.g. we were looking for user_name: super, then the debug output will look like this:
getent_passwd:
super: null