| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Ansible Project |
| 5 |
# Copyright: (c) 2018, 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_guest_customization_info |
| 16 |
short_description: Gather info about VM customization specifications |
| 17 |
description: |
| 18 |
- This module can be used to gather information about customization specifications. |
| 19 |
author: |
| 20 |
- Abhijeet Kasurde (@Akasurde) |
| 21 |
options: |
| 22 |
spec_name: |
| 23 |
description: |
| 24 |
- Name of customization specification to find. |
| 25 |
required: false |
| 26 |
type: str |
| 27 |
extends_documentation_fragment: |
| 28 |
- vmware.vmware.base_options |
| 29 |
|
| 30 |
''' |
| 31 |
|
| 32 |
EXAMPLES = r''' |
| 33 |
- name: Gather info about all customization specification |
| 34 |
community.vmware.vmware_guest_customization_info: |
| 35 |
hostname: "{{ vcenter_hostname }}" |
| 36 |
username: "{{ vcenter_username }}" |
| 37 |
password: "{{ vcenter_password }}" |
| 38 |
delegate_to: localhost |
| 39 |
register: all_custom_spec_info |
| 40 |
|
| 41 |
- name: Gather info about customization specification with the given name |
| 42 |
community.vmware.vmware_guest_customization_info: |
| 43 |
hostname: "{{ vcenter_hostname }}" |
| 44 |
username: "{{ vcenter_username }}" |
| 45 |
password: "{{ vcenter_password }}" |
| 46 |
spec_name: custom_linux_spec |
| 47 |
delegate_to: localhost |
| 48 |
register: custom_spec_info |
| 49 |
''' |
| 50 |
|
| 51 |
RETURN = r''' |
| 52 |
custom_spec_info: |
| 53 |
description: metadata about the customization specification |
| 54 |
returned: always |
| 55 |
type: dict |
| 56 |
sample: { |
| 57 |
"assignip-eee0d684-44b7-457c-8c55-2585590b0d99": { |
| 58 |
"change_version": "1523438001", |
| 59 |
"description": "sample description", |
| 60 |
"dns_server_list": [], |
| 61 |
"dns_suffix_list": [], |
| 62 |
"domain": "None", |
| 63 |
"hostname": "sample1", |
| 64 |
"hw_clock_utc": null, |
| 65 |
"last_updated_time": "2018-04-11T09:13:21+00:00", |
| 66 |
"name": "sample", |
| 67 |
"nic_setting_map": [ |
| 68 |
{ |
| 69 |
"dns_domain": null, |
| 70 |
"gateway": [], |
| 71 |
"ip_address": "192.168.10.10", |
| 72 |
"net_bios": null, |
| 73 |
"nic_dns_server_list": [], |
| 74 |
"primary_wins": null, |
| 75 |
"secondry_wins": null, |
| 76 |
"subnet_mask": "255.255.255.0" |
| 77 |
} |
| 78 |
], |
| 79 |
"time_zone": null, |
| 80 |
"type": "Linux" |
| 81 |
}, |
| 82 |
} |
| 83 |
''' |
| 84 |
|
| 85 |
try: |
| 86 |
from pyVmomi import vim |
| 87 |
except ImportError: |
| 88 |
pass |
| 89 |
|
| 90 |
from ansible.module_utils.basic import AnsibleModule |
| 91 |
from ansible.module_utils.common.text.converters import to_text |
| 92 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 93 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 94 |
|
| 95 |
|
| 96 |
class VmwareCustomSpecManger(PyVmomi): |
| 97 |
def __init__(self, module): |
| 98 |
super(VmwareCustomSpecManger, self).__init__(module) |
| 99 |
self.cc_mgr = self.content.customizationSpecManager |
| 100 |
if self.cc_mgr is None: |
| 101 |
self.module.fail_json(msg="Failed to get customization spec manager.") |
| 102 |
|
| 103 |
def gather_custom_spec_info(self): |
| 104 |
""" |
| 105 |
Gather information about customization specifications |
| 106 |
""" |
| 107 |
|
| 108 |
spec_name = self.params.get('spec_name', None) |
| 109 |
specs_list = [] |
| 110 |
if spec_name: |
| 111 |
if self.cc_mgr.DoesCustomizationSpecExist(name=spec_name): |
| 112 |
specs_list.append(spec_name) |
| 113 |
else: |
| 114 |
self.module.fail_json(msg="Unable to find customization specification named '%s'" % spec_name) |
| 115 |
else: |
| 116 |
available_specs = self.cc_mgr.info |
| 117 |
for spec_info in available_specs: |
| 118 |
specs_list.append(spec_info.name) |
| 119 |
|
| 120 |
spec_info = dict() |
| 121 |
for spec in specs_list: |
| 122 |
current_spec = self.cc_mgr.GetCustomizationSpec(name=spec) |
| 123 |
adapter_mapping_list = [] |
| 124 |
for nic in current_spec.spec.nicSettingMap: |
| 125 |
temp_data = dict( |
| 126 |
mac_address=nic.macAddress, |
| 127 |
ip_address=nic.adapter.ip.ipAddress if hasattr(nic.adapter.ip, 'ipAddress') else None, |
| 128 |
subnet_mask=nic.adapter.subnetMask, |
| 129 |
gateway=list(nic.adapter.gateway), |
| 130 |
nic_dns_server_list=list(nic.adapter.dnsServerList), |
| 131 |
dns_domain=nic.adapter.dnsDomain, |
| 132 |
primary_wins=nic.adapter.primaryWINS, |
| 133 |
secondry_wins=nic.adapter.secondaryWINS, |
| 134 |
net_bios=nic.adapter.netBIOS, |
| 135 |
) |
| 136 |
adapter_mapping_list.append(temp_data) |
| 137 |
|
| 138 |
# Set the following variables from parameters in LnuxPrep or SysPrep |
| 139 |
current_hostname = None |
| 140 |
domain = None |
| 141 |
time_zone = None |
| 142 |
hw_clock = None |
| 143 |
if isinstance(current_spec.spec.identity, vim.vm.customization.LinuxPrep): |
| 144 |
if isinstance(current_spec.spec.identity.hostName, vim.vm.customization.PrefixNameGenerator): |
| 145 |
current_hostname = current_spec.spec.identity.hostName.base |
| 146 |
elif isinstance(current_spec.spec.identity.hostName, vim.vm.customization.FixedName): |
| 147 |
current_hostname = current_spec.spec.identity.hostName.name |
| 148 |
domain = current_spec.spec.identity.domain |
| 149 |
time_zone = current_spec.spec.identity.timeZone |
| 150 |
hw_clock = current_spec.spec.identity.hwClockUTC |
| 151 |
else: |
| 152 |
time_zone = current_spec.spec.identity.guiUnattended.timeZone |
| 153 |
|
| 154 |
spec_info[spec] = dict( |
| 155 |
# Spec |
| 156 |
name=current_spec.info.name, |
| 157 |
description=current_spec.info.description, |
| 158 |
type=current_spec.info.type, |
| 159 |
last_updated_time=current_spec.info.lastUpdateTime, |
| 160 |
change_version=current_spec.info.changeVersion, |
| 161 |
# Identity |
| 162 |
hostname=current_hostname, |
| 163 |
domain=domain, |
| 164 |
time_zone=time_zone, |
| 165 |
hw_clock_utc=hw_clock, |
| 166 |
# global IP Settings |
| 167 |
dns_suffix_list=list(current_spec.spec.globalIPSettings.dnsSuffixList), |
| 168 |
dns_server_list=list(current_spec.spec.globalIPSettings.dnsServerList), |
| 169 |
# NIC setting map |
| 170 |
nic_setting_map=adapter_mapping_list, |
| 171 |
) |
| 172 |
return spec_info |
| 173 |
|
| 174 |
|
| 175 |
def main(): |
| 176 |
argument_spec = base_argument_spec() |
| 177 |
argument_spec.update( |
| 178 |
spec_name=dict(type='str'), |
| 179 |
) |
| 180 |
module = AnsibleModule( |
| 181 |
argument_spec=argument_spec, |
| 182 |
supports_check_mode=True |
| 183 |
) |
| 184 |
|
| 185 |
pyv = VmwareCustomSpecManger(module) |
| 186 |
try: |
| 187 |
module.exit_json(custom_spec_info=pyv.gather_custom_spec_info()) |
| 188 |
except Exception as exc: |
| 189 |
module.fail_json(msg="Failed to gather information with exception : %s" % to_text(exc)) |
| 190 |
|
| 191 |
|
| 192 |
if __name__ == '__main__': |
| 193 |
main() |