| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2016, IBM Corp |
| 5 |
# Author(s): Andreas Nafpliotis <[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_manager |
| 16 |
short_description: Manage local users on an ESXi host |
| 17 |
description: |
| 18 |
- Manage local users on an ESXi host |
| 19 |
author: |
| 20 |
- Andreas Nafpliotis (@nafpliot-ibm) |
| 21 |
notes: |
| 22 |
- Be sure that the ESXi user used for login, has the appropriate rights to create / delete / edit users |
| 23 |
options: |
| 24 |
local_user_name: |
| 25 |
description: |
| 26 |
- The local user name to be changed. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
local_user_password: |
| 30 |
description: |
| 31 |
- The password to be set. |
| 32 |
required: false |
| 33 |
type: str |
| 34 |
local_user_description: |
| 35 |
description: |
| 36 |
- Description for the user. |
| 37 |
required: false |
| 38 |
type: str |
| 39 |
state: |
| 40 |
description: |
| 41 |
- Indicate desired state of the user. If the user already exists when V(present), the user info is updated |
| 42 |
choices: ['present', 'absent'] |
| 43 |
default: present |
| 44 |
type: str |
| 45 |
extends_documentation_fragment: |
| 46 |
- vmware.vmware.base_options |
| 47 |
|
| 48 |
''' |
| 49 |
|
| 50 |
EXAMPLES = r''' |
| 51 |
- name: Add local user to ESXi |
| 52 |
community.vmware.vmware_local_user_manager: |
| 53 |
hostname: esxi_hostname |
| 54 |
username: root |
| 55 |
password: vmware |
| 56 |
local_user_name: foo |
| 57 |
local_user_password: password |
| 58 |
delegate_to: localhost |
| 59 |
''' |
| 60 |
|
| 61 |
RETURN = r'''# ''' |
| 62 |
|
| 63 |
try: |
| 64 |
from pyVmomi import vim, 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 |
|
| 72 |
|
| 73 |
class VMwareLocalUserManager(PyVmomi): |
| 74 |
|
| 75 |
def __init__(self, module): |
| 76 |
super(VMwareLocalUserManager, self).__init__(module) |
| 77 |
self.local_user_name = self.module.params['local_user_name'] |
| 78 |
self.local_user_password = self.module.params['local_user_password'] |
| 79 |
self.local_user_description = self.module.params['local_user_description'] |
| 80 |
self.state = self.module.params['state'] |
| 81 |
|
| 82 |
if self.is_vcenter(): |
| 83 |
self.module.fail_json(msg="Failed to get local account manager settings " |
| 84 |
"from ESXi server: %s" % self.module.params['hostname'], |
| 85 |
details="It seems that %s is a vCenter server instead of an " |
| 86 |
"ESXi server" % self.module.params['hostname']) |
| 87 |
|
| 88 |
def process_state(self): |
| 89 |
try: |
| 90 |
local_account_manager_states = { |
| 91 |
'absent': { |
| 92 |
'present': self.state_remove_user, |
| 93 |
'absent': self.state_exit_unchanged, |
| 94 |
}, |
| 95 |
'present': { |
| 96 |
'present': self.state_update_user, |
| 97 |
'absent': self.state_create_user, |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
local_account_manager_states[self.state][self.check_local_user_manager_state()]() |
| 102 |
except vmodl.RuntimeFault as runtime_fault: |
| 103 |
self.module.fail_json(msg=runtime_fault.msg) |
| 104 |
except vmodl.MethodFault as method_fault: |
| 105 |
self.module.fail_json(msg=method_fault.msg) |
| 106 |
except Exception as e: |
| 107 |
self.module.fail_json(msg=str(e)) |
| 108 |
|
| 109 |
def check_local_user_manager_state(self): |
| 110 |
user_account = self.find_user_account() |
| 111 |
if not user_account: |
| 112 |
return 'absent' |
| 113 |
else: |
| 114 |
return 'present' |
| 115 |
|
| 116 |
def find_user_account(self): |
| 117 |
searchStr = self.local_user_name |
| 118 |
exactMatch = True |
| 119 |
findUsers = True |
| 120 |
findGroups = False |
| 121 |
user_account = self.content.userDirectory.RetrieveUserGroups(None, searchStr, None, None, exactMatch, findUsers, findGroups) |
| 122 |
return user_account |
| 123 |
|
| 124 |
def create_account_spec(self): |
| 125 |
account_spec = vim.host.LocalAccountManager.AccountSpecification() |
| 126 |
account_spec.id = self.local_user_name |
| 127 |
account_spec.password = self.local_user_password |
| 128 |
account_spec.description = self.local_user_description |
| 129 |
return account_spec |
| 130 |
|
| 131 |
def state_create_user(self): |
| 132 |
account_spec = self.create_account_spec() |
| 133 |
|
| 134 |
try: |
| 135 |
self.content.accountManager.CreateUser(account_spec) |
| 136 |
self.module.exit_json(changed=True) |
| 137 |
except vmodl.RuntimeFault as runtime_fault: |
| 138 |
self.module.fail_json(msg=runtime_fault.msg) |
| 139 |
except vmodl.MethodFault as method_fault: |
| 140 |
self.module.fail_json(msg=method_fault.msg) |
| 141 |
|
| 142 |
def state_update_user(self): |
| 143 |
account_spec = self.create_account_spec() |
| 144 |
|
| 145 |
try: |
| 146 |
self.content.accountManager.UpdateUser(account_spec) |
| 147 |
self.module.exit_json(changed=True) |
| 148 |
except vmodl.RuntimeFault as runtime_fault: |
| 149 |
self.module.fail_json(msg=runtime_fault.msg) |
| 150 |
except vmodl.MethodFault as method_fault: |
| 151 |
self.module.fail_json(msg=method_fault.msg) |
| 152 |
|
| 153 |
def state_remove_user(self): |
| 154 |
try: |
| 155 |
self.content.accountManager.RemoveUser(self.local_user_name) |
| 156 |
self.module.exit_json(changed=True) |
| 157 |
except vmodl.RuntimeFault as runtime_fault: |
| 158 |
self.module.fail_json(msg=runtime_fault.msg) |
| 159 |
except vmodl.MethodFault as method_fault: |
| 160 |
self.module.fail_json(msg=method_fault.msg) |
| 161 |
|
| 162 |
def state_exit_unchanged(self): |
| 163 |
self.module.exit_json(changed=False) |
| 164 |
|
| 165 |
|
| 166 |
def main(): |
| 167 |
argument_spec = base_argument_spec() |
| 168 |
argument_spec.update(dict(local_user_name=dict(required=True, type='str'), |
| 169 |
local_user_password=dict(type='str', no_log=True), |
| 170 |
local_user_description=dict(type='str'), |
| 171 |
state=dict(default='present', choices=['present', 'absent'], type='str'))) |
| 172 |
|
| 173 |
module = AnsibleModule(argument_spec=argument_spec, |
| 174 |
required_if=[ |
| 175 |
['state', 'present', ['local_user_password']] |
| 176 |
], |
| 177 |
supports_check_mode=False) |
| 178 |
|
| 179 |
vmware_local_user_manager = VMwareLocalUserManager(module) |
| 180 |
vmware_local_user_manager.process_state() |
| 181 |
|
| 182 |
|
| 183 |
if __name__ == '__main__': |
| 184 |
main() |