| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2020, Abhijeet Kasurde <[email protected]> |
| 5 |
# Copyright: (c) 2020, Ansible Project |
| 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_datacenter_info |
| 16 |
short_description: Gather information about VMware vSphere Datacenters |
| 17 |
description: |
| 18 |
- This module can be used to gather information VMware vSphere Datacenters. |
| 19 |
author: |
| 20 |
- Abhijeet Kasurde (@Akasurde) |
| 21 |
options: |
| 22 |
datacenter: |
| 23 |
description: |
| 24 |
- The name of the datacenter to gather information for. |
| 25 |
- If not provided, will gather information about all datacenters from the VMware infra. |
| 26 |
type: str |
| 27 |
aliases: ['datacenter_name'] |
| 28 |
schema: |
| 29 |
description: |
| 30 |
- Specify the output schema desired. |
| 31 |
- The V(summary) output schema is the legacy output from the module. |
| 32 |
- The V(vsphere) output schema is the vSphere API class definition. |
| 33 |
choices: ['summary', 'vsphere'] |
| 34 |
default: 'summary' |
| 35 |
type: str |
| 36 |
properties: |
| 37 |
description: |
| 38 |
- Specify the properties to retrieve. |
| 39 |
- If not specified, all properties are retrieved (deeply). |
| 40 |
- Results are returned in a structure identical to the vSphere API. |
| 41 |
- 'Example:' |
| 42 |
- ' properties: [' |
| 43 |
- ' "overallStatus"' |
| 44 |
- ' ]' |
| 45 |
- Only valid when O(schema=vsphere). |
| 46 |
type: list |
| 47 |
elements: str |
| 48 |
show_tag: |
| 49 |
description: |
| 50 |
- Tags related to Datacenter are shown if set to V(true). |
| 51 |
default: false |
| 52 |
type: bool |
| 53 |
extends_documentation_fragment: |
| 54 |
- vmware.vmware.base_options |
| 55 |
''' |
| 56 |
|
| 57 |
EXAMPLES = r''' |
| 58 |
- name: Gather information about all datacenters |
| 59 |
community.vmware.vmware_datacenter_info: |
| 60 |
hostname: '{{ vcenter_hostname }}' |
| 61 |
username: '{{ vcenter_username }}' |
| 62 |
password: '{{ vcenter_password }}' |
| 63 |
delegate_to: localhost |
| 64 |
|
| 65 |
- name: Gather information about a particular datacenter |
| 66 |
community.vmware.vmware_datacenter_info: |
| 67 |
hostname: '{{ vcenter_hostname }}' |
| 68 |
username: '{{ vcenter_username }}' |
| 69 |
password: '{{ vcenter_password }}' |
| 70 |
datacenter: '{{ datacenter_name }}' |
| 71 |
delegate_to: localhost |
| 72 |
|
| 73 |
- name: Gather information about a particular datacenter |
| 74 |
community.vmware.vmware_datacenter_info: |
| 75 |
hostname: '{{ vcenter_hostname }}' |
| 76 |
username: '{{ vcenter_username }}' |
| 77 |
password: '{{ vcenter_password }}' |
| 78 |
datacenter: '{{ datacenter_name }}' |
| 79 |
show_tag: true |
| 80 |
delegate_to: localhost |
| 81 |
|
| 82 |
- name: Gather vSphere schema information |
| 83 |
community.vmware.vmware_datacenter_info: |
| 84 |
hostname: '{{ vcenter_hostname }}' |
| 85 |
username: '{{ vcenter_username }}' |
| 86 |
password: '{{ vcenter_password }}' |
| 87 |
datacenter: '{{ datacenter_name }}' |
| 88 |
schema: vsphere |
| 89 |
properties: |
| 90 |
- configStatus |
| 91 |
- overallStatus |
| 92 |
''' |
| 93 |
|
| 94 |
RETURN = r''' |
| 95 |
datacenter_info: |
| 96 |
description: Information about datacenter |
| 97 |
returned: always |
| 98 |
type: list |
| 99 |
sample: |
| 100 |
[ |
| 101 |
{ |
| 102 |
"configStatus": "gray", |
| 103 |
"moid": "datacenter-2", |
| 104 |
"name": "Asia-Datacenter1" |
| 105 |
} |
| 106 |
] |
| 107 |
''' |
| 108 |
|
| 109 |
try: |
| 110 |
from pyVmomi import vim |
| 111 |
except ImportError: |
| 112 |
pass |
| 113 |
|
| 114 |
from ansible.module_utils.basic import AnsibleModule |
| 115 |
from ansible.module_utils.common.text.converters import to_native |
| 116 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 117 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 118 |
from ansible_collections.community.vmware.plugins.module_utils.vmware_rest_client import VmwareRestClient |
| 119 |
|
| 120 |
|
| 121 |
class VmwareDatacenterInfo(PyVmomi): |
| 122 |
def __init__(self, module): |
| 123 |
super(VmwareDatacenterInfo, self).__init__(module) |
| 124 |
if self.params.get('show_tag'): |
| 125 |
self.vmware_client = VmwareRestClient(self.module) |
| 126 |
|
| 127 |
def get_datacenter_info(self): |
| 128 |
self.datacenter_name = self.params.get('datacenter') |
| 129 |
results = dict( |
| 130 |
changed=False, |
| 131 |
datacenter_info=[], |
| 132 |
) |
| 133 |
|
| 134 |
datacenter_objs = self.get_managed_objects_properties(vim_type=vim.Datacenter, properties=['name']) |
| 135 |
dcs = [] |
| 136 |
for dc_obj in datacenter_objs: |
| 137 |
if len(dc_obj.propSet) == 1: |
| 138 |
if self.datacenter_name is not None: |
| 139 |
if dc_obj.propSet[0].val == to_native(self.datacenter_name): |
| 140 |
dcs.append(dc_obj.obj) |
| 141 |
continue |
| 142 |
else: |
| 143 |
dcs.append(dc_obj.obj) |
| 144 |
|
| 145 |
for obj in dcs: |
| 146 |
if obj is None: |
| 147 |
continue |
| 148 |
temp_dc = dict( |
| 149 |
name=obj.name, |
| 150 |
moid=obj._moId, |
| 151 |
) |
| 152 |
if self.module.params['schema'] == 'summary': |
| 153 |
temp_dc.update( |
| 154 |
dict( |
| 155 |
config_status=obj.configStatus, |
| 156 |
overall_status=obj.overallStatus, |
| 157 |
) |
| 158 |
) |
| 159 |
else: |
| 160 |
temp_dc.update(self.to_json(obj, self.params.get('properties'))) |
| 161 |
if self.params.get('show_tag'): |
| 162 |
temp_dc.update({ |
| 163 |
'tags': self.vmware_client.get_tags_for_datacenter(datacenter_mid=obj._moId) |
| 164 |
}) |
| 165 |
|
| 166 |
results['datacenter_info'].append(temp_dc) |
| 167 |
self.module.exit_json(**results) |
| 168 |
|
| 169 |
|
| 170 |
def main(): |
| 171 |
argument_spec = base_argument_spec() |
| 172 |
argument_spec.update( |
| 173 |
dict( |
| 174 |
datacenter=dict(type='str', aliases=['datacenter_name']), |
| 175 |
schema=dict(type='str', choices=['summary', 'vsphere'], default='summary'), |
| 176 |
properties=dict(type='list', elements='str'), |
| 177 |
show_tag=dict(type='bool', default=False), |
| 178 |
) |
| 179 |
) |
| 180 |
module = AnsibleModule( |
| 181 |
argument_spec=argument_spec, |
| 182 |
supports_check_mode=True |
| 183 |
) |
| 184 |
|
| 185 |
vmware_datacenter_mgr = VmwareDatacenterInfo(module) |
| 186 |
vmware_datacenter_mgr.get_datacenter_info() |
| 187 |
|
| 188 |
|
| 189 |
if __name__ == '__main__': |
| 190 |
main() |