Debian Codegraph @

ansible_collections/community/vmware/plugins/modules/vmware_guest_vgpu_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Ansible Project
5 # Copyright: (c) 2018, Diane Wang <[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
11 __metaclass__ = type
12
13 DOCUMENTATION = r"""
14 ---
15 module: vmware_guest_vgpu_info
16 version_added: '3.3.0'
17 short_description: Gather information about vGPU profiles of the specified virtual machine in the given vCenter infrastructure
18 description:
19 - This module is used to gather metadata about vGPU profiles of the given virtual machine.
20 author:
21 - Jared Priddy (@jdptechnc)
22 options:
23 name:
24 description:
25 - Name of the virtual machine.
26 - This is a required parameter, if parameter O(uuid) or O(moid) is not supplied.
27 type: str
28 uuid:
29 description:
30 - UUID of the instance to gather facts if known, this is VMware's unique identifier.
31 - This is a required parameter, if parameter O(name) or O(moid) is not supplied.
32 type: str
33 moid:
34 description:
35 - Managed Object ID of the instance to manage if known, this is a unique identifier only within a single vCenter instance.
36 - This is required if O(name) or O(uuid) is not supplied.
37 type: str
38 folder:
39 description:
40 - Destination folder, absolute or relative path to find an existing guest.
41 - This is a required parameter, only if multiple VMs are found with same name.
42 - The folder should include the datacenter. ESXi server's datacenter is ha-datacenter.
43 - 'Examples:'
44 - ' folder: /ha-datacenter/vm'
45 - ' folder: ha-datacenter/vm'
46 - ' folder: /datacenter1/vm'
47 - ' folder: datacenter1/vm'
48 - ' folder: /datacenter1/vm/folder1'
49 - ' folder: datacenter1/vm/folder1'
50 - ' folder: /folder1/datacenter1/vm'
51 - ' folder: folder1/datacenter1/vm'
52 - ' folder: /folder1/datacenter1/vm/folder2'
53 type: str
54 datacenter:
55 default: ha-datacenter
56 description:
57 - The datacenter name to which virtual machine belongs to.
58 type: str
59 use_instance_uuid:
60 description:
61 - Whether to use the VMware instance UUID rather than the BIOS UUID.
62 default: false
63 type: bool
64 extends_documentation_fragment:
65 - vmware.vmware.base_options
66 """
67
68 EXAMPLES = r"""
69 - name: Gather information about vGPU profiles of a VM
70 community.vmware.vmware_guest_vgpu_info:
71 hostname: "{{ vcenter_hostname }}"
72 username: "{{ vcenter_username }}"
73 password: "{{ vcenter_password }}"
74 datacenter: "{{ datacenter_name }}"
75 validate_certs: false
76 name: UbuntuTest
77 delegate_to: localhost
78 register: vgpu_info
79
80 """
81
82 RETURN = r"""
83 vgpu_info:
84 description: metadata about the virtual machine's vGPU profiles
85 returned: always
86 type: list
87 sample: {
88 "vgpu": [
89 {
90 "Controller_Key": 100,
91 "Key": 13000,
92 "Label": "PCI device 0",
93 "Summary": "NVIDIA GRID vGPU grid_m10-8q",
94 "Unit_Number": 18,
95 "Vgpu": "grid_m10-8q"
96 }
97 ]
98 }
99 """
100
101
102 from ansible.module_utils.basic import AnsibleModule
103 from ansible.module_utils.common.text.converters import to_text
104 from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi
105 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
106
107
108 class PyVmomiHelper(PyVmomi):
109 def __init__(self, module):
110 super(PyVmomiHelper, self).__init__(module)
111
112 def gather_vgpu_profile_facts(self, vm_obj):
113 """
114 Gather facts about VM's vGPU profile settings
115 Args:
116 vm_obj: Managed object of virtual machine
117 Returns: list of vGPU profiles with facts
118 """
119 vgpu_info = []
120 for vgpu_VirtualDevice_obj in vm_obj.config.hardware.device:
121 if hasattr(vgpu_VirtualDevice_obj.backing, "vgpu"):
122 vgpu = dict(
123 Vgpu=vgpu_VirtualDevice_obj.backing.vgpu,
124 Key=vgpu_VirtualDevice_obj.key,
125 Summary=vgpu_VirtualDevice_obj.deviceInfo.summary,
126 Label=vgpu_VirtualDevice_obj.deviceInfo.label,
127 Unit_Number=vgpu_VirtualDevice_obj.unitNumber,
128 Controller_Key=vgpu_VirtualDevice_obj.controllerKey,
129 )
130 vgpu_info.append(vgpu)
131 return vgpu_info
132
133
134 def main():
135 argument_spec = base_argument_spec()
136 argument_spec.update(
137 name=dict(type="str"),
138 uuid=dict(type="str"),
139 use_instance_uuid=dict(type="bool", default=False),
140 moid=dict(type="str"),
141 folder=dict(type="str"),
142 datacenter=dict(type="str", default="ha-datacenter"),
143 )
144
145 module = AnsibleModule(
146 argument_spec=argument_spec,
147 required_one_of=[["name", "uuid", "moid"]],
148 supports_check_mode=True,
149 )
150
151 pyv = PyVmomiHelper(module)
152 vm = pyv.get_vm()
153
154 if not vm:
155 vm_id = (
156 module.params.get("uuid")
157 or module.params.get("name")
158 or module.params.get("moid")
159 )
160 module.fail_json(
161 msg="Unable to gather vGPU information for non-existing VM %s" % vm_id
162 )
163 else:
164 try:
165 module.exit_json(vgpu=pyv.gather_vgpu_profile_facts(vm))
166 except Exception as exc:
167 module.fail_json(msg="Failed to gather information with exception : %s" % to_text(exc))
168
169
170 if __name__ == "__main__":
171 main()