| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# Copyright: (c) 2021, Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# template: header.j2 |
| 6 |
# This module is autogenerated using the ansible.content_builder. |
| 7 |
# See: https://github.com/ansible-community/ansible.content_builder |
| 8 |
|
| 9 |
|
| 10 |
DOCUMENTATION = r""" |
| 11 |
module: vcenter_storage_policies_info |
| 12 |
short_description: Returns information about at most 1024 visible (subject to permission |
| 13 |
checks) storage solicies availabe in vCenter |
| 14 |
description: Returns information about at most 1024 visible (subject to permission |
| 15 |
checks) storage solicies availabe in vCenter. These storage policies can be used |
| 16 |
for provisioning virtual machines or disks. |
| 17 |
options: |
| 18 |
policies: |
| 19 |
description: |
| 20 |
- Identifiers of storage policies that can match the filter. |
| 21 |
- If unset or empty, storage policies with any identifiers match the filter. |
| 22 |
- When clients pass a value of this structure as a parameter, the field must |
| 23 |
contain the id of resources returned by M(vmware.vmware_rest.vcenter_storage_policies_info). |
| 24 |
elements: str |
| 25 |
type: list |
| 26 |
session_timeout: |
| 27 |
description: |
| 28 |
- 'Timeout settings for client session. ' |
| 29 |
- 'The maximal number of seconds for the whole operation including connection |
| 30 |
establishment, request sending and response. ' |
| 31 |
- The default value is 300s. |
| 32 |
type: float |
| 33 |
version_added: 2.1.0 |
| 34 |
vcenter_hostname: |
| 35 |
description: |
| 36 |
- The hostname or IP address of the vSphere vCenter |
| 37 |
- If the value is not specified in the task, the value of environment variable |
| 38 |
C(VMWARE_HOST) will be used instead. |
| 39 |
required: true |
| 40 |
type: str |
| 41 |
vcenter_password: |
| 42 |
description: |
| 43 |
- The vSphere vCenter password |
| 44 |
- If the value is not specified in the task, the value of environment variable |
| 45 |
C(VMWARE_PASSWORD) will be used instead. |
| 46 |
required: true |
| 47 |
type: str |
| 48 |
vcenter_rest_log_file: |
| 49 |
description: |
| 50 |
- 'You can use this optional parameter to set the location of a log file. ' |
| 51 |
- 'This file will be used to record the HTTP REST interaction. ' |
| 52 |
- 'The file will be stored on the host that runs the module. ' |
| 53 |
- 'If the value is not specified in the task, the value of ' |
| 54 |
- environment variable C(VMWARE_REST_LOG_FILE) will be used instead. |
| 55 |
type: str |
| 56 |
vcenter_username: |
| 57 |
description: |
| 58 |
- The vSphere vCenter username |
| 59 |
- If the value is not specified in the task, the value of environment variable |
| 60 |
C(VMWARE_USER) will be used instead. |
| 61 |
required: true |
| 62 |
type: str |
| 63 |
vcenter_validate_certs: |
| 64 |
default: true |
| 65 |
description: |
| 66 |
- Allows connection when SSL certificates are not valid. Set to C(false) when |
| 67 |
certificates are not trusted. |
| 68 |
- If the value is not specified in the task, the value of environment variable |
| 69 |
C(VMWARE_VALIDATE_CERTS) will be used instead. |
| 70 |
type: bool |
| 71 |
author: |
| 72 |
- Ansible Cloud Team (@ansible-collections) |
| 73 |
version_added: 0.3.0 |
| 74 |
requirements: |
| 75 |
- vSphere 7.0.3 or greater |
| 76 |
- python >= 3.6 |
| 77 |
- aiohttp |
| 78 |
notes: |
| 79 |
- Tested on vSphere 7.0.3 |
| 80 |
""" |
| 81 |
|
| 82 |
EXAMPLES = r""" |
| 83 |
- name: List existing storage policies |
| 84 |
vmware.vmware_rest.vcenter_storage_policies_info: |
| 85 |
register: storage_policies |
| 86 |
""" |
| 87 |
RETURN = r""" |
| 88 |
# content generated by the update_return_section callback# task: List existing storage policies |
| 89 |
value: |
| 90 |
description: List existing storage policies |
| 91 |
returned: On success |
| 92 |
sample: |
| 93 |
- description: Management Storage policy used for VMC large cluster |
| 94 |
name: Management Storage Policy - Large |
| 95 |
policy: cd8f7c94-3e11-67fc-17f5-4e96d91a5beb |
| 96 |
- description: Allow the datastore to determine the best placement strategy for |
| 97 |
storage objects |
| 98 |
name: VVol No Requirements Policy |
| 99 |
policy: f4e5bade-15a2-4805-bf8e-52318c4ce443 |
| 100 |
- description: Management Storage policy used for smaller VMC Stretched Cluster |
| 101 |
configuration. |
| 102 |
name: Management Storage Policy - Stretched Lite |
| 103 |
policy: d109de24-c966-428f-8da2-d281e6671e35 |
| 104 |
- description: Sample storage policy for VMware's VM and virtual disk encryption |
| 105 |
name: VM Encryption Policy |
| 106 |
policy: 4d5f673c-536f-11e6-beb8-9e71128cae77 |
| 107 |
- description: Management Storage policy used for encrypting VM |
| 108 |
name: Management Storage policy - Encryption |
| 109 |
policy: b1263970-8662-69e2-adc6-fa8ae01abecc |
| 110 |
- description: Management Storage policy used for VMC single node cluster |
| 111 |
name: Management Storage Policy - Single Node |
| 112 |
policy: a9423670-7455-11e8-adc0-fa7ae01bbebc |
| 113 |
- description: Storage policy used as default for Host-local PMem datastores |
| 114 |
name: Host-local PMem Default Storage Policy |
| 115 |
policy: c268da1b-b343-49f7-a468-b1deeb7078e0 |
| 116 |
- description: Storage policy used as default for vSAN datastores |
| 117 |
name: vSAN Default Storage Policy |
| 118 |
policy: aa6d5a82-1c88-45da-85d3-3d74b91a5bad |
| 119 |
- description: Management Storage policy used for VMC regular cluster |
| 120 |
name: Management Storage Policy - Regular |
| 121 |
policy: bb7e6b13-2d99-46eb-96e4-3d85c91a5bde |
| 122 |
- description: Management Storage policy used for VMC regular cluster which requires |
| 123 |
THIN provisioning |
| 124 |
name: Management Storage policy - Thin |
| 125 |
policy: b6423670-8552-66e8-adc1-fa6ae01abeac |
| 126 |
- description: Management Storage policy used for VMC stretched cluster |
| 127 |
name: Management Storage Policy - Stretched |
| 128 |
policy: f31f2442-8247-4517-87c2-8d69d7a6c696 |
| 129 |
type: list |
| 130 |
""" |
| 131 |
|
| 132 |
|
| 133 |
# This structure describes the format of the data expected by the end-points |
| 134 |
PAYLOAD_FORMAT = { |
| 135 |
"list": {"query": {"policies": "policies"}, "body": {}, "path": {}} |
| 136 |
} # pylint: disable=line-too-long |
| 137 |
|
| 138 |
from ansible.module_utils.basic import env_fallback |
| 139 |
import os |
| 140 |
|
| 141 |
if os.getenv("VMWARE_ENABLE_TURBO", False): |
| 142 |
try: |
| 143 |
from ansible_collections.cloud.common.plugins.module_utils.turbo.module import ( |
| 144 |
AnsibleTurboModule as AnsibleModule, |
| 145 |
) |
| 146 |
|
| 147 |
AnsibleModule.collection_name = "vmware.vmware_rest" |
| 148 |
except ImportError: |
| 149 |
from ansible.module_utils.basic import AnsibleModule |
| 150 |
else: |
| 151 |
from ansible.module_utils.basic import AnsibleModule |
| 152 |
|
| 153 |
from ansible_collections.vmware.vmware_rest.plugins.module_utils.vmware_rest import ( |
| 154 |
gen_args, |
| 155 |
open_session, |
| 156 |
session_timeout, |
| 157 |
update_changed_flag, |
| 158 |
) |
| 159 |
|
| 160 |
|
| 161 |
def prepare_argument_spec(): |
| 162 |
argument_spec = { |
| 163 |
"vcenter_hostname": dict( |
| 164 |
type="str", |
| 165 |
required=True, |
| 166 |
fallback=(env_fallback, ["VMWARE_HOST"]), |
| 167 |
), |
| 168 |
"vcenter_username": dict( |
| 169 |
type="str", |
| 170 |
required=True, |
| 171 |
fallback=(env_fallback, ["VMWARE_USER"]), |
| 172 |
), |
| 173 |
"vcenter_password": dict( |
| 174 |
type="str", |
| 175 |
required=True, |
| 176 |
no_log=True, |
| 177 |
fallback=(env_fallback, ["VMWARE_PASSWORD"]), |
| 178 |
), |
| 179 |
"vcenter_validate_certs": dict( |
| 180 |
type="bool", |
| 181 |
required=False, |
| 182 |
default=True, |
| 183 |
fallback=(env_fallback, ["VMWARE_VALIDATE_CERTS"]), |
| 184 |
), |
| 185 |
"vcenter_rest_log_file": dict( |
| 186 |
type="str", |
| 187 |
required=False, |
| 188 |
fallback=(env_fallback, ["VMWARE_REST_LOG_FILE"]), |
| 189 |
), |
| 190 |
"session_timeout": dict( |
| 191 |
type="float", |
| 192 |
required=False, |
| 193 |
fallback=(env_fallback, ["VMWARE_SESSION_TIMEOUT"]), |
| 194 |
), |
| 195 |
} |
| 196 |
|
| 197 |
argument_spec["policies"] = {"type": "list", "elements": "str"} |
| 198 |
|
| 199 |
return argument_spec |
| 200 |
|
| 201 |
|
| 202 |
async def main(): |
| 203 |
required_if = list([]) |
| 204 |
|
| 205 |
module_args = prepare_argument_spec() |
| 206 |
module = AnsibleModule( |
| 207 |
argument_spec=module_args, required_if=required_if, supports_check_mode=True |
| 208 |
) |
| 209 |
if not module.params["vcenter_hostname"]: |
| 210 |
module.fail_json("vcenter_hostname cannot be empty") |
| 211 |
if not module.params["vcenter_username"]: |
| 212 |
module.fail_json("vcenter_username cannot be empty") |
| 213 |
if not module.params["vcenter_password"]: |
| 214 |
module.fail_json("vcenter_password cannot be empty") |
| 215 |
try: |
| 216 |
session = await open_session( |
| 217 |
vcenter_hostname=module.params["vcenter_hostname"], |
| 218 |
vcenter_username=module.params["vcenter_username"], |
| 219 |
vcenter_password=module.params["vcenter_password"], |
| 220 |
validate_certs=module.params["vcenter_validate_certs"], |
| 221 |
log_file=module.params["vcenter_rest_log_file"], |
| 222 |
) |
| 223 |
except Exception as err: |
| 224 |
if hasattr(err, "get_message"): |
| 225 |
module.fail_json(err.get_message()) |
| 226 |
else: |
| 227 |
module.fail_json(str(err)) |
| 228 |
result = await entry_point(module, session) |
| 229 |
module.exit_json(**result) |
| 230 |
|
| 231 |
|
| 232 |
# template: info_list_and_get_module.j2 |
| 233 |
def build_url(params): |
| 234 |
import yarl |
| 235 |
|
| 236 |
_in_query_parameters = PAYLOAD_FORMAT["list"]["query"].keys() |
| 237 |
return yarl.URL( |
| 238 |
("https://{vcenter_hostname}" "/api/vcenter/storage/policies").format(**params) |
| 239 |
+ gen_args(params, _in_query_parameters), |
| 240 |
encoded=True, |
| 241 |
) |
| 242 |
|
| 243 |
|
| 244 |
async def entry_point(module, session): |
| 245 |
url = build_url(module.params) |
| 246 |
async with session.get(url, **session_timeout(module.params)) as resp: |
| 247 |
_json = await resp.json() |
| 248 |
|
| 249 |
if "value" not in _json: # 7.0.2+ |
| 250 |
_json = {"value": _json} |
| 251 |
|
| 252 |
return await update_changed_flag(_json, resp.status, "get") |
| 253 |
|
| 254 |
|
| 255 |
if __name__ == "__main__": |
| 256 |
import asyncio |
| 257 |
|
| 258 |
current_loop = asyncio.new_event_loop() |
| 259 |
try: |
| 260 |
asyncio.set_event_loop(current_loop) |
| 261 |
current_loop.run_until_complete(main()) |
| 262 |
finally: |
| 263 |
current_loop.close() |