| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
from __future__ import (absolute_import, division, print_function) |
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
DOCUMENTATION = r''' |
| 13 |
--- |
| 14 |
module: omevv_vcenter_info |
| 15 |
short_description: Retrieve all or specific OpenManage Enterprise vCenter information. |
| 16 |
version_added: "9.8.0" |
| 17 |
description: |
| 18 |
- This module allows you to retrieve all or specific vCenter information from OpenManage Enterprise. |
| 19 |
extends_documentation_fragment: |
| 20 |
- dellemc.openmanage.ome_auth_options |
| 21 |
options: |
| 22 |
vcenter_hostname: |
| 23 |
description: |
| 24 |
- vCenter IP address or hostname. |
| 25 |
- If I(vcenter_hostname) is provided, the module retrieves only specified vCenter information. |
| 26 |
type: str |
| 27 |
requirements: |
| 28 |
- "python >= 3.9.6" |
| 29 |
author: |
| 30 |
- "Lovepreet Singh (@singh-lovepreet1)" |
| 31 |
attributes: |
| 32 |
check_mode: |
| 33 |
description: Runs task to validate without performing action on the target machine. |
| 34 |
support: full |
| 35 |
notes: |
| 36 |
- This module supports IPv4 and IPv6 addresses. |
| 37 |
''' |
| 38 |
|
| 39 |
|
| 40 |
EXAMPLES = r''' |
| 41 |
--- |
| 42 |
- name: Retrieve all vCenter information. |
| 43 |
dellemc.openmanage.omevv_vcenter_info: |
| 44 |
hostname: "192.168.0.1" |
| 45 |
username: "username" |
| 46 |
password: "password" |
| 47 |
ca_path: "path/to/ca_file" |
| 48 |
|
| 49 |
- name: Retrieve specific vCenter information. |
| 50 |
dellemc.openmanage.omevv_vcenter_info: |
| 51 |
hostname: "192.168.0.1" |
| 52 |
username: "username" |
| 53 |
password: "password" |
| 54 |
ca_path: "path/to/ca_file" |
| 55 |
vcenter_hostname: xx.xx.xx.xx |
| 56 |
''' |
| 57 |
|
| 58 |
RETURN = r''' |
| 59 |
--- |
| 60 |
msg: |
| 61 |
description: Status of the vCenter information for the retrieve operation. |
| 62 |
returned: always |
| 63 |
type: str |
| 64 |
sample: "Successfully retrieved the vCenter information." |
| 65 |
vcenter_info: |
| 66 |
description: Information on the vCenter. |
| 67 |
returned: success |
| 68 |
type: list |
| 69 |
elements: dict |
| 70 |
sample: |
| 71 |
[ |
| 72 |
{ |
| 73 |
"uuid": "77373c7e-d2b0-453b-9888-102499919bd1", |
| 74 |
"consoleAddress": vcenter_ip_or_hostname, |
| 75 |
"description": "vCenter 8.0", |
| 76 |
"registeredExtensions": [ |
| 77 |
"PHM", |
| 78 |
"WEBCLIENT", |
| 79 |
"PHA", |
| 80 |
"VLCM" |
| 81 |
] |
| 82 |
}, |
| 83 |
{ |
| 84 |
"uuid": "77373c7e-d2b0-453b-9888-102499919bd2", |
| 85 |
"consoleAddress": vcenter_ip_or_hostname, |
| 86 |
"description": "vCenter 8.1", |
| 87 |
"registeredExtensions": [ |
| 88 |
"PHM", |
| 89 |
"WEBCLIENT", |
| 90 |
"PHA", |
| 91 |
"VLCM" |
| 92 |
] |
| 93 |
} |
| 94 |
] |
| 95 |
error_info: |
| 96 |
description: Details of the HTTP Error. |
| 97 |
returned: on HTTP error |
| 98 |
type: dict |
| 99 |
sample: { |
| 100 |
"error": { |
| 101 |
"code": "Base.1.0.GeneralError", |
| 102 |
"message": "A general error has occurred. See ExtendedInfo for more information.", |
| 103 |
"@Message.ExtendedInfo": [ |
| 104 |
{ |
| 105 |
"MessageId": "GEN1234", |
| 106 |
"RelatedProperties": [], |
| 107 |
"Message": "Unable to process the request because an error occurred.", |
| 108 |
"MessageArgs": [], |
| 109 |
"Severity": "Critical", |
| 110 |
"Resolution": "Retry the operation. If the issue persists, contact your system administrator." |
| 111 |
} |
| 112 |
] |
| 113 |
} |
| 114 |
} |
| 115 |
''' |
| 116 |
|
| 117 |
import json |
| 118 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 119 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 120 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.omevv import RestOMEVV |
| 121 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.omevv_utils.omevv_info_utils import OMEVVInfo |
| 122 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import OmeAnsibleModule |
| 123 |
|
| 124 |
SUCCESS_MSG = "Successfully retrieved the vCenter information." |
| 125 |
NO_VCENTER_MSG = "'{vcenter_hostname}' vCenter is not registered in OME." |
| 126 |
|
| 127 |
|
| 128 |
class OMEVVVCenterInfo: |
| 129 |
|
| 130 |
def __init__(self, module, rest_obj) -> None: |
| 131 |
""" |
| 132 |
Initializes a new instance of the class. |
| 133 |
Args: |
| 134 |
module (Module): The module object. |
| 135 |
rest_obj (object): The REST object. |
| 136 |
Returns: |
| 137 |
None |
| 138 |
""" |
| 139 |
self.module = module |
| 140 |
self.obj = rest_obj |
| 141 |
|
| 142 |
def perform_module_operation(self) -> dict: |
| 143 |
""" |
| 144 |
Perform the module operation to retrieve the vCenter information. |
| 145 |
Returns: |
| 146 |
dict: A dictionary containing the message and the vCenter information. |
| 147 |
- msg (str): The success message or the error message. |
| 148 |
- vcenter_info (list): The list of vCenter information. |
| 149 |
""" |
| 150 |
vcenter_id = self.module.params.get("vcenter_hostname") |
| 151 |
self.omevv_utils_obj = OMEVVInfo(self.obj) |
| 152 |
resp = self.omevv_utils_obj.get_vcenter_info(vcenter_id) |
| 153 |
result = {'msg': SUCCESS_MSG, 'vcenter_info': resp} |
| 154 |
if vcenter_id and not resp: |
| 155 |
result['msg'] = NO_VCENTER_MSG.format(vcenter_hostname=vcenter_id) |
| 156 |
return result |
| 157 |
|
| 158 |
|
| 159 |
def main(): |
| 160 |
argument_spec = { |
| 161 |
"vcenter_hostname": {"type": 'str'} |
| 162 |
} |
| 163 |
module = OmeAnsibleModule(argument_spec=argument_spec, |
| 164 |
supports_check_mode=True) |
| 165 |
try: |
| 166 |
with RestOMEVV(module.params) as rest_obj: |
| 167 |
rest_obj.username = module.params.get("username") |
| 168 |
rest_obj.password = module.params.get("password") |
| 169 |
omevv_obj = OMEVVVCenterInfo(module, rest_obj) |
| 170 |
resp = omevv_obj.perform_module_operation() |
| 171 |
module.exit_json(msg=resp['msg'], vcenter_info=resp['vcenter_info']) |
| 172 |
except HTTPError as err: |
| 173 |
module.exit_json(msg=str(err), error_info=json.load(err), failed=True) |
| 174 |
except URLError as err: |
| 175 |
module.exit_json(msg=str(err), unreachable=True) |
| 176 |
except (IOError, ValueError, SSLValidationError, TypeError, ConnectionError, |
| 177 |
AttributeError, IndexError, KeyError, OSError) as err: |
| 178 |
module.exit_json(msg=str(err), failed=True) |
| 179 |
|
| 180 |
|
| 181 |
if __name__ == '__main__': |
| 182 |
main() |