| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2020, sky-joker |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
|
| 13 |
DOCUMENTATION = r""" |
| 14 |
module: vmware_vcenter_settings_info |
| 15 |
short_description: Gather info vCenter settings |
| 16 |
description: |
| 17 |
- This module can be used to gather information about vCenter settings. |
| 18 |
author: |
| 19 |
- sky-joker (@sky-joker) |
| 20 |
options: |
| 21 |
schema: |
| 22 |
description: |
| 23 |
- Specify the output schema desired. |
| 24 |
- The 'summary' output schema is the legacy output from the module. |
| 25 |
- The 'vsphere' output schema is the vSphere API class definition. |
| 26 |
choices: ['summary', 'vsphere'] |
| 27 |
default: 'summary' |
| 28 |
type: str |
| 29 |
properties: |
| 30 |
description: |
| 31 |
- Specify the properties to retrieve. |
| 32 |
- 'Example:' |
| 33 |
- ' properties: [' |
| 34 |
- ' "config.workflow.port"' |
| 35 |
- ' ]' |
| 36 |
- Only valid when O(schema=vsphere). |
| 37 |
type: list |
| 38 |
elements: str |
| 39 |
extends_documentation_fragment: |
| 40 |
- vmware.vmware.base_options |
| 41 |
""" |
| 42 |
|
| 43 |
EXAMPLES = r""" |
| 44 |
- name: "Gather info about vCenter settings" |
| 45 |
community.vmware.vmware_vcenter_settings_info: |
| 46 |
hostname: "{{ vcenter_hostname }}" |
| 47 |
username: "{{ vcenter_username }}" |
| 48 |
password: "{{ vcenter_password }}" |
| 49 |
register: vcenter_settings_info |
| 50 |
|
| 51 |
- name: "Gather some info from vCenter using the vSphere API output schema" |
| 52 |
community.vmware.vmware_vcenter_settings_info: |
| 53 |
hostname: "{{ vcenter_hostname }}" |
| 54 |
username: "{{ vcenter_username }}" |
| 55 |
password: "{{ vcenter_password }}" |
| 56 |
schema: vsphere |
| 57 |
properties: |
| 58 |
- config.workflow.port |
| 59 |
register: vcenter_settings_info_vsphere_api |
| 60 |
""" |
| 61 |
|
| 62 |
RETURN = r""" |
| 63 |
vcenter_config_info: |
| 64 |
description: dict of vCenter settings |
| 65 |
returned: success |
| 66 |
type: dict |
| 67 |
sample: | |
| 68 |
{ |
| 69 |
"db_event_cleanup_previous": true, |
| 70 |
"db_event_retention_previous": 30, |
| 71 |
"db_max_connections_previous": 50, |
| 72 |
"db_task_cleanup_previous": true, |
| 73 |
"db_task_retention_previous": 30, |
| 74 |
"directory_query_limit_previous": true, |
| 75 |
"directory_query_limit_size_previous": 5000, |
| 76 |
"directory_timeout_previous": 60, |
| 77 |
"directory_validation_period_previous": 1440, |
| 78 |
"directory_validation_previous": true, |
| 79 |
"logging_options_previous": "info", |
| 80 |
"mail_sender_previous": "", |
| 81 |
"mail_server_previous": "", |
| 82 |
"runtime_managed_address_previous": "", |
| 83 |
"runtime_server_name_previous": "vcenter.local", |
| 84 |
"runtime_unique_id_previous": 48, |
| 85 |
"snmp_1_community_previous": "public", |
| 86 |
"snmp_1_enabled_previous": true, |
| 87 |
"snmp_1_url_previous": "localhost", |
| 88 |
"snmp_2_community_previous": "", |
| 89 |
"snmp_2_enabled_previous": false, |
| 90 |
"snmp_2_url_previous": "", |
| 91 |
"snmp_3_community_previous": "", |
| 92 |
"snmp_3_enabled_previous": false, |
| 93 |
"snmp_3_url_previous": "", |
| 94 |
"snmp_4_community_previous": "", |
| 95 |
"snmp_4_enabled_previous": false, |
| 96 |
"snmp_4_url_previous": "", |
| 97 |
"snmp_receiver_1_port_previous": 162, |
| 98 |
"snmp_receiver_2_port_previous": 162, |
| 99 |
"snmp_receiver_3_port_previous": 162, |
| 100 |
"snmp_receiver_4_port_previous": 162, |
| 101 |
"timeout_long_operations_previous": 120, |
| 102 |
"timeout_normal_operations_previous": 30 |
| 103 |
} |
| 104 |
""" |
| 105 |
|
| 106 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 107 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 108 |
from ansible.module_utils.basic import AnsibleModule |
| 109 |
|
| 110 |
|
| 111 |
class VmwareVcenterSettingsInfo(PyVmomi): |
| 112 |
def __init__(self, module): |
| 113 |
super(VmwareVcenterSettingsInfo, self).__init__(module) |
| 114 |
self.schema = self.params["schema"] |
| 115 |
self.properties = self.params["properties"] |
| 116 |
|
| 117 |
if not self.is_vcenter(): |
| 118 |
self.module.fail_json(msg="You have to connect to a vCenter server!") |
| 119 |
|
| 120 |
def ensure(self): |
| 121 |
result = {} |
| 122 |
exists_vcenter_config = {} |
| 123 |
option_manager = self.content.setting |
| 124 |
|
| 125 |
for setting in option_manager.setting: |
| 126 |
exists_vcenter_config[setting.key] = setting.value |
| 127 |
|
| 128 |
if self.schema == "summary": |
| 129 |
common_name_value_map = { |
| 130 |
# Database |
| 131 |
"VirtualCenter.MaxDBConnection": "db_max_connections_previous", |
| 132 |
"task.maxAgeEnabled": "db_task_cleanup_previous", |
| 133 |
"task.maxAge": "db_task_retention_previous", |
| 134 |
"event.maxAgeEnabled": "db_event_cleanup_previous", |
| 135 |
"event.maxAge": "db_event_retention_previous", |
| 136 |
# Runtime settings |
| 137 |
"instance.id": "runtime_unique_id_previous", |
| 138 |
"VirtualCenter.ManagedIP": "runtime_managed_address_previous", |
| 139 |
"VirtualCenter.InstanceName": "runtime_server_name_previous", |
| 140 |
# User directory |
| 141 |
"ads.timeout": "directory_timeout_previous", |
| 142 |
"ads.maxFetchEnabled": "directory_query_limit_previous", |
| 143 |
"ads.maxFetch": "directory_query_limit_size_previous", |
| 144 |
"ads.checkIntervalEnabled": "directory_validation_previous", |
| 145 |
"ads.checkInterval": "directory_validation_period_previous", |
| 146 |
# Mail |
| 147 |
"mail.smtp.server": "mail_server_previous", |
| 148 |
"mail.sender": "mail_sender_previous", |
| 149 |
# SNMP receivers - SNMP receiver #1 |
| 150 |
"snmp.receiver.1.enabled": "snmp_1_enabled_previous", |
| 151 |
"snmp.receiver.1.name": "snmp_1_url_previous", |
| 152 |
"snmp.receiver.1.port": "snmp_receiver_1_port_previous", |
| 153 |
"snmp.receiver.1.community": "snmp_1_community_previous", |
| 154 |
# SNMP receivers - SNMP receiver #2 |
| 155 |
"snmp.receiver.2.enabled": "snmp_2_enabled_previous", |
| 156 |
"snmp.receiver.2.name": "snmp_2_url_previous", |
| 157 |
"snmp.receiver.2.port": "snmp_receiver_2_port_previous", |
| 158 |
"snmp.receiver.2.community": "snmp_2_community_previous", |
| 159 |
# SNMP receivers - SNMP receiver #3 |
| 160 |
"snmp.receiver.3.enabled": "snmp_3_enabled_previous", |
| 161 |
"snmp.receiver.3.name": "snmp_3_url_previous", |
| 162 |
"snmp.receiver.3.port": "snmp_receiver_3_port_previous", |
| 163 |
"snmp.receiver.3.community": "snmp_3_community_previous", |
| 164 |
# SNMP receivers - SNMP receiver #4 |
| 165 |
"snmp.receiver.4.enabled": "snmp_4_enabled_previous", |
| 166 |
"snmp.receiver.4.name": "snmp_4_url_previous", |
| 167 |
"snmp.receiver.4.port": "snmp_receiver_4_port_previous", |
| 168 |
"snmp.receiver.4.community": "snmp_4_community_previous", |
| 169 |
# Timeout settings |
| 170 |
"client.timeout.normal": "timeout_normal_operations_previous", |
| 171 |
"client.timeout.long": "timeout_long_operations_previous", |
| 172 |
# Logging settings |
| 173 |
"log.level": "logging_options_previous", |
| 174 |
} |
| 175 |
|
| 176 |
for key, value in common_name_value_map.items(): |
| 177 |
if key in exists_vcenter_config: |
| 178 |
result[value] = setting.value |
| 179 |
else: |
| 180 |
if self.properties: |
| 181 |
for property in self.properties: |
| 182 |
if property in exists_vcenter_config: |
| 183 |
result[property] = exists_vcenter_config[property] |
| 184 |
else: |
| 185 |
self.module.fail_json(msg="Propety '%s' not found" % property) |
| 186 |
else: |
| 187 |
for property in exists_vcenter_config.keys(): |
| 188 |
result[property] = exists_vcenter_config[property] |
| 189 |
|
| 190 |
self.module.exit_json(changed=False, vcenter_config_info=result) |
| 191 |
|
| 192 |
|
| 193 |
def main(): |
| 194 |
argument_spec = base_argument_spec() |
| 195 |
argument_spec.update( |
| 196 |
schema=dict(type="str", choices=["summary", "vsphere"], default="summary"), |
| 197 |
properties=dict(type="list", elements="str"), |
| 198 |
) |
| 199 |
|
| 200 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 201 |
|
| 202 |
vmware_vcenter_settings_info = VmwareVcenterSettingsInfo(module) |
| 203 |
vmware_vcenter_settings_info.ensure() |
| 204 |
|
| 205 |
|
| 206 |
if __name__ == "__main__": |
| 207 |
main() |