Verified Commit 72395058 authored by Maciej Delmanowski's avatar Maciej Delmanowski

Merge branch 'drybjed-alternative-local-getent'

parents 6282db84 9dfc582d
Pipeline #62751788 passed with stages
in 15 minutes and 28 seconds
#!/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),
key=operator.attrgetter("pw_name"),
)[0]
return ({user.pw_name: ["x", user.pw_uid, user.pw_gid, user.pw_gecos,
user.pw_dir, user.pw_shell]})
if __name__ == "__main__":
try:
user = sys.argv[1]
try:
print(dumps(getent(user), sort_keys=True, indent=4))
except IndexError:
sys.exit(2)
except Exception:
print("Usage: %s <user>" % os.path.basename(sys.argv[0]))
sys.exit(1)
---
- name: Gather local Ansible user details
getent:
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
set_fact:
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
package:
......
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