[debops.system_users] Custom local getent script

The 'getent' Ansible module does not work correctly on Apple macOS,
therefore the 'debops.system_users' role will use a custom Python script
to get the current Ansible user information from the Ansible Controller.

Ref: https://github.com/ansible/ansible/issues/38339
parent 6282db84
#!/usr/bin/env python
# Get the specified user information from the system password database.
# This solution should work on all platforms; Ansible 'getent' module does not
# work correctly on Apple macOS.
# Ref: https://github.com/ansible/ansible/issues/38339
# Usage: getent_passwd.py <username>
import sys
import os
import pwd
import operator
from json import dumps
def getent(user):
all = pwd.getpwall()
user = sorted(
(u for u in all if u.pw_name == user),
return ({user.pw_name: ["x", user.pw_uid, user.pw_gid, user.pw_gecos,
user.pw_dir, user.pw_shell]})
if __name__ == "__main__":
user = sys.argv[1]
print(dumps(getent(user), sort_keys=True, indent=4))
except IndexError:
except Exception:
print("Usage: %s <user>" % os.path.basename(sys.argv[0]))
- name: Gather local Ansible user details
database: 'passwd'
key: '{{ system_users__self_name }}'
script: 'script/getent_passwd.py {{ system_users__self_name }}'
register: system_users__register_passwd
delegate_to: 'localhost'
become: False
changed_when: False
check_mode: False
run_once: True
- name: Remember local Ansible user details
system_users__fact_self_comment: '{{ getent_passwd[system_users__self_name][3] }}'
system_users__fact_self_shell: '{{ getent_passwd[system_users__self_name][5] }}'
system_users__fact_self_comment: '{{ (system_users__register_passwd.stdout|from_json)[system_users__self_name][3] }}'
system_users__fact_self_shell: '{{ (system_users__register_passwd.stdout|from_json)[system_users__self_name][5] }}'
- name: Ensure that reqired packages are installed
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment