| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, VMware, Inc. |
| 5 |
# Copyright: (c) 2019, Abhijeet Kasurde <[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_dvswitch_nioc |
| 16 |
short_description: Manage distributed switch Network IO Control |
| 17 |
description: |
| 18 |
- This module can be used to manage distributed switch Network IO Control configurations. |
| 19 |
author: |
| 20 |
- Joseph Andreatta (@vmwjoseph) |
| 21 |
options: |
| 22 |
switch: |
| 23 |
description: |
| 24 |
- The name of the distributed switch. |
| 25 |
required: true |
| 26 |
aliases: ['dvswitch'] |
| 27 |
type: str |
| 28 |
version: |
| 29 |
description: |
| 30 |
- Network IO control version. |
| 31 |
choices: |
| 32 |
- 'version2' |
| 33 |
- 'version3' |
| 34 |
required: false |
| 35 |
type: str |
| 36 |
state: |
| 37 |
description: |
| 38 |
- Enable or disable NIOC on the distributed switch. |
| 39 |
default: 'present' |
| 40 |
choices: ['present', 'absent'] |
| 41 |
required: false |
| 42 |
type: str |
| 43 |
resources: |
| 44 |
description: |
| 45 |
- List of dicts containing. |
| 46 |
suboptions: |
| 47 |
name: |
| 48 |
description: |
| 49 |
- Resource name. |
| 50 |
choices: ["faultTolerance", "hbr", "iSCSI", "management", "nfs", "vdp", "virtualMachine", "vmotion", "vsan", "backupNfc", "nvmetcp"] |
| 51 |
required: true |
| 52 |
type: str |
| 53 |
limit: |
| 54 |
description: |
| 55 |
- The maximum allowed usage for a traffic class belonging to this resource pool per host physical NIC. |
| 56 |
default: -1 |
| 57 |
type: int |
| 58 |
reservation: |
| 59 |
description: |
| 60 |
- Ignored if NIOC version is set to version2 |
| 61 |
- Amount of bandwidth resource that is guaranteed available to the host infrastructure traffic class. |
| 62 |
- If the utilization is less than the reservation, the extra bandwidth is used for other host infrastructure traffic class types. |
| 63 |
- Reservation is not allowed to exceed the value of limit, if limit is set. |
| 64 |
- Unit is Mbits/sec. |
| 65 |
- Ignored unless version is "version3". |
| 66 |
- Amount of bandwidth resource that is guaranteed available to the host infrastructure traffic class. |
| 67 |
type: int |
| 68 |
default: 0 |
| 69 |
|
| 70 |
shares_level: |
| 71 |
description: |
| 72 |
- The allocation level |
| 73 |
- The level is a simplified view of shares. |
| 74 |
- Levels map to a pre-determined set of numeric values for shares. |
| 75 |
choices: [ "low", "normal", "high", "custom" ] |
| 76 |
type: str |
| 77 |
shares: |
| 78 |
description: |
| 79 |
- The number of shares allocated. |
| 80 |
- Ignored unless O(resources[].shares_level=custom). |
| 81 |
type: int |
| 82 |
required: false |
| 83 |
type: list |
| 84 |
default: [] |
| 85 |
elements: dict |
| 86 |
extends_documentation_fragment: |
| 87 |
- vmware.vmware.base_options |
| 88 |
|
| 89 |
''' |
| 90 |
|
| 91 |
RETURN = r''' |
| 92 |
dvswitch_nioc_status: |
| 93 |
description: |
| 94 |
- result of the changes |
| 95 |
returned: success |
| 96 |
type: str |
| 97 |
resources_changed: |
| 98 |
description: |
| 99 |
- list of resources which were changed |
| 100 |
returned: success |
| 101 |
type: list |
| 102 |
sample: [ "vmotion", "vsan" ] |
| 103 |
''' |
| 104 |
|
| 105 |
EXAMPLES = r''' |
| 106 |
- name: Enable NIOC |
| 107 |
community.vmware.vmware_dvswitch_nioc: |
| 108 |
hostname: '{{ vcenter_hostname }}' |
| 109 |
username: '{{ vcenter_username }}' |
| 110 |
password: '{{ vcenter_password }}' |
| 111 |
switch: dvSwitch |
| 112 |
version: version3 |
| 113 |
resources: |
| 114 |
- name: vmotion |
| 115 |
limit: -1 |
| 116 |
reservation: 128 |
| 117 |
shares_level: normal |
| 118 |
- name: vsan |
| 119 |
limit: -1 |
| 120 |
shares_level: custom |
| 121 |
shares: 99 |
| 122 |
reservation: 256 |
| 123 |
state: present |
| 124 |
delegate_to: localhost |
| 125 |
|
| 126 |
- name: Disable NIOC |
| 127 |
community.vmware.vmware_dvswitch_nioc: |
| 128 |
hostname: '{{ vcenter_hostname }}' |
| 129 |
username: '{{ vcenter_username }}' |
| 130 |
password: '{{ vcenter_password }}' |
| 131 |
switch: dvSwitch |
| 132 |
state: absent |
| 133 |
delegate_to: localhost |
| 134 |
''' |
| 135 |
|
| 136 |
try: |
| 137 |
from pyVmomi import vim, vmodl |
| 138 |
except ImportError: |
| 139 |
pass |
| 140 |
|
| 141 |
from ansible.module_utils.basic import AnsibleModule |
| 142 |
from ansible.module_utils.common.text.converters import to_native |
| 143 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import ( |
| 144 |
PyVmomi, |
| 145 |
find_dvs_by_name, |
| 146 |
wait_for_task) |
| 147 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 148 |
|
| 149 |
|
| 150 |
class VMwareDVSwitchNIOC(PyVmomi): |
| 151 |
|
| 152 |
def __init__(self, module): |
| 153 |
super(VMwareDVSwitchNIOC, self).__init__(module) |
| 154 |
self.dvs = None |
| 155 |
self.resource_changes = list() |
| 156 |
self.switch = module.params['switch'] |
| 157 |
self.version = module.params.get('version') |
| 158 |
self.state = module.params['state'] |
| 159 |
self.resources = module.params.get('resources') |
| 160 |
self.result = { |
| 161 |
'changed': False, |
| 162 |
'dvswitch_nioc_status': 'Unchanged', |
| 163 |
'resources_changed': list(), |
| 164 |
} |
| 165 |
|
| 166 |
def process_state(self): |
| 167 |
nioc_states = { |
| 168 |
'absent': { |
| 169 |
'present': self.state_disable_nioc, |
| 170 |
'absent': self.state_exit, |
| 171 |
}, |
| 172 |
'present': { |
| 173 |
'version': self.state_update_nioc_version, |
| 174 |
'update': self.state_update_nioc_resources, |
| 175 |
'present': self.state_exit, |
| 176 |
'absent': self.state_enable_nioc, |
| 177 |
} |
| 178 |
} |
| 179 |
nioc_states[self.state][self.check_nioc_state()]() |
| 180 |
self.state_exit() |
| 181 |
|
| 182 |
def state_exit(self): |
| 183 |
self.module.exit_json(**self.result) |
| 184 |
|
| 185 |
def state_disable_nioc(self): |
| 186 |
self.result['changed'] = True |
| 187 |
if not self.module.check_mode: |
| 188 |
self.set_nioc_enabled(False) |
| 189 |
self.result['dvswitch_nioc_status'] = 'Disabled NIOC' |
| 190 |
|
| 191 |
def state_enable_nioc(self): |
| 192 |
self.result['changed'] = True |
| 193 |
if not self.module.check_mode: |
| 194 |
self.set_nioc_enabled(True) |
| 195 |
self.set_nioc_version() |
| 196 |
self.result['dvswitch_nioc_status'] = "Enabled NIOC with version %s" % self.version |
| 197 |
|
| 198 |
# Check resource state and apply all required changes |
| 199 |
if self.check_resources() == 'update': |
| 200 |
self.set_nioc_resources(self.resource_changes) |
| 201 |
|
| 202 |
def state_update_nioc_version(self): |
| 203 |
self.result['changed'] = True |
| 204 |
if not self.module.check_mode: |
| 205 |
self.set_nioc_version() |
| 206 |
self.result['dvswitch_nioc_status'] = "Set NIOC to version %s" % self.version |
| 207 |
|
| 208 |
# Check resource state and apply all required changes |
| 209 |
if self.check_resources() == 'update': |
| 210 |
self.set_nioc_resources(self.resource_changes) |
| 211 |
|
| 212 |
def state_update_nioc_resources(self): |
| 213 |
self.result['changed'] = True |
| 214 |
if not self.module.check_mode: |
| 215 |
self.result['dvswitch_nioc_status'] = "Resource configuration modified" |
| 216 |
self.set_nioc_resources(self.resource_changes) |
| 217 |
|
| 218 |
def set_nioc_enabled(self, state): |
| 219 |
try: |
| 220 |
self.dvs.EnableNetworkResourceManagement(enable=state) |
| 221 |
except vim.fault.DvsFault as dvs_fault: |
| 222 |
self.module.fail_json(msg='DvsFault while setting NIOC enabled=%r: %s' % (state, to_native(dvs_fault.msg))) |
| 223 |
except vim.fault.DvsNotAuthorized as auth_fault: |
| 224 |
self.module.fail_json(msg='Not authorized to set NIOC enabled=%r: %s' % (state, to_native(auth_fault.msg))) |
| 225 |
except vmodl.fault.NotSupported as support_fault: |
| 226 |
self.module.fail_json(msg='NIOC not supported by DVS: %s' % to_native(support_fault.msg)) |
| 227 |
except vmodl.RuntimeFault as runtime_fault: |
| 228 |
self.module.fail_json(msg='RuntimeFault while setting NIOC enabled=%r: %s' % (state, to_native(runtime_fault.msg))) |
| 229 |
|
| 230 |
def set_nioc_version(self): |
| 231 |
upgrade_spec = vim.DistributedVirtualSwitch.ConfigSpec() |
| 232 |
upgrade_spec.configVersion = self.dvs.config.configVersion |
| 233 |
if not self.version: |
| 234 |
self.version = 'version2' |
| 235 |
upgrade_spec.networkResourceControlVersion = self.version |
| 236 |
|
| 237 |
try: |
| 238 |
task = self.dvs.ReconfigureDvs_Task(spec=upgrade_spec) |
| 239 |
wait_for_task(task) |
| 240 |
except vmodl.RuntimeFault as runtime_fault: |
| 241 |
self.module.fail_json(msg="RuntimeFault when setting NIOC version: %s " % to_native(runtime_fault.msg)) |
| 242 |
|
| 243 |
def check_nioc_state(self): |
| 244 |
self.dvs = find_dvs_by_name(self.content, self.switch) |
| 245 |
|
| 246 |
if self.dvs is None: |
| 247 |
self.module.fail_json(msg='DVS %s was not found.' % self.switch) |
| 248 |
else: |
| 249 |
if not self.dvs.config.networkResourceManagementEnabled: |
| 250 |
return 'absent' |
| 251 |
if self.version and self.dvs.config.networkResourceControlVersion != self.version: |
| 252 |
return 'version' |
| 253 |
|
| 254 |
# NIOC is enabled and the correct version, so return the state of the resources |
| 255 |
return self.check_resources() |
| 256 |
|
| 257 |
def check_resources(self): |
| 258 |
self.dvs = find_dvs_by_name(self.content, self.switch) |
| 259 |
if self.dvs is None: |
| 260 |
self.module.fail_json(msg="DVS named '%s' was not found" % self.switch) |
| 261 |
|
| 262 |
for resource in self.resources: |
| 263 |
if self.check_resource_state(resource) == 'update': |
| 264 |
self.resource_changes.append(resource) |
| 265 |
self.result['resources_changed'].append(resource['name']) |
| 266 |
|
| 267 |
if len(self.resource_changes) > 0: |
| 268 |
return 'update' |
| 269 |
return 'present' |
| 270 |
|
| 271 |
def check_resource_state(self, resource): |
| 272 |
resource_cfg = self.find_netioc_by_key(resource['name']) |
| 273 |
if resource_cfg is None: |
| 274 |
self.module.fail_json(msg="NetIOC resource named '%s' was not found" % resource['name']) |
| 275 |
|
| 276 |
rc = { |
| 277 |
"limit": resource_cfg.allocationInfo.limit, |
| 278 |
"shares_level": resource_cfg.allocationInfo.shares.level |
| 279 |
} |
| 280 |
if resource_cfg.allocationInfo.shares.level == 'custom': |
| 281 |
rc["shares"] = resource_cfg.allocationInfo.shares.shares |
| 282 |
if self.dvs.config.networkResourceControlVersion == "version3": |
| 283 |
rc["reservation"] = resource_cfg.allocationInfo.reservation |
| 284 |
|
| 285 |
for k, v in rc.items(): |
| 286 |
if k in resource and v != resource[k]: |
| 287 |
return 'update' |
| 288 |
return 'valid' |
| 289 |
|
| 290 |
def set_nioc_resources(self, resources): |
| 291 |
if self.dvs.config.networkResourceControlVersion == 'version3': |
| 292 |
self._update_version3_resources(resources) |
| 293 |
elif self.dvs.config.networkResourceControlVersion == 'version2': |
| 294 |
self._update_version2_resources(resources) |
| 295 |
|
| 296 |
def _update_version3_resources(self, resources): |
| 297 |
allocations = list() |
| 298 |
|
| 299 |
for resource in resources: |
| 300 |
allocation = vim.DistributedVirtualSwitch.HostInfrastructureTrafficResource() |
| 301 |
allocation.allocationInfo = vim.DistributedVirtualSwitch.HostInfrastructureTrafficResource.ResourceAllocation() |
| 302 |
allocation.key = resource['name'] |
| 303 |
if 'limit' in resource: |
| 304 |
allocation.allocationInfo.limit = resource['limit'] |
| 305 |
if 'reservation' in resource: |
| 306 |
allocation.allocationInfo.reservation = resource['reservation'] |
| 307 |
if 'shares_level' in resource: |
| 308 |
allocation.allocationInfo.shares = vim.SharesInfo() |
| 309 |
allocation.allocationInfo.shares.level = resource['shares_level'] |
| 310 |
if 'shares' in resource and resource['shares_level'] == 'custom': |
| 311 |
allocation.allocationInfo.shares.shares = resource['shares'] |
| 312 |
elif resource['shares_level'] == 'custom': |
| 313 |
self.module.fail_json( |
| 314 |
msg="Resource %s, shares_level set to custom but shares not specified" % resource['name'] |
| 315 |
) |
| 316 |
|
| 317 |
allocations.append(allocation) |
| 318 |
|
| 319 |
spec = vim.DistributedVirtualSwitch.ConfigSpec() |
| 320 |
spec.configVersion = self.dvs.config.configVersion |
| 321 |
spec.infrastructureTrafficResourceConfig = allocations |
| 322 |
|
| 323 |
task = self.dvs.ReconfigureDvs_Task(spec) |
| 324 |
wait_for_task(task) |
| 325 |
|
| 326 |
def _update_version2_resources(self, resources): |
| 327 |
allocations = list() |
| 328 |
|
| 329 |
for resource in resources: |
| 330 |
resource_cfg = self.find_netioc_by_key(resource['name']) |
| 331 |
allocation = vim.DVSNetworkResourcePoolConfigSpec() |
| 332 |
allocation.allocationInfo = vim.DVSNetworkResourcePoolAllocationInfo() |
| 333 |
allocation.key = resource['name'] |
| 334 |
allocation.configVersion = resource_cfg.configVersion |
| 335 |
if 'limit' in resource: |
| 336 |
allocation.allocationInfo.limit = resource['limit'] |
| 337 |
if 'shares_level' in resource: |
| 338 |
allocation.allocationInfo.shares = vim.SharesInfo() |
| 339 |
allocation.allocationInfo.shares.level = resource['shares_level'] |
| 340 |
if 'shares' in resource and resource['shares_level'] == 'custom': |
| 341 |
allocation.allocationInfo.shares.shares = resource['shares'] |
| 342 |
|
| 343 |
allocations.append(allocation) |
| 344 |
|
| 345 |
self.dvs.UpdateNetworkResourcePool(allocations) |
| 346 |
|
| 347 |
def find_netioc_by_key(self, resource_name): |
| 348 |
config = None |
| 349 |
if self.dvs.config.networkResourceControlVersion == "version3": |
| 350 |
config = self.dvs.config.infrastructureTrafficResourceConfig |
| 351 |
elif self.dvs.config.networkResourceControlVersion == "version2": |
| 352 |
config = self.dvs.networkResourcePool |
| 353 |
|
| 354 |
for obj in config: |
| 355 |
if obj.key == resource_name: |
| 356 |
return obj |
| 357 |
return None |
| 358 |
|
| 359 |
|
| 360 |
def main(): |
| 361 |
argument_spec = base_argument_spec() |
| 362 |
|
| 363 |
argument_spec.update( |
| 364 |
dict( |
| 365 |
switch=dict(required=True, type='str', aliases=['dvswitch']), |
| 366 |
version=dict(type='str', choices=['version2', 'version3']), |
| 367 |
state=dict(default='present', choices=['present', 'absent'], type='str'), |
| 368 |
resources=dict( |
| 369 |
type='list', |
| 370 |
default=list(), |
| 371 |
elements='dict', |
| 372 |
options=dict( |
| 373 |
name=dict( |
| 374 |
type='str', |
| 375 |
required=True, |
| 376 |
choices=[ |
| 377 |
'faultTolerance', |
| 378 |
'hbr', |
| 379 |
'iSCSI', |
| 380 |
'management', |
| 381 |
'nfs', |
| 382 |
'vdp', |
| 383 |
'virtualMachine', |
| 384 |
'vmotion', |
| 385 |
'vsan', |
| 386 |
'backupNfc', |
| 387 |
'nvmetcp' |
| 388 |
] |
| 389 |
), |
| 390 |
limit=dict(type='int', default=-1), |
| 391 |
shares_level=dict( |
| 392 |
type='str', |
| 393 |
required=False, |
| 394 |
choices=[ |
| 395 |
'low', |
| 396 |
'normal', |
| 397 |
'high', |
| 398 |
'custom' |
| 399 |
] |
| 400 |
), |
| 401 |
shares=dict(type='int', required=False), |
| 402 |
reservation=dict(type='int', default=0) |
| 403 |
) |
| 404 |
), |
| 405 |
) |
| 406 |
) |
| 407 |
|
| 408 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 409 |
|
| 410 |
try: |
| 411 |
vmware_dvswitch_nioc = VMwareDVSwitchNIOC(module) |
| 412 |
vmware_dvswitch_nioc.process_state() |
| 413 |
except vmodl.RuntimeFault as runtime_fault: |
| 414 |
module.fail_json(msg=to_native(runtime_fault.msg)) |
| 415 |
except vmodl.MethodFault as method_fault: |
| 416 |
module.fail_json(msg=to_native(method_fault.msg)) |
| 417 |
except Exception as e: |
| 418 |
module.fail_json(msg=to_native(e)) |
| 419 |
|
| 420 |
|
| 421 |
if __name__ == '__main__': |
| 422 |
main() |