| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2021, sky-joker |
| 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_object_custom_attributes_info |
| 14 |
short_description: Gather custom attributes of an object |
| 15 |
author: |
| 16 |
- sky-joker (@sky-joker) |
| 17 |
description: |
| 18 |
- This module can be gathered custom attributes of an object. |
| 19 |
notes: |
| 20 |
- Supports C(check_mode). |
| 21 |
options: |
| 22 |
object_type: |
| 23 |
description: |
| 24 |
- Type of an object to work with. |
| 25 |
type: str |
| 26 |
choices: |
| 27 |
- Datacenter |
| 28 |
- Cluster |
| 29 |
- HostSystem |
| 30 |
- ResourcePool |
| 31 |
- Folder |
| 32 |
- VirtualMachine |
| 33 |
- DistributedVirtualSwitch |
| 34 |
- DistributedVirtualPortgroup |
| 35 |
- Datastore |
| 36 |
required: true |
| 37 |
object_name: |
| 38 |
description: |
| 39 |
- Name of the object to work with. |
| 40 |
type: str |
| 41 |
aliases: |
| 42 |
- name |
| 43 |
moid: |
| 44 |
description: |
| 45 |
- Managed Object ID of the instance to get if known, this is a unique identifier only within a single vCenter instance. |
| 46 |
- This is required if O(object_name) is not supplied. |
| 47 |
type: str |
| 48 |
extends_documentation_fragment: |
| 49 |
- vmware.vmware.base_options |
| 50 |
""" |
| 51 |
|
| 52 |
EXAMPLES = r""" |
| 53 |
- name: Gather custom attributes of a virtual machine |
| 54 |
community.vmware.vmware_object_custom_attributes_info: |
| 55 |
hostname: "{{ vcenter_hostname }}" |
| 56 |
username: "{{ vcenter_username }}" |
| 57 |
password: "{{ vcenter_password }}" |
| 58 |
validate_certs: false |
| 59 |
object_type: VirtualMachine |
| 60 |
object_name: "{{ object_name }}" |
| 61 |
register: vm_attributes |
| 62 |
|
| 63 |
- name: Gather custom attributes of a virtual machine with moid |
| 64 |
community.vmware.vmware_object_custom_attributes_info: |
| 65 |
hostname: "{{ vcenter_hostname }}" |
| 66 |
username: "{{ vcenter_username }}" |
| 67 |
password: "{{ vcenter_password }}" |
| 68 |
validate_certs: false |
| 69 |
object_type: VirtualMachine |
| 70 |
moid: "{{ moid }}" |
| 71 |
register: vm_attributes |
| 72 |
""" |
| 73 |
|
| 74 |
RETURN = r""" |
| 75 |
custom_attributes: |
| 76 |
description: list of custom attributes of an object. |
| 77 |
returned: always |
| 78 |
type: list |
| 79 |
sample: >- |
| 80 |
[ |
| 81 |
{ |
| 82 |
"attribute": "example01", |
| 83 |
"key": 132, |
| 84 |
"type": "VirtualMachine", |
| 85 |
"value": "10" |
| 86 |
}, |
| 87 |
{ |
| 88 |
"attribute": "example02", |
| 89 |
"key": 131, |
| 90 |
"type": "VirtualMachine", |
| 91 |
"value": "20" |
| 92 |
}, |
| 93 |
{ |
| 94 |
"attribute": "example03", |
| 95 |
"key": 130, |
| 96 |
"type": "VirtualMachine", |
| 97 |
"value": null |
| 98 |
} |
| 99 |
] |
| 100 |
""" |
| 101 |
|
| 102 |
try: |
| 103 |
from pyVmomi import vim |
| 104 |
except ImportError: |
| 105 |
pass |
| 106 |
|
| 107 |
from ansible.module_utils.basic import AnsibleModule |
| 108 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi, find_obj |
| 109 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 110 |
|
| 111 |
|
| 112 |
class VmwareCustomAttributesInfo(PyVmomi): |
| 113 |
def __init__(self, module): |
| 114 |
super(VmwareCustomAttributesInfo, self).__init__(module) |
| 115 |
|
| 116 |
if not self.is_vcenter(): |
| 117 |
self.module.fail_json(msg="You have to connect to a vCenter server!") |
| 118 |
|
| 119 |
self.object_type = self.params['object_type'] |
| 120 |
self.object_name = self.params['object_name'] |
| 121 |
self.moid = self.params['moid'] |
| 122 |
|
| 123 |
self.valid_object_types = { |
| 124 |
'Datacenter': vim.Datacenter, |
| 125 |
'Cluster': vim.ClusterComputeResource, |
| 126 |
'HostSystem': vim.HostSystem, |
| 127 |
'ResourcePool': vim.ResourcePool, |
| 128 |
'Folder': vim.Folder, |
| 129 |
'VirtualMachine': vim.VirtualMachine, |
| 130 |
'DistributedVirtualSwitch': vim.DistributedVirtualSwitch, |
| 131 |
'DistributedVirtualPortgroup': vim.DistributedVirtualPortgroup, |
| 132 |
'Datastore': vim.Datastore |
| 133 |
} |
| 134 |
|
| 135 |
def execute(self): |
| 136 |
result = {'changed': False} |
| 137 |
|
| 138 |
if self.object_name: |
| 139 |
obj = find_obj(self.content, [self.valid_object_types[self.object_type]], self.object_name) |
| 140 |
elif self.moid: |
| 141 |
obj = self.find_obj_by_moid(self.object_type, self.moid) |
| 142 |
if not obj: |
| 143 |
self.module.fail_json(msg="can't find the object: %s" % self.object_name if self.object_name else self.moid) |
| 144 |
|
| 145 |
custom_attributes = [] |
| 146 |
available_fields = {} |
| 147 |
for available_custom_attribute in obj.availableField: |
| 148 |
available_fields.update({ |
| 149 |
available_custom_attribute.key: { |
| 150 |
'name': available_custom_attribute.name, |
| 151 |
'type': available_custom_attribute.managedObjectType |
| 152 |
} |
| 153 |
}) |
| 154 |
|
| 155 |
custom_values = {} |
| 156 |
for custom_value in obj.customValue: |
| 157 |
custom_values.update({ |
| 158 |
custom_value.key: custom_value.value |
| 159 |
}) |
| 160 |
|
| 161 |
for key, value in available_fields.items(): |
| 162 |
attribute_result = { |
| 163 |
'attribute': value['name'], |
| 164 |
'type': self.to_json(value['type']).replace('vim.', '') if value['type'] is not None else 'Global', |
| 165 |
'key': key, |
| 166 |
'value': None |
| 167 |
} |
| 168 |
|
| 169 |
if key in custom_values: |
| 170 |
attribute_result['value'] = custom_values[key] |
| 171 |
|
| 172 |
custom_attributes.append(attribute_result) |
| 173 |
|
| 174 |
result['custom_attributes'] = custom_attributes |
| 175 |
self.module.exit_json(**result) |
| 176 |
|
| 177 |
|
| 178 |
def main(): |
| 179 |
argument_spec = base_argument_spec() |
| 180 |
argument_spec.update( |
| 181 |
object_type=dict(type='str', required=True, choices=[ |
| 182 |
'Datacenter', |
| 183 |
'Cluster', |
| 184 |
'HostSystem', |
| 185 |
'ResourcePool', |
| 186 |
'Folder', |
| 187 |
'VirtualMachine', |
| 188 |
'DistributedVirtualSwitch', |
| 189 |
'DistributedVirtualPortgroup', |
| 190 |
'Datastore' |
| 191 |
]), |
| 192 |
object_name=dict(type='str', aliases=['name']), |
| 193 |
moid=dict(type='str') |
| 194 |
) |
| 195 |
|
| 196 |
module = AnsibleModule(argument_spec=argument_spec, |
| 197 |
mutually_exclusive=[ |
| 198 |
['object_name', 'moid'] |
| 199 |
], |
| 200 |
required_one_of=[ |
| 201 |
['object_name', 'moid'] |
| 202 |
], |
| 203 |
supports_check_mode=True) |
| 204 |
|
| 205 |
vmware_custom_attributes_info = VmwareCustomAttributesInfo(module) |
| 206 |
vmware_custom_attributes_info.execute() |
| 207 |
|
| 208 |
|
| 209 |
if __name__ == "__main__": |
| 210 |
main() |