| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2023, Alexander Nikitin ([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 |
DOCUMENTATION = r''' |
| 12 |
--- |
| 13 |
module: vmware_host_graphics |
| 14 |
version_added: '3.10.0' |
| 15 |
short_description: Manage Host Graphic Settings |
| 16 |
description: |
| 17 |
- This module can be used to manage Host Graphic Settings |
| 18 |
author: |
| 19 |
- Alexander Nikitin (@ihumster) |
| 20 |
options: |
| 21 |
cluster_name: |
| 22 |
description: |
| 23 |
- Name of cluster. |
| 24 |
- All host system from given cluster used to manage Host Graphic Settings. |
| 25 |
- Required parameter, if O(esxi_hostname) is not set. |
| 26 |
type: str |
| 27 |
esxi_hostname: |
| 28 |
description: |
| 29 |
- List of ESXi hostname to manage Host Graphic Settings. |
| 30 |
- Required parameter, if O(cluster_name) is not set. |
| 31 |
type: list |
| 32 |
elements: str |
| 33 |
graphic_type: |
| 34 |
description: |
| 35 |
- Default graphics type |
| 36 |
default: shared |
| 37 |
choices: [ shared, sharedDirect ] |
| 38 |
type: str |
| 39 |
assigment_policy: |
| 40 |
description: |
| 41 |
- Shared passthrough GPU assignment policy |
| 42 |
default: performance |
| 43 |
choices: [ consolidation, performance ] |
| 44 |
type: str |
| 45 |
restart_xorg: |
| 46 |
description: |
| 47 |
- Restart X.Org Server after change any parameter ( O(graphic_type) or O(assigment_policy) ) |
| 48 |
default: False |
| 49 |
type: bool |
| 50 |
extends_documentation_fragment: |
| 51 |
- vmware.vmware.base_options |
| 52 |
|
| 53 |
''' |
| 54 |
|
| 55 |
EXAMPLES = r''' |
| 56 |
- name: Change Host Graphics Settings |
| 57 |
community.vmware.vmware_host_graphics: |
| 58 |
hostname: '{{ vcenter_hostname }}' |
| 59 |
username: '{{ vcenter_username }}' |
| 60 |
password: '{{ vcenter_password }}' |
| 61 |
esxi_hostname: '{{ esxi_hostname }}' |
| 62 |
graphic_type: sharedDirect |
| 63 |
assigment_policy: consolidation |
| 64 |
delegate_to: localhost |
| 65 |
''' |
| 66 |
|
| 67 |
RETURN = r''' |
| 68 |
results: |
| 69 |
description: |
| 70 |
- data about host system graphics settings. |
| 71 |
returned: always |
| 72 |
type: dict |
| 73 |
sample: { |
| 74 |
"changed": true, |
| 75 |
"esxi01": { |
| 76 |
"changed": false, |
| 77 |
"msg": "All Host Graphics Settings already configured" |
| 78 |
}, |
| 79 |
"esxi02": { |
| 80 |
"changed": true, |
| 81 |
"msg": "New host graphics settings changed to: hostDefaultGraphicsType = 'shared', sharedPassthruAssignmentPolicy = 'performance'.X.Org was restarted" |
| 82 |
} |
| 83 |
} |
| 84 |
''' |
| 85 |
|
| 86 |
try: |
| 87 |
from pyVmomi import vim |
| 88 |
except ImportError: |
| 89 |
pass |
| 90 |
|
| 91 |
from ansible.module_utils.basic import AnsibleModule |
| 92 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 93 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 94 |
from ansible.module_utils.common.text.converters import to_native |
| 95 |
|
| 96 |
|
| 97 |
class VMwareHostGraphicSettings(PyVmomi): |
| 98 |
""" Main class for configuring Host Graphics Settings """ |
| 99 |
def __init__(self, module): |
| 100 |
super(VMwareHostGraphicSettings, self).__init__(module) |
| 101 |
self.graphic_type = self.params['graphic_type'] |
| 102 |
self.assigment_policy = self.params['assigment_policy'] |
| 103 |
self.restart_xorg = self.params['restart_xorg'] |
| 104 |
self.results = {"changed": False} |
| 105 |
esxi_hostname = self.params['esxi_hostname'] |
| 106 |
cluster_name = self.params['cluster_name'] |
| 107 |
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_hostname) |
| 108 |
if self.hosts is None: |
| 109 |
self.module.fail_json(msg="Failed to find host system.") |
| 110 |
|
| 111 |
def ensure(self): |
| 112 |
""" |
| 113 |
Function to manage host graphics |
| 114 |
""" |
| 115 |
for host in self.hosts: |
| 116 |
self.results[host.name] = dict() |
| 117 |
if host.runtime.connectionState == "connected": |
| 118 |
hgm = host.configManager.graphicsManager |
| 119 |
hsm = host.configManager.serviceSystem |
| 120 |
changed = False |
| 121 |
current_config = hgm.graphicsConfig |
| 122 |
if current_config.hostDefaultGraphicsType != self.graphic_type: |
| 123 |
changed = True |
| 124 |
current_config.hostDefaultGraphicsType = self.graphic_type |
| 125 |
if current_config.sharedPassthruAssignmentPolicy != self.assigment_policy: |
| 126 |
changed = True |
| 127 |
current_config.sharedPassthruAssignmentPolicy = self.assigment_policy |
| 128 |
|
| 129 |
if changed: |
| 130 |
if self.module.check_mode: |
| 131 |
not_world = '' if self.restart_xorg else 'not ' |
| 132 |
self.results[host.name]['changed'] = False |
| 133 |
self.results[host.name]['msg'] = f"New host graphics settings will be changed to: hostDefaultGraphicsType = \ |
| 134 |
'{current_config.hostDefaultGraphicsType}', sharedPassthruAssignmentPolicy = \ |
| 135 |
'{current_config.sharedPassthruAssignmentPolicy}'. \ |
| 136 |
X.Org will {not_world}be restarted." |
| 137 |
else: |
| 138 |
try: |
| 139 |
hgm.UpdateGraphicsConfig(current_config) |
| 140 |
if self.restart_xorg: |
| 141 |
hsm.RestartService('xorg') |
| 142 |
xorg_status = 'was restarted' if self.restart_xorg else 'was not restarted.' |
| 143 |
self.results['changed'] = True |
| 144 |
self.results[host.name]['changed'] = True |
| 145 |
self.results[host.name]['msg'] = f"New host graphics settings changed to: hostDefaultGraphicsType = \ |
| 146 |
'{current_config.hostDefaultGraphicsType}', sharedPassthruAssignmentPolicy = \ |
| 147 |
'{current_config.sharedPassthruAssignmentPolicy}'. \ |
| 148 |
X.Org {xorg_status}" |
| 149 |
except vim.fault.HostConfigFault as config_fault: |
| 150 |
self.module.fail_json( |
| 151 |
msg=f"Failed to configure host graphics settings for host {host.name} due to : {to_native(config_fault.msg)}" |
| 152 |
) |
| 153 |
else: |
| 154 |
self.results[host.name]['changed'] = False |
| 155 |
self.results[host.name]['msg'] = 'All Host Graphics Settings have already been configured' |
| 156 |
else: |
| 157 |
self.results[host.name]['changed'] = False |
| 158 |
self.results[host.name]['msg'] = f"Host {host.name} is disconnected and cannot be changed" |
| 159 |
|
| 160 |
self.module.exit_json(**self.results) |
| 161 |
|
| 162 |
|
| 163 |
def main(): |
| 164 |
""" Main module method""" |
| 165 |
argument_spec = base_argument_spec() |
| 166 |
argument_spec.update( |
| 167 |
cluster_name=dict(type='str'), |
| 168 |
esxi_hostname=dict(type='list', elements='str'), |
| 169 |
graphic_type=dict(type='str', default='shared', choices=['shared', 'sharedDirect']), |
| 170 |
assigment_policy=dict(type='str', default='performance', choices=['consolidation', 'performance']), |
| 171 |
restart_xorg=dict(type='bool', default=False), |
| 172 |
) |
| 173 |
|
| 174 |
module = AnsibleModule( |
| 175 |
argument_spec=argument_spec, |
| 176 |
supports_check_mode=True, |
| 177 |
required_one_of=[ |
| 178 |
['cluster_name', 'esxi_hostname'], |
| 179 |
], |
| 180 |
) |
| 181 |
|
| 182 |
vmware_host_graphics = VMwareHostGraphicSettings(module) |
| 183 |
vmware_host_graphics.ensure() |
| 184 |
|
| 185 |
|
| 186 |
if __name__ == "__main__": |
| 187 |
main() |