| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2019, Ansible Project |
| 5 |
# Copyright: (c) 2019, Abhijeet Kasurde <[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 |
|
| 13 |
DOCUMENTATION = r''' |
| 14 |
--- |
| 15 |
module: vmware_vm_storage_policy_info |
| 16 |
short_description: Gather information about vSphere storage profile defined storage policy information. |
| 17 |
description: |
| 18 |
- Returns basic information on vSphere storage profiles. |
| 19 |
- A vSphere storage profile defines storage policy information that describes storage requirements |
| 20 |
for virtual machines and storage capabilities of storage providers. |
| 21 |
author: |
| 22 |
- Abhijeet Kasurde (@Akasurde) |
| 23 |
extends_documentation_fragment: |
| 24 |
- vmware.vmware.base_options |
| 25 |
|
| 26 |
''' |
| 27 |
|
| 28 |
EXAMPLES = r''' |
| 29 |
- name: Get SPBM info |
| 30 |
community.vmware.vmware_vm_storage_policy_info: |
| 31 |
hostname: '{{ vcenter_hostname }}' |
| 32 |
username: '{{ vcenter_username }}' |
| 33 |
password: '{{ vcenter_password }}' |
| 34 |
delegate_to: localhost |
| 35 |
register: profiles |
| 36 |
''' |
| 37 |
|
| 38 |
RETURN = r''' |
| 39 |
spbm_profiles: |
| 40 |
description: list of dictionary of SPBM info |
| 41 |
returned: success |
| 42 |
type: list |
| 43 |
sample: [ |
| 44 |
{ |
| 45 |
"constraints_sub_profiles": [ |
| 46 |
{ |
| 47 |
"rule_set_info": [ |
| 48 |
{ |
| 49 |
"id": "hostFailuresToTolerate", |
| 50 |
"value": 1 |
| 51 |
}, |
| 52 |
{ |
| 53 |
"id": "stripeWidth", |
| 54 |
"value": 1 |
| 55 |
}, |
| 56 |
{ |
| 57 |
"id": "forceProvisioning", |
| 58 |
"value": false |
| 59 |
}, |
| 60 |
{ |
| 61 |
"id": "proportionalCapacity", |
| 62 |
"value": 0 |
| 63 |
}, |
| 64 |
{ |
| 65 |
"id": "cacheReservation", |
| 66 |
"value": 0 |
| 67 |
} |
| 68 |
], |
| 69 |
"rule_set_name": "VSAN sub-profile" |
| 70 |
} |
| 71 |
], |
| 72 |
"description": "Storage policy used as default for vSAN datastores", |
| 73 |
"id": "aa6d5a82-1c88-45da-85d3-3d74b91a5bad", |
| 74 |
"name": "vSAN Default Storage Policy" |
| 75 |
}, |
| 76 |
] |
| 77 |
''' |
| 78 |
|
| 79 |
try: |
| 80 |
from pyVmomi import pbm |
| 81 |
except ImportError: |
| 82 |
pass |
| 83 |
|
| 84 |
from ansible.module_utils.basic import AnsibleModule |
| 85 |
from ansible_collections.community.vmware.plugins.module_utils.vmware_spbm import SPBM |
| 86 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 87 |
|
| 88 |
|
| 89 |
class SPBMClient(SPBM): |
| 90 |
def __init__(self, module): |
| 91 |
super(SPBMClient, self).__init__(module) |
| 92 |
|
| 93 |
def show_capabilities(self, capabilities): |
| 94 |
""" |
| 95 |
Return property instance for given capabilities |
| 96 |
""" |
| 97 |
capabilities_info = [] |
| 98 |
for capability in capabilities: |
| 99 |
for constraint in capability.constraint: |
| 100 |
if hasattr(constraint, 'propertyInstance'): |
| 101 |
for propertyInstance in constraint.propertyInstance: |
| 102 |
capabilities_info.append( |
| 103 |
{ |
| 104 |
'id': propertyInstance.id, |
| 105 |
'value': propertyInstance.value |
| 106 |
} |
| 107 |
) |
| 108 |
return capabilities_info |
| 109 |
|
| 110 |
def get_storage_policy_info(self): |
| 111 |
self.get_spbm_connection() |
| 112 |
|
| 113 |
results = dict(changed=False, spbm_profiles=[]) |
| 114 |
profile_manager = self.spbm_content.profileManager |
| 115 |
profile_ids = profile_manager.PbmQueryProfile( |
| 116 |
resourceType=pbm.profile.ResourceType(resourceType="STORAGE"), |
| 117 |
profileCategory="REQUIREMENT" |
| 118 |
) |
| 119 |
profiles = [] |
| 120 |
if profile_ids: |
| 121 |
profiles = profile_manager.PbmRetrieveContent(profileIds=profile_ids) |
| 122 |
|
| 123 |
for profile in profiles: |
| 124 |
temp_profile_info = { |
| 125 |
'name': profile.name, |
| 126 |
'id': profile.profileId.uniqueId, |
| 127 |
'description': profile.description, |
| 128 |
'constraints_sub_profiles': [] |
| 129 |
} |
| 130 |
if hasattr(profile.constraints, 'subProfiles'): |
| 131 |
subprofiles = profile.constraints.subProfiles |
| 132 |
temp_sub_profiles = [] |
| 133 |
for subprofile in subprofiles: |
| 134 |
rule_set_info = self.show_capabilities(subprofile.capability) |
| 135 |
# if a storage policy set tag base placement rules, the tags are set into the value. |
| 136 |
# https://github.com/ansible-collections/community.vmware/issues/742 |
| 137 |
for _rule_set_info in rule_set_info: |
| 138 |
if isinstance(_rule_set_info['value'], pbm.capability.types.DiscreteSet): |
| 139 |
_rule_set_info['value'] = _rule_set_info['value'].values |
| 140 |
temp_sub_profiles.append({ |
| 141 |
'rule_set_name': subprofile.name, |
| 142 |
'rule_set_info': rule_set_info, |
| 143 |
}) |
| 144 |
temp_profile_info['constraints_sub_profiles'] = temp_sub_profiles |
| 145 |
|
| 146 |
results['spbm_profiles'].append(temp_profile_info) |
| 147 |
|
| 148 |
self.module.exit_json(**results) |
| 149 |
|
| 150 |
|
| 151 |
def main(): |
| 152 |
argument_spec = base_argument_spec() |
| 153 |
|
| 154 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 155 |
|
| 156 |
spbm_client = SPBMClient(module) |
| 157 |
spbm_client.get_storage_policy_info() |
| 158 |
|
| 159 |
|
| 160 |
if __name__ == '__main__': |
| 161 |
main() |