| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2022, Ansible Project |
| 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_custom_attribute |
| 15 |
version_added: '3.2.0' |
| 16 |
short_description: Manage custom attributes definitions |
| 17 |
description: |
| 18 |
- This module can be used to add and remove custom attributes definitions for various vSphere objects. |
| 19 |
author: |
| 20 |
- Mario Lenz (@mariolenz) |
| 21 |
options: |
| 22 |
custom_attribute: |
| 23 |
description: |
| 24 |
- Name of the custom attribute. |
| 25 |
required: true |
| 26 |
type: str |
| 27 |
object_type: |
| 28 |
description: |
| 29 |
- Type of the object the custom attribute is associated with. |
| 30 |
type: str |
| 31 |
choices: |
| 32 |
- Cluster |
| 33 |
- Datacenter |
| 34 |
- Datastore |
| 35 |
- DistributedVirtualPortgroup |
| 36 |
- DistributedVirtualSwitch |
| 37 |
- Folder |
| 38 |
- Global |
| 39 |
- HostSystem |
| 40 |
- ResourcePool |
| 41 |
- VirtualMachine |
| 42 |
required: true |
| 43 |
state: |
| 44 |
description: |
| 45 |
- Manage definition of custom attributes. |
| 46 |
- If set to V(present) and definition not present, then custom attribute definition is created. |
| 47 |
- If set to V(present) and definition is present, then no action taken. |
| 48 |
- If set to V(absent) and definition is present, then custom attribute definition is removed. |
| 49 |
- If set to V(absent) and definition is absent, then no action taken. |
| 50 |
default: 'present' |
| 51 |
choices: ['present', 'absent'] |
| 52 |
type: str |
| 53 |
extends_documentation_fragment: |
| 54 |
- vmware.vmware.base_options |
| 55 |
|
| 56 |
''' |
| 57 |
|
| 58 |
EXAMPLES = r''' |
| 59 |
- name: Add VM Custom Attribute Definition |
| 60 |
community.vmware.vmware_custom_attribute: |
| 61 |
hostname: "{{ vcenter_hostname }}" |
| 62 |
username: "{{ vcenter_username }}" |
| 63 |
password: "{{ vcenter_password }}" |
| 64 |
state: present |
| 65 |
object_type: VirtualMachine |
| 66 |
custom_attribute: custom_attr_def_1 |
| 67 |
delegate_to: localhost |
| 68 |
register: defs |
| 69 |
|
| 70 |
- name: Remove VM Custom Attribute Definition |
| 71 |
community.vmware.vmware_custom_attribute: |
| 72 |
hostname: "{{ vcenter_hostname }}" |
| 73 |
username: "{{ vcenter_username }}" |
| 74 |
password: "{{ vcenter_password }}" |
| 75 |
state: absent |
| 76 |
object_type: VirtualMachine |
| 77 |
custom_attribute: custom_attr_def_1 |
| 78 |
delegate_to: localhost |
| 79 |
register: defs |
| 80 |
''' |
| 81 |
|
| 82 |
RETURN = r''' |
| 83 |
''' |
| 84 |
|
| 85 |
from ansible.module_utils.basic import AnsibleModule |
| 86 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 87 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 88 |
|
| 89 |
try: |
| 90 |
from pyVmomi import vim |
| 91 |
except ImportError: |
| 92 |
pass |
| 93 |
|
| 94 |
|
| 95 |
class CustomAttribute(PyVmomi): |
| 96 |
def __init__(self, module): |
| 97 |
super(CustomAttribute, self).__init__(module) |
| 98 |
|
| 99 |
if not self.is_vcenter(): |
| 100 |
self.module.fail_json(msg="You have to connect to a vCenter server!") |
| 101 |
|
| 102 |
object_types_map = { |
| 103 |
'Cluster': vim.ClusterComputeResource, |
| 104 |
'Datacenter': vim.Datacenter, |
| 105 |
'Datastore': vim.Datastore, |
| 106 |
'DistributedVirtualPortgroup': vim.DistributedVirtualPortgroup, |
| 107 |
'DistributedVirtualSwitch': vim.DistributedVirtualSwitch, |
| 108 |
'Folder': vim.Folder, |
| 109 |
'Global': None, |
| 110 |
'HostSystem': vim.HostSystem, |
| 111 |
'ResourcePool': vim.ResourcePool, |
| 112 |
'VirtualMachine': vim.VirtualMachine |
| 113 |
} |
| 114 |
|
| 115 |
self.object_type = object_types_map[self.params['object_type']] |
| 116 |
|
| 117 |
def remove_custom_def(self, field): |
| 118 |
changed = False |
| 119 |
for x in self.custom_field_mgr: |
| 120 |
if x.name == field and x.managedObjectType == self.object_type: |
| 121 |
changed = True |
| 122 |
if not self.module.check_mode: |
| 123 |
self.content.customFieldsManager.RemoveCustomFieldDef(key=x.key) |
| 124 |
break |
| 125 |
return {'changed': changed, 'failed': False} |
| 126 |
|
| 127 |
def add_custom_def(self, field): |
| 128 |
changed = False |
| 129 |
found = False |
| 130 |
for x in self.custom_field_mgr: |
| 131 |
if x.name == field and x.managedObjectType == self.object_type: |
| 132 |
found = True |
| 133 |
break |
| 134 |
|
| 135 |
if not found: |
| 136 |
changed = True |
| 137 |
if not self.module.check_mode: |
| 138 |
self.content.customFieldsManager.AddFieldDefinition(name=field, moType=self.object_type) |
| 139 |
return {'changed': changed, 'failed': False} |
| 140 |
|
| 141 |
|
| 142 |
def main(): |
| 143 |
argument_spec = base_argument_spec() |
| 144 |
argument_spec.update( |
| 145 |
custom_attribute=dict(type='str', no_log=False, required=True), |
| 146 |
object_type=dict(type='str', required=True, choices=[ |
| 147 |
'Cluster', |
| 148 |
'Datacenter', |
| 149 |
'Datastore', |
| 150 |
'DistributedVirtualPortgroup', |
| 151 |
'DistributedVirtualSwitch', |
| 152 |
'Folder', |
| 153 |
'Global', |
| 154 |
'HostSystem', |
| 155 |
'ResourcePool', |
| 156 |
'VirtualMachine' |
| 157 |
]), |
| 158 |
state=dict(type='str', default='present', choices=['absent', 'present']), |
| 159 |
) |
| 160 |
module = AnsibleModule( |
| 161 |
argument_spec=argument_spec, |
| 162 |
supports_check_mode=True, |
| 163 |
) |
| 164 |
|
| 165 |
pyv = CustomAttribute(module) |
| 166 |
results = dict(changed=False, custom_attribute_defs=list()) |
| 167 |
if module.params['state'] == "present": |
| 168 |
results = pyv.add_custom_def(module.params['custom_attribute']) |
| 169 |
elif module.params['state'] == "absent": |
| 170 |
results = pyv.remove_custom_def(module.params['custom_attribute']) |
| 171 |
|
| 172 |
module.exit_json(**results) |
| 173 |
|
| 174 |
|
| 175 |
if __name__ == '__main__': |
| 176 |
main() |