| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2022, Mario Lenz <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = r''' |
| 13 |
--- |
| 14 |
module: vmware_host_lockdown_exceptions |
| 15 |
version_added: '3.1.0' |
| 16 |
short_description: Manage Lockdown Mode Exception Users |
| 17 |
description: |
| 18 |
- This module can be used to manage Lockdown Mode Exception Users. |
| 19 |
- Please specify O(hostname) as vCenter IP or hostname only, as lockdown operations are not possible from standalone ESXi server. |
| 20 |
author: |
| 21 |
- Mario Lenz (@mariolenz) |
| 22 |
options: |
| 23 |
cluster_name: |
| 24 |
description: |
| 25 |
- Name of cluster. |
| 26 |
- All host systems from given cluster used to manage exception users. |
| 27 |
- Required parameter, if O(esxi_hostname) is not set. |
| 28 |
type: str |
| 29 |
esxi_hostname: |
| 30 |
description: |
| 31 |
- List of ESXi hostname to manage exception users. |
| 32 |
- Required parameter, if O(cluster_name) is not set. |
| 33 |
type: list |
| 34 |
elements: str |
| 35 |
state: |
| 36 |
description: |
| 37 |
- If V(present), make sure the given users are defined as Lockdown Mode Exception Users. |
| 38 |
- If V(absent), make sure the given users are NO Lockdown Mode Exception Users. |
| 39 |
- If V(set), will replace Lockdown Mode Exception Users defined list of users. |
| 40 |
default: present |
| 41 |
choices: [ present, absent , set ] |
| 42 |
type: str |
| 43 |
exception_users: |
| 44 |
description: |
| 45 |
- List of Lockdown Mode Exception Users. |
| 46 |
- To remove all Exception Users, O(state=set) the empty list. |
| 47 |
type: list |
| 48 |
elements: str |
| 49 |
required: true |
| 50 |
extends_documentation_fragment: |
| 51 |
- vmware.vmware.base_options |
| 52 |
|
| 53 |
''' |
| 54 |
|
| 55 |
EXAMPLES = r''' |
| 56 |
- name: Remove all Lockdown Mode Exception Users on a host |
| 57 |
community.vmware.vmware_host_lockdown: |
| 58 |
hostname: '{{ vcenter_hostname }}' |
| 59 |
username: '{{ vcenter_username }}' |
| 60 |
password: '{{ vcenter_password }}' |
| 61 |
esxi_hostname: '{{ esxi_hostname }}' |
| 62 |
exception_users: [] |
| 63 |
state: set |
| 64 |
delegate_to: localhost |
| 65 |
''' |
| 66 |
|
| 67 |
RETURN = r''' |
| 68 |
results: |
| 69 |
description: metadata about exception users of Host systems |
| 70 |
returned: always |
| 71 |
type: dict |
| 72 |
sample: { |
| 73 |
"host_lockdown_exceptions": { |
| 74 |
"DC0_C0": { |
| 75 |
"current_exception_users": [], |
| 76 |
"desired_exception_users": [], |
| 77 |
"previous_exception_users": [ |
| 78 |
"root" |
| 79 |
] |
| 80 |
}, |
| 81 |
} |
| 82 |
} |
| 83 |
''' |
| 84 |
|
| 85 |
try: |
| 86 |
from pyVmomi import vim |
| 87 |
except ImportError: |
| 88 |
pass |
| 89 |
|
| 90 |
from ansible.module_utils.basic import AnsibleModule |
| 91 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 92 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 93 |
from ansible.module_utils.common.text.converters import to_native |
| 94 |
|
| 95 |
|
| 96 |
class VmwareLockdownManager(PyVmomi): |
| 97 |
def __init__(self, module): |
| 98 |
super(VmwareLockdownManager, self).__init__(module) |
| 99 |
if not self.is_vcenter(): |
| 100 |
self.module.fail_json(msg="Lockdown operations are performed from vCenter only. " |
| 101 |
"hostname %s is an ESXi server. Please specify hostname " |
| 102 |
"as vCenter server." % self.module.params['hostname']) |
| 103 |
cluster_name = self.params.get('cluster_name', None) |
| 104 |
esxi_host_name = self.params.get('esxi_hostname', None) |
| 105 |
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name) |
| 106 |
|
| 107 |
def ensure(self): |
| 108 |
""" |
| 109 |
Function to manage internal state management |
| 110 |
""" |
| 111 |
results = dict(changed=False, host_lockdown_exceptions=dict()) |
| 112 |
change_list = [] |
| 113 |
desired_state = self.params.get('state') |
| 114 |
exception_users = self.params.get('exception_users') |
| 115 |
for host in self.hosts: |
| 116 |
current_exception_users = host.configManager.hostAccessManager.QueryLockdownExceptions() |
| 117 |
current_exception_users.sort() |
| 118 |
new_exception_users = current_exception_users.copy() |
| 119 |
results['host_lockdown_exceptions'][host.name] = dict(previous_exception_users=current_exception_users) |
| 120 |
changed = False |
| 121 |
|
| 122 |
if desired_state == 'present': |
| 123 |
for user in exception_users: |
| 124 |
if user not in current_exception_users: |
| 125 |
new_exception_users.append(user) |
| 126 |
changed = True |
| 127 |
elif desired_state == 'absent': |
| 128 |
for user in exception_users: |
| 129 |
if user in current_exception_users: |
| 130 |
new_exception_users.remove(user) |
| 131 |
changed = True |
| 132 |
elif desired_state == 'set': |
| 133 |
if set(current_exception_users) != set(exception_users): |
| 134 |
new_exception_users = exception_users |
| 135 |
changed = True |
| 136 |
|
| 137 |
new_exception_users.sort() |
| 138 |
results['host_lockdown_exceptions'][host.name]['desired_exception_users'] = new_exception_users |
| 139 |
results['host_lockdown_exceptions'][host.name]['current_exception_users'] = new_exception_users |
| 140 |
|
| 141 |
if changed and not self.module.check_mode: |
| 142 |
try: |
| 143 |
host.configManager.hostAccessManager.UpdateLockdownExceptions(new_exception_users) |
| 144 |
|
| 145 |
except vim.fault.HostConfigFault as host_config_fault: |
| 146 |
self.module.fail_json(msg="Failed to manage lockdown mode for esxi" |
| 147 |
" hostname %s : %s" % (host.name, to_native(host_config_fault.msg))) |
| 148 |
except vim.fault.AdminDisabled as admin_disabled: |
| 149 |
self.module.fail_json(msg="Failed to manage lockdown mode as administrator " |
| 150 |
"permission has been disabled for " |
| 151 |
"esxi hostname %s : %s" % (host.name, to_native(admin_disabled.msg))) |
| 152 |
except Exception as generic_exception: |
| 153 |
self.module.fail_json(msg="Failed to manage lockdown mode due to generic exception for esxi " |
| 154 |
"hostname %s : %s" % (host.name, to_native(generic_exception))) |
| 155 |
change_list.append(changed) |
| 156 |
|
| 157 |
if any(change_list): |
| 158 |
results['changed'] = True |
| 159 |
|
| 160 |
self.module.exit_json(**results) |
| 161 |
|
| 162 |
|
| 163 |
def main(): |
| 164 |
argument_spec = base_argument_spec() |
| 165 |
argument_spec.update( |
| 166 |
cluster_name=dict(type='str', required=False), |
| 167 |
esxi_hostname=dict(type='list', required=False, elements='str'), |
| 168 |
exception_users=dict(type='list', required=True, elements='str'), |
| 169 |
state=dict(type='str', default='present', choices=['present', 'absent', 'set'], required=False), |
| 170 |
) |
| 171 |
|
| 172 |
module = AnsibleModule( |
| 173 |
argument_spec=argument_spec, |
| 174 |
supports_check_mode=True, |
| 175 |
required_one_of=[ |
| 176 |
['cluster_name', 'esxi_hostname'], |
| 177 |
] |
| 178 |
) |
| 179 |
|
| 180 |
vmware_lockdown_mgr = VmwareLockdownManager(module) |
| 181 |
vmware_lockdown_mgr.ensure() |
| 182 |
|
| 183 |
|
| 184 |
if __name__ == "__main__": |
| 185 |
main() |