Debian Codegraph @

ansible_collections/community/vmware/plugins/modules/vmware_local_user_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Abhijeet Kasurde <[email protected]>
5 # Copyright: (c) 2018, Christian Kotte <[email protected]>
6 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
7 # SPDX-License-Identifier: GPL-3.0-or-later
8
9 from __future__ import absolute_import, division, print_function
10 __metaclass__ = type
11
12
13 DOCUMENTATION = r'''
14 ---
15 module: vmware_local_user_info
16 short_description: Gather info about users on the given ESXi host
17 description:
18 - This module can be used to gather information about users present on the given ESXi host system in VMware infrastructure.
19 - User must hold the 'Authorization.ModifyPermissions' privilege to invoke this module.
20 author:
21 - Abhijeet Kasurde (@Akasurde)
22 - Christian Kotte (@ckotte)
23 extends_documentation_fragment:
24 - vmware.vmware.base_options
25
26 '''
27
28 EXAMPLES = r'''
29 - name: Gather info about all Users on given ESXi host system
30 community.vmware.vmware_local_user_info:
31 hostname: '{{ esxi_hostname }}'
32 username: '{{ esxi_username }}'
33 password: '{{ esxi_password }}'
34 delegate_to: localhost
35 register: all_user_info
36 '''
37
38 RETURN = r'''
39 local_user_info:
40 description: metadata about all local users
41 returned: always
42 type: dict
43 sample: [
44 {
45 "role": "admin",
46 "description": "Administrator",
47 "group": false,
48 "user_id": 0,
49 "user_name": "root",
50 "shell_access": true
51 },
52 {
53 "role": "admin",
54 "description": "DCUI User",
55 "group": false,
56 "user_id": 100,
57 "user_name": "dcui",
58 "shell_access": false
59 },
60 ]
61 '''
62
63 try:
64 from pyVmomi import vmodl
65 except ImportError:
66 pass
67
68 from ansible.module_utils.basic import AnsibleModule
69 from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi
70 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
71 from ansible.module_utils.common.text.converters import to_native
72
73
74 class VMwareUserInfoManager(PyVmomi):
75 """Class to manage local user info"""
76
77 def __init__(self, module):
78 super(VMwareUserInfoManager, self).__init__(module)
79
80 if self.is_vcenter():
81 self.module.fail_json(
82 msg="Failed to get local account manager settings.",
83 details="It seems that '%s' is a vCenter server instead of an ESXi server" % self.module.params['hostname']
84 )
85
86 def gather_user_info(self):
87 """Gather info about local users"""
88 results = dict(changed=False, local_user_info=[])
89 search_string = ''
90 exact_match = False
91 find_users = True
92 find_groups = False
93 user_accounts = self.content.userDirectory.RetrieveUserGroups(
94 None, search_string, None, None, exact_match, find_users, find_groups
95 )
96 if user_accounts:
97 for user in user_accounts:
98 temp_user = dict()
99 temp_user['user_name'] = user.principal
100 temp_user['description'] = user.fullName
101 temp_user['group'] = user.group
102 temp_user['user_id'] = user.id
103 temp_user['shell_access'] = user.shellAccess
104 temp_user['role'] = None
105 try:
106 permissions = self.content.authorizationManager.RetrieveEntityPermissions(
107 entity=self.content.rootFolder,
108 inherited=False
109 )
110 except vmodl.fault.ManagedObjectNotFound as not_found:
111 self.module.fail_json(
112 msg="The entity doesn't exist: %s" % to_native(not_found)
113 )
114 for permission in permissions:
115 if permission.principal == user.principal:
116 temp_user['role'] = self.get_role_name(permission.roleId, self.content.authorizationManager.roleList)
117 break
118
119 results['local_user_info'].append(temp_user)
120 self.module.exit_json(**results)
121
122 @staticmethod
123 def get_role_name(role_id, role_list):
124 """Get role name from role ID"""
125 role_name = None
126 # Default role: No access
127 if role_id == -5:
128 role_name = 'no-access'
129 # Default role: Read-only
130 elif role_id == -2:
131 role_name = 'read-only'
132 # Default role: Administrator
133 elif role_id == -1:
134 role_name = 'admin'
135 # Custom roles
136 else:
137 for role in role_list:
138 if role.roleId == role_id:
139 role_name = role.name
140 break
141 return role_name
142
143
144 def main():
145 """Main"""
146 argument_spec = base_argument_spec()
147 module = AnsibleModule(argument_spec=argument_spec,
148 supports_check_mode=True)
149
150 vmware_local_user_info = VMwareUserInfoManager(module)
151 vmware_local_user_info.gather_user_info()
152
153
154 if __name__ == '__main__':
155 main()