Debian Codegraph @

lib/ansible/module_utils/facts/system/user.py

raw
1 # This file is part of Ansible
2 #
3 # Ansible is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # Ansible is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
15
16 from __future__ import annotations
17
18 import getpass
19 import os
20 import pwd
21 import typing as t
22
23 from ansible.module_utils.facts.collector import BaseFactCollector
24
25
26 class UserFactCollector(BaseFactCollector):
27 name = 'user'
28 _fact_ids = set(['user_id', 'user_uid', 'user_gid',
29 'user_gecos', 'user_dir', 'user_shell',
30 'real_user_id', 'effective_user_id',
31 'effective_group_ids']) # type: t.Set[str]
32
33 def collect(self, module=None, collected_facts=None):
34 user_facts = {}
35
36 user_facts['user_id'] = getpass.getuser()
37
38 try:
39 pwent = pwd.getpwnam(getpass.getuser())
40 except KeyError:
41 pwent = pwd.getpwuid(os.getuid())
42
43 user_facts['user_uid'] = pwent.pw_uid
44 user_facts['user_gid'] = pwent.pw_gid
45 user_facts['user_gecos'] = pwent.pw_gecos
46 user_facts['user_dir'] = pwent.pw_dir
47 user_facts['user_shell'] = pwent.pw_shell
48 user_facts['real_user_id'] = os.getuid()
49 user_facts['effective_user_id'] = os.geteuid()
50 user_facts['real_group_id'] = os.getgid()
51 user_facts['effective_group_id'] = os.getgid()
52
53 return user_facts