| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2020, Ansible Project |
| 5 |
# Copyright: (c) 2020, Dustin Scott <[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 |
DOCUMENTATION = r''' |
| 13 |
--- |
| 14 |
module: vmware_vm_storage_policy |
| 15 |
short_description: Create vSphere storage policies |
| 16 |
description: |
| 17 |
- A vSphere storage policy defines metadata that describes storage requirements |
| 18 |
for virtual machines and storage capabilities of storage providers. |
| 19 |
- Currently, only tag-based storage policy creation is supported. |
| 20 |
author: |
| 21 |
- Dustin Scott (@scottd018) |
| 22 |
options: |
| 23 |
name: |
| 24 |
description: |
| 25 |
- Name of the storage policy to create, update, or delete. |
| 26 |
required: true |
| 27 |
type: str |
| 28 |
description: |
| 29 |
description: |
| 30 |
- Description of the storage policy to create or update. |
| 31 |
- This parameter is ignored when O(state=absent). |
| 32 |
type: str |
| 33 |
required: false |
| 34 |
tag_category: |
| 35 |
description: |
| 36 |
- Name of the pre-existing tag category to assign to the storage policy. |
| 37 |
- This parameter is ignored when O(state=absent). |
| 38 |
- This parameter is required when O(state=present). |
| 39 |
required: false |
| 40 |
type: str |
| 41 |
tag_name: |
| 42 |
description: |
| 43 |
- Name of the pre-existing tag to assign to the storage policy. |
| 44 |
- This parameter is ignored when O(state=absent). |
| 45 |
- This parameter is required when O(state=present). |
| 46 |
required: false |
| 47 |
type: str |
| 48 |
tag_affinity: |
| 49 |
description: |
| 50 |
- If set to V(true), the storage policy enforces that virtual machines require the existence of a tag for datastore placement. |
| 51 |
- If set to V(false), the storage policy enforces that virtual machines require the absence of a tag for datastore placement. |
| 52 |
- This parameter is ignored when O(state=absent). |
| 53 |
required: false |
| 54 |
type: bool |
| 55 |
default: true |
| 56 |
state: |
| 57 |
description: |
| 58 |
- State of storage policy. |
| 59 |
- If set to V(present), the storage policy is created. |
| 60 |
- If set to V(absent), the storage policy is deleted. |
| 61 |
default: present |
| 62 |
choices: [ absent, present ] |
| 63 |
type: str |
| 64 |
extends_documentation_fragment: |
| 65 |
- vmware.vmware.base_options |
| 66 |
''' |
| 67 |
|
| 68 |
EXAMPLES = r''' |
| 69 |
- name: Create or update a vSphere tag-based storage policy |
| 70 |
community.vmware.vmware_vm_storage_policy: |
| 71 |
hostname: '{{ vcenter_hostname }}' |
| 72 |
username: '{{ vcenter_username }}' |
| 73 |
password: '{{ vcenter_password }}' |
| 74 |
name: "vSphere storage policy" |
| 75 |
description: "vSphere storage performance policy" |
| 76 |
tag_category: "performance_tier" |
| 77 |
tag_name: "gold" |
| 78 |
tag_affinity: true |
| 79 |
state: "present" |
| 80 |
delegate_to: localhost |
| 81 |
|
| 82 |
- name: Remove a vSphere tag-based storage policy |
| 83 |
community.vmware.vmware_vm_storage_policy: |
| 84 |
hostname: '{{ vcenter_hostname }}' |
| 85 |
username: '{{ vcenter_username }}' |
| 86 |
password: '{{ vcenter_password }}' |
| 87 |
name: "vSphere storage policy" |
| 88 |
state: "absent" |
| 89 |
delegate_to: localhost |
| 90 |
''' |
| 91 |
|
| 92 |
RETURN = r''' |
| 93 |
vmware_vm_storage_policy: |
| 94 |
description: dictionary of information for the storage policy |
| 95 |
returned: success |
| 96 |
type: dict |
| 97 |
sample: { |
| 98 |
"vmware_vm_storage_policy": { |
| 99 |
"description": "Storage policy for gold-tier storage", |
| 100 |
"id": "aa6d5a82-1c88-45da-85d3-3d74b91a5bad", |
| 101 |
"name": "gold" |
| 102 |
} |
| 103 |
} |
| 104 |
''' |
| 105 |
|
| 106 |
try: |
| 107 |
from pyVmomi import pbm |
| 108 |
except ImportError: |
| 109 |
pass |
| 110 |
|
| 111 |
from ansible.module_utils.basic import AnsibleModule |
| 112 |
from ansible_collections.community.vmware.plugins.module_utils.vmware_spbm import SPBM |
| 113 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 114 |
from ansible_collections.community.vmware.plugins.module_utils.vmware_rest_client import VmwareRestClient |
| 115 |
|
| 116 |
|
| 117 |
class VmwareStoragePolicyManager(SPBM): |
| 118 |
def __init__(self, module): |
| 119 |
super(VmwareStoragePolicyManager, self).__init__(module) |
| 120 |
self.rest_client = VmwareRestClient(module) |
| 121 |
|
| 122 |
# |
| 123 |
# MOB METHODS |
| 124 |
# |
| 125 |
# These will generate the individual items with the following expected structure (see |
| 126 |
# https://github.com/vmware/pyvmomi/blob/master/pyVmomi/PbmObjects.py): |
| 127 |
# |
| 128 |
# PbmProfile: array |
| 129 |
# - name: string |
| 130 |
# description: string |
| 131 |
# constraints: PbmCapabilityConstraints |
| 132 |
# subProfiles: ArrayOfPbmCapabilitySubProfile |
| 133 |
# - name: string |
| 134 |
# capability: ArrayOfPbmCapabilityInstance |
| 135 |
# - constraint: ArrayOfCapabilityConstraintInstance |
| 136 |
# - id: string |
| 137 |
# value: anyType |
| 138 |
# values: arrayOfStrings |
| 139 |
# - tags |
| 140 |
# |
| 141 |
# |
| 142 |
def create_mob_tag_values(self, tags): |
| 143 |
return pbm.capability.types.DiscreteSet(values=tags) |
| 144 |
|
| 145 |
def create_mob_capability_property_instance(self, tag_id, tag_operator, tags): |
| 146 |
return pbm.capability.PropertyInstance( |
| 147 |
id=tag_id, |
| 148 |
operator=tag_operator, |
| 149 |
value=self.create_mob_tag_values(tags) |
| 150 |
) |
| 151 |
|
| 152 |
def create_mob_capability_constraint_instance(self, tag_id, tag_operator, tags): |
| 153 |
return pbm.capability.ConstraintInstance( |
| 154 |
propertyInstance=[self.create_mob_capability_property_instance(tag_id, tag_operator, tags)] |
| 155 |
) |
| 156 |
|
| 157 |
def create_mob_capability_metadata_uniqueid(self, tag_category): |
| 158 |
return pbm.capability.CapabilityMetadata.UniqueId( |
| 159 |
namespace="http://www.vmware.com/storage/tag", |
| 160 |
id=tag_category |
| 161 |
) |
| 162 |
|
| 163 |
def create_mob_capability_instance(self, tag_id, tag_operator, tags, tag_category): |
| 164 |
return pbm.capability.CapabilityInstance( |
| 165 |
id=self.create_mob_capability_metadata_uniqueid(tag_category), |
| 166 |
constraint=[self.create_mob_capability_constraint_instance(tag_id, tag_operator, tags)] |
| 167 |
) |
| 168 |
|
| 169 |
def create_mob_capability_constraints_subprofile(self, tag_id, tag_operator, tags, tag_category): |
| 170 |
return pbm.profile.SubProfileCapabilityConstraints.SubProfile( |
| 171 |
name="Tag based placement", |
| 172 |
capability=[self.create_mob_capability_instance(tag_id, tag_operator, tags, tag_category)] |
| 173 |
) |
| 174 |
|
| 175 |
def create_mob_capability_subprofile(self, tag_id, tag_operator, tags, tag_category): |
| 176 |
return pbm.profile.SubProfileCapabilityConstraints( |
| 177 |
subProfiles=[self.create_mob_capability_constraints_subprofile(tag_id, tag_operator, tags, tag_category)] |
| 178 |
) |
| 179 |
|
| 180 |
def create_mob_pbm_update_spec(self, tag_id, tag_operator, tags, tag_category, description): |
| 181 |
return pbm.profile.CapabilityBasedProfileUpdateSpec( |
| 182 |
description=description, |
| 183 |
constraints=self.create_mob_capability_subprofile(tag_id, tag_operator, tags, tag_category) |
| 184 |
) |
| 185 |
|
| 186 |
def create_mob_pbm_create_spec(self, tag_id, tag_operator, tags, tag_category, description, name): |
| 187 |
return pbm.profile.CapabilityBasedProfileCreateSpec( |
| 188 |
name=name, |
| 189 |
description=description, |
| 190 |
resourceType=pbm.profile.ResourceType(resourceType="STORAGE"), |
| 191 |
category="REQUIREMENT", |
| 192 |
constraints=self.create_mob_capability_subprofile(tag_id, tag_operator, tags, tag_category) |
| 193 |
) |
| 194 |
|
| 195 |
def get_tag_constraints(self, capabilities): |
| 196 |
""" |
| 197 |
Return tag constraints for a profile given its capabilities |
| 198 |
""" |
| 199 |
tag_constraints = {} |
| 200 |
for capability in capabilities: |
| 201 |
for constraint in capability.constraint: |
| 202 |
if hasattr(constraint, 'propertyInstance'): |
| 203 |
for propertyInstance in constraint.propertyInstance: |
| 204 |
if hasattr(propertyInstance.value, 'values'): |
| 205 |
tag_constraints['id'] = propertyInstance.id |
| 206 |
tag_constraints['values'] = propertyInstance.value.values |
| 207 |
tag_constraints['operator'] = propertyInstance.operator |
| 208 |
|
| 209 |
return tag_constraints |
| 210 |
|
| 211 |
def get_profile_manager(self): |
| 212 |
self.get_spbm_connection() |
| 213 |
|
| 214 |
return self.spbm_content.profileManager |
| 215 |
|
| 216 |
def get_storage_policies(self, profile_manager): |
| 217 |
profile_ids = profile_manager.PbmQueryProfile( |
| 218 |
resourceType=pbm.profile.ResourceType(resourceType="STORAGE"), |
| 219 |
profileCategory="REQUIREMENT" |
| 220 |
) |
| 221 |
profiles = [] |
| 222 |
if profile_ids: |
| 223 |
profiles = profile_manager.PbmRetrieveContent(profileIds=profile_ids) |
| 224 |
|
| 225 |
return profiles |
| 226 |
|
| 227 |
def format_profile(self, profile): |
| 228 |
formatted_profile = { |
| 229 |
'name': profile.name, |
| 230 |
'id': profile.profileId.uniqueId, |
| 231 |
'description': profile.description |
| 232 |
} |
| 233 |
|
| 234 |
return formatted_profile |
| 235 |
|
| 236 |
def format_tag_mob_id(self, tag_category): |
| 237 |
return "com.vmware.storage.tag." + tag_category + ".property" |
| 238 |
|
| 239 |
def format_results_and_exit(self, results, policy, changed): |
| 240 |
results['vmware_vm_storage_policy'] = self.format_profile(policy) |
| 241 |
results['changed'] = changed |
| 242 |
|
| 243 |
self.module.exit_json(**results) |
| 244 |
|
| 245 |
def update_storage_policy(self, policy, pbm_client, results): |
| 246 |
expected_description = self.params.get('description') |
| 247 |
expected_tags = [self.params.get('tag_name')] |
| 248 |
expected_tag_category = self.params.get('tag_category') |
| 249 |
expected_tag_id = self.format_tag_mob_id(expected_tag_category) |
| 250 |
expected_operator = "NOT" |
| 251 |
if self.params.get('tag_affinity'): |
| 252 |
expected_operator = None |
| 253 |
|
| 254 |
needs_change = False |
| 255 |
|
| 256 |
if policy.description != expected_description: |
| 257 |
needs_change = True |
| 258 |
|
| 259 |
if hasattr(policy.constraints, 'subProfiles'): |
| 260 |
for subprofile in policy.constraints.subProfiles: |
| 261 |
tag_constraints = self.get_tag_constraints(subprofile.capability) |
| 262 |
if tag_constraints['id'] == expected_tag_id: |
| 263 |
if tag_constraints['values'] != expected_tags: |
| 264 |
needs_change = True |
| 265 |
else: |
| 266 |
needs_change = True |
| 267 |
|
| 268 |
if tag_constraints['operator'] != expected_operator: |
| 269 |
needs_change = True |
| 270 |
else: |
| 271 |
needs_change = True |
| 272 |
|
| 273 |
if needs_change: |
| 274 |
pbm_client.PbmUpdate( |
| 275 |
profileId=policy.profileId, |
| 276 |
updateSpec=self.create_mob_pbm_update_spec(expected_tag_id, expected_operator, expected_tags, expected_tag_category, expected_description) |
| 277 |
) |
| 278 |
|
| 279 |
self.format_results_and_exit(results, policy, needs_change) |
| 280 |
|
| 281 |
def remove_storage_policy(self, policy, pbm_client, results): |
| 282 |
pbm_client.PbmDelete(profileId=[policy.profileId]) |
| 283 |
|
| 284 |
self.format_results_and_exit(results, policy, True) |
| 285 |
|
| 286 |
def create_storage_policy(self, policy, pbm_client, results): |
| 287 |
profile_ids = pbm_client.PbmCreate( |
| 288 |
createSpec=self.create_mob_pbm_create_spec( |
| 289 |
self.format_tag_mob_id(self.params.get('tag_category')), |
| 290 |
None, |
| 291 |
[self.params.get('tag_name')], |
| 292 |
self.params.get('tag_category'), |
| 293 |
self.params.get('description'), |
| 294 |
self.params.get('name') |
| 295 |
) |
| 296 |
) |
| 297 |
|
| 298 |
policy = pbm_client.PbmRetrieveContent(profileIds=[profile_ids]) |
| 299 |
|
| 300 |
self.format_results_and_exit(results, policy[0], True) |
| 301 |
|
| 302 |
def ensure_state(self): |
| 303 |
client = self.get_profile_manager() |
| 304 |
policies = self.get_storage_policies(client) |
| 305 |
policy_name = self.params.get('name') |
| 306 |
results = dict(changed=False, vmware_vm_storage_policy={}) |
| 307 |
|
| 308 |
if self.params.get('state') == 'present': |
| 309 |
if self.params.get('tag_category') is None: |
| 310 |
self.module.fail_json(msg="tag_category is required when 'state' is 'present'") |
| 311 |
|
| 312 |
if self.params.get('tag_name') is None: |
| 313 |
self.module.fail_json(msg="tag_name is required when 'state' is 'present'") |
| 314 |
|
| 315 |
# ensure if the category exists |
| 316 |
category_result = self.rest_client.get_category_by_name(self.params.get('tag_category')) |
| 317 |
if category_result is None: |
| 318 |
self.module.fail_json(msg="%s is not found in vCenter Server tag categories" % self.params.get('tag_category')) |
| 319 |
|
| 320 |
# ensure if the tag exists |
| 321 |
tag_result = self.rest_client.get_tag_by_category_name(self.params.get('tag_name'), self.params.get('tag_category')) |
| 322 |
if tag_result is None: |
| 323 |
self.module.fail_json(msg="%s is not found in vCenter Server tags" % self.params.get('tag_name')) |
| 324 |
|
| 325 |
# loop through and update the first match |
| 326 |
for policy in policies: |
| 327 |
if policy.name == policy_name: |
| 328 |
self.update_storage_policy(policy, client, results) |
| 329 |
|
| 330 |
# if we didn't exit by now create the profile |
| 331 |
self.create_storage_policy(policy, client, results) |
| 332 |
|
| 333 |
if self.params.get('state') == 'absent': |
| 334 |
# loop through and delete the first match |
| 335 |
for policy in policies: |
| 336 |
if policy.name == policy_name: |
| 337 |
self.remove_storage_policy(policy, client, results) |
| 338 |
|
| 339 |
# if we didn't exit by now exit without changing anything |
| 340 |
self.module.exit_json(**results) |
| 341 |
|
| 342 |
|
| 343 |
def main(): |
| 344 |
argument_spec = base_argument_spec() |
| 345 |
argument_spec.update( |
| 346 |
name=dict(type='str', required=True), |
| 347 |
description=dict(type='str', required=False), |
| 348 |
tag_name=dict(type='str', required=False), |
| 349 |
tag_category=dict(type='str', required=False), |
| 350 |
tag_affinity=dict(type='bool', default=True), |
| 351 |
state=dict(type='str', choices=['absent', 'present'], default='present') |
| 352 |
) |
| 353 |
|
| 354 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False) |
| 355 |
manager = VmwareStoragePolicyManager(module) |
| 356 |
|
| 357 |
manager.ensure_state() |
| 358 |
|
| 359 |
|
| 360 |
if __name__ == '__main__': |
| 361 |
main() |