| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Abhijeet Kasurde <[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 |
| 15 |
short_description: Manage administrator permission for the local administrative account for the ESXi host |
| 16 |
description: |
| 17 |
- This module can be used to manage administrator permission for the local administrative account for the host when ESXi hostname is given. |
| 18 |
- This module is destructive as administrator permission are managed using APIs used, please read options carefully and proceed. |
| 19 |
- Please specify O(hostname) as vCenter IP or hostname only, as lockdown operations are not possible from standalone ESXi server. |
| 20 |
author: |
| 21 |
- Abhijeet Kasurde (@Akasurde) |
| 22 |
options: |
| 23 |
cluster_name: |
| 24 |
description: |
| 25 |
- Name of cluster. |
| 26 |
- All host systems from given cluster used to manage lockdown. |
| 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 lockdown. |
| 32 |
- Required parameter, if O(cluster_name) is not set. |
| 33 |
- See examples for specifications. |
| 34 |
type: list |
| 35 |
elements: str |
| 36 |
state: |
| 37 |
description: |
| 38 |
- State of hosts system |
| 39 |
- If set to V(disabled), all host systems will be removed from lockdown mode. |
| 40 |
- If host system is already out of lockdown mode and set to V(disabled), no action will be taken. |
| 41 |
- If set to V(normal), all host systems will be set in lockdown mode. |
| 42 |
- If host system is already in lockdown mode and set to V(normal), no action will be taken. |
| 43 |
- If set to V(strict), all host systems will be set in strict lockdown mode. |
| 44 |
- If host system is already in strict lockdown mode and set to V(strict), no action will be taken. |
| 45 |
default: normal |
| 46 |
choices: [ disabled, normal, strict ] |
| 47 |
type: str |
| 48 |
extends_documentation_fragment: |
| 49 |
- vmware.vmware.base_options |
| 50 |
|
| 51 |
''' |
| 52 |
|
| 53 |
EXAMPLES = r''' |
| 54 |
- name: Enter host system into lockdown mode |
| 55 |
community.vmware.vmware_host_lockdown: |
| 56 |
hostname: '{{ vcenter_hostname }}' |
| 57 |
username: '{{ vcenter_username }}' |
| 58 |
password: '{{ vcenter_password }}' |
| 59 |
esxi_hostname: '{{ esxi_hostname }}' |
| 60 |
state: normal |
| 61 |
delegate_to: localhost |
| 62 |
|
| 63 |
- name: Exit host systems from lockdown mode |
| 64 |
community.vmware.vmware_host_lockdown: |
| 65 |
hostname: '{{ vcenter_hostname }}' |
| 66 |
username: '{{ vcenter_username }}' |
| 67 |
password: '{{ vcenter_password }}' |
| 68 |
esxi_hostname: '{{ esxi_hostname }}' |
| 69 |
state: disabled |
| 70 |
delegate_to: localhost |
| 71 |
|
| 72 |
- name: Enter host systems into lockdown mode |
| 73 |
community.vmware.vmware_host_lockdown: |
| 74 |
hostname: '{{ vcenter_hostname }}' |
| 75 |
username: '{{ vcenter_username }}' |
| 76 |
password: '{{ vcenter_password }}' |
| 77 |
esxi_hostname: |
| 78 |
- '{{ esxi_hostname_1 }}' |
| 79 |
- '{{ esxi_hostname_2 }}' |
| 80 |
state: normal |
| 81 |
delegate_to: localhost |
| 82 |
|
| 83 |
- name: Exit host systems from lockdown mode |
| 84 |
community.vmware.vmware_host_lockdown: |
| 85 |
hostname: '{{ vcenter_hostname }}' |
| 86 |
username: '{{ vcenter_username }}' |
| 87 |
password: '{{ vcenter_password }}' |
| 88 |
esxi_hostname: |
| 89 |
- '{{ esxi_hostname_1 }}' |
| 90 |
- '{{ esxi_hostname_2 }}' |
| 91 |
state: disabled |
| 92 |
delegate_to: localhost |
| 93 |
|
| 94 |
- name: Enter all host system from cluster into lockdown mode |
| 95 |
community.vmware.vmware_host_lockdown: |
| 96 |
hostname: '{{ vcenter_hostname }}' |
| 97 |
username: '{{ vcenter_username }}' |
| 98 |
password: '{{ vcenter_password }}' |
| 99 |
cluster_name: '{{ cluster_name }}' |
| 100 |
state: normal |
| 101 |
delegate_to: localhost |
| 102 |
''' |
| 103 |
|
| 104 |
RETURN = r''' |
| 105 |
results: |
| 106 |
description: metadata about state of Host system lock down |
| 107 |
returned: always |
| 108 |
type: dict |
| 109 |
sample: { |
| 110 |
"host_lockdown_state": { |
| 111 |
"DC0_C0": { |
| 112 |
"current_state": "normal", |
| 113 |
"previous_state": "disabled", |
| 114 |
"desired_state": "normal", |
| 115 |
}, |
| 116 |
} |
| 117 |
} |
| 118 |
''' |
| 119 |
|
| 120 |
try: |
| 121 |
from pyVmomi import vim |
| 122 |
except ImportError: |
| 123 |
pass |
| 124 |
|
| 125 |
from ansible.module_utils.basic import AnsibleModule |
| 126 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 127 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 128 |
from ansible.module_utils.common.text.converters import to_native |
| 129 |
|
| 130 |
|
| 131 |
class VmwareLockdownManager(PyVmomi): |
| 132 |
def __init__(self, module): |
| 133 |
super(VmwareLockdownManager, self).__init__(module) |
| 134 |
if not self.is_vcenter(): |
| 135 |
self.module.fail_json(msg="Lockdown operations are performed from vCenter only. " |
| 136 |
"hostname %s is an ESXi server. Please specify hostname " |
| 137 |
"as vCenter server." % self.module.params['hostname']) |
| 138 |
cluster_name = self.params.get('cluster_name', None) |
| 139 |
esxi_host_name = self.params.get('esxi_hostname', None) |
| 140 |
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name) |
| 141 |
|
| 142 |
def ensure(self): |
| 143 |
""" |
| 144 |
Function to manage internal state management |
| 145 |
""" |
| 146 |
results = dict(changed=False, host_lockdown_state=dict()) |
| 147 |
change_list = [] |
| 148 |
desired_state = self.params.get('state') |
| 149 |
|
| 150 |
for host in self.hosts: |
| 151 |
current_state_api = host.configManager.hostAccessManager.lockdownMode |
| 152 |
current_state = current_state_api[8:].lower() |
| 153 |
results['host_lockdown_state'][host.name] = dict(current_state=desired_state, |
| 154 |
desired_state=desired_state, |
| 155 |
previous_state=current_state |
| 156 |
) |
| 157 |
changed = False |
| 158 |
if current_state != desired_state: |
| 159 |
changed = True |
| 160 |
if not self.module.check_mode: |
| 161 |
try: |
| 162 |
desired_state_api = 'lockdown' + desired_state.capitalize() |
| 163 |
host.configManager.hostAccessManager.ChangeLockdownMode(desired_state_api) |
| 164 |
except vim.fault.HostConfigFault as host_config_fault: |
| 165 |
self.module.fail_json(msg="Failed to manage lockdown mode for esxi" |
| 166 |
" hostname %s : %s" % (host.name, to_native(host_config_fault.msg))) |
| 167 |
except vim.fault.AdminDisabled as admin_disabled: |
| 168 |
self.module.fail_json(msg="Failed to manage lockdown mode as administrator " |
| 169 |
"permission has been disabled for " |
| 170 |
"esxi hostname %s : %s" % (host.name, to_native(admin_disabled.msg))) |
| 171 |
except Exception as generic_exception: |
| 172 |
self.module.fail_json(msg="Failed to manage lockdown mode due to generic exception for esxi " |
| 173 |
"hostname %s : %s" % (host.name, to_native(generic_exception))) |
| 174 |
|
| 175 |
change_list.append(changed) |
| 176 |
|
| 177 |
if any(change_list): |
| 178 |
results['changed'] = True |
| 179 |
|
| 180 |
self.module.exit_json(**results) |
| 181 |
|
| 182 |
|
| 183 |
def main(): |
| 184 |
argument_spec = base_argument_spec() |
| 185 |
argument_spec.update( |
| 186 |
cluster_name=dict(type='str', required=False), |
| 187 |
esxi_hostname=dict(type='list', required=False, elements='str'), |
| 188 |
state=dict(type='str', default='normal', choices=['disabled', 'normal', 'strict'], required=False), |
| 189 |
) |
| 190 |
|
| 191 |
module = AnsibleModule( |
| 192 |
argument_spec=argument_spec, |
| 193 |
supports_check_mode=True, |
| 194 |
required_one_of=[ |
| 195 |
['cluster_name', 'esxi_hostname'], |
| 196 |
] |
| 197 |
) |
| 198 |
|
| 199 |
vmware_lockdown_mgr = VmwareLockdownManager(module) |
| 200 |
vmware_lockdown_mgr.ensure() |
| 201 |
|
| 202 |
|
| 203 |
if __name__ == "__main__": |
| 204 |
main() |