Welcome, Guest! Registration

loc2log

Sunday, 2024-12-22
Main » 2020 » July » 4 » Check if user exists with Ansible
8:57 PM
Check if user exists with Ansible

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

Views: 8340 | Added by: ep | Tags: ansible | Rating: 1.0/1
Total comments: 0
Only registered users can add comments.
[ Registration | Login ]