| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Michael Tipton <mike () ibeta.org> |
| 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 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = r''' |
| 13 |
--- |
| 14 |
module: vcenter_extension |
| 15 |
short_description: Register/deregister vCenter Extensions |
| 16 |
description: |
| 17 |
- This module can be used to register/deregister vCenter Extensions. |
| 18 |
author: |
| 19 |
- Michael Tipton (@castawayegr) |
| 20 |
options: |
| 21 |
extension_key: |
| 22 |
description: |
| 23 |
- The extension key of the extension to install or uninstall. |
| 24 |
required: true |
| 25 |
type: str |
| 26 |
version: |
| 27 |
description: |
| 28 |
- The version of the extension you are installing or uninstalling. |
| 29 |
required: true |
| 30 |
type: str |
| 31 |
name: |
| 32 |
description: |
| 33 |
- Required for O(state=present). The name of the extension you are installing. |
| 34 |
type: str |
| 35 |
company: |
| 36 |
description: |
| 37 |
- Required for O(state=present). The name of the company that makes the extension. |
| 38 |
type: str |
| 39 |
description: |
| 40 |
description: |
| 41 |
- Required for O(state=present). A short description of the extension. |
| 42 |
type: str |
| 43 |
email: |
| 44 |
description: |
| 45 |
- Required for O(state=present). Administrator email to use for extension. |
| 46 |
type: str |
| 47 |
url: |
| 48 |
description: |
| 49 |
- Required for O(state=present). Link to server hosting extension zip file to install. |
| 50 |
type: str |
| 51 |
ssl_thumbprint: |
| 52 |
description: |
| 53 |
- Required for O(state=present). SSL thumbprint of the extension hosting server. |
| 54 |
type: str |
| 55 |
server_type: |
| 56 |
description: |
| 57 |
- Required for O(state=present). Type of server being used to install the extension (SOAP, REST, HTTP, etc.). |
| 58 |
default: vsphere-client-serenity |
| 59 |
type: str |
| 60 |
client_type: |
| 61 |
description: |
| 62 |
- Required for O(state=present). Type of client the extension is (win32, .net, linux, etc.). |
| 63 |
default: vsphere-client-serenity |
| 64 |
type: str |
| 65 |
visible: |
| 66 |
description: |
| 67 |
- Show the extension in solution manager inside vCenter. |
| 68 |
default: true |
| 69 |
type: bool |
| 70 |
state: |
| 71 |
description: |
| 72 |
- Add or remove vCenter Extension. |
| 73 |
choices: [absent, present] |
| 74 |
default: present |
| 75 |
type: str |
| 76 |
extends_documentation_fragment: |
| 77 |
- vmware.vmware.base_options |
| 78 |
|
| 79 |
''' |
| 80 |
|
| 81 |
EXAMPLES = r''' |
| 82 |
- name: Register vCenter Extension |
| 83 |
community.vmware.vcenter_extension: |
| 84 |
hostname: "{{ groups['vcsa'][0] }}" |
| 85 |
username: "{{ vcenter_username }}" |
| 86 |
password: "{{ site_password }}" |
| 87 |
extension_key: "{{ extension_key }}" |
| 88 |
version: "1.0" |
| 89 |
company: "Acme" |
| 90 |
name: "Acme Extension" |
| 91 |
description: "acme management" |
| 92 |
email: "[email protected]" |
| 93 |
url: "https://10.0.0.1/ACME-vSphere-web-plugin-1.0.zip" |
| 94 |
ssl_thumbprint: "{{ ssl_thumbprint }}" |
| 95 |
state: present |
| 96 |
delegate_to: localhost |
| 97 |
register: register_extension |
| 98 |
|
| 99 |
- name: Deregister vCenter Extension |
| 100 |
community.vmware.vcenter_extension: |
| 101 |
hostname: "{{ groups['vcsa'][0] }}" |
| 102 |
username: "{{ vcenter_username }}" |
| 103 |
password: "{{ site_password }}" |
| 104 |
extension_key: "{{ extension_key }}" |
| 105 |
version: "1.0" |
| 106 |
state: absent |
| 107 |
delegate_to: localhost |
| 108 |
register: deregister_extension |
| 109 |
''' |
| 110 |
|
| 111 |
RETURN = r''' |
| 112 |
result: |
| 113 |
description: information about performed operation |
| 114 |
returned: always |
| 115 |
type: str |
| 116 |
sample: "'com.acme.Extension' installed." |
| 117 |
''' |
| 118 |
|
| 119 |
try: |
| 120 |
from pyVmomi import vim |
| 121 |
except ImportError: |
| 122 |
pass |
| 123 |
|
| 124 |
import datetime |
| 125 |
|
| 126 |
from ansible.module_utils.basic import AnsibleModule |
| 127 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 128 |
from ansible_collections.vmware.vmware.plugins.module_utils.clients.pyvmomi import PyvmomiClient |
| 129 |
|
| 130 |
|
| 131 |
def main(): |
| 132 |
argument_spec = base_argument_spec() |
| 133 |
argument_spec.update(dict( |
| 134 |
extension_key=dict(type='str', required=True, no_log=False), |
| 135 |
version=dict(type='str', required=True), |
| 136 |
email=dict(type='str', required=False), |
| 137 |
description=dict(type='str', required=False), |
| 138 |
company=dict(type='str', required=False), |
| 139 |
name=dict(type='str', required=False), |
| 140 |
url=dict(type='str', required=False), |
| 141 |
ssl_thumbprint=dict(type='str', required=False), |
| 142 |
client_type=dict(type='str', default='vsphere-client-serenity', required=False), |
| 143 |
server_type=dict(type='str', default='vsphere-client-serenity', required=False), |
| 144 |
visible=dict(type='bool', default='True', required=False), |
| 145 |
state=dict(type='str', default='present', choices=['absent', 'present']), |
| 146 |
)) |
| 147 |
|
| 148 |
module = AnsibleModule( |
| 149 |
argument_spec=argument_spec, |
| 150 |
supports_check_mode=False, |
| 151 |
required_if=[ |
| 152 |
['state', 'present', ['email', 'description', 'company', 'name', 'url', 'ssl_thumbprint', 'server_type', 'client_type']] |
| 153 |
] |
| 154 |
) |
| 155 |
|
| 156 |
state = module.params['state'] |
| 157 |
extension_key = module.params['extension_key'] |
| 158 |
version = module.params['version'] |
| 159 |
email = module.params['email'] |
| 160 |
desc = module.params['description'] |
| 161 |
name = module.params['name'] |
| 162 |
company = module.params['company'] |
| 163 |
client_type = module.params['client_type'] |
| 164 |
server_type = module.params['server_type'] |
| 165 |
url = module.params['url'] |
| 166 |
visible = module.params['visible'] |
| 167 |
thumbprint = module.params['ssl_thumbprint'] |
| 168 |
|
| 169 |
pyvmomi_client = PyvmomiClient( |
| 170 |
hostname=module.params.get('hostname'), |
| 171 |
username=module.params.get('username'), |
| 172 |
password=module.params.get('password'), |
| 173 |
port=module.params.get('port'), |
| 174 |
validate_certs=module.params.get('validate_certs') |
| 175 |
) |
| 176 |
em = pyvmomi_client.content.extensionManager |
| 177 |
key_check = em.FindExtension(extension_key) |
| 178 |
results = dict(changed=False, installed=dict()) |
| 179 |
|
| 180 |
if state == 'present' and key_check: |
| 181 |
results['changed'] = False |
| 182 |
results['installed'] = "'%s' is already installed" % (extension_key) |
| 183 |
|
| 184 |
elif state == 'present' and not key_check: |
| 185 |
extension = vim.Extension() |
| 186 |
extension.key = extension_key |
| 187 |
extension.company = company |
| 188 |
extension.version = version |
| 189 |
extension.lastHeartbeatTime = datetime.datetime.now() |
| 190 |
description = vim.Description() |
| 191 |
description.label = name |
| 192 |
description.summary = desc |
| 193 |
extension.description = description |
| 194 |
extension.shownInSolutionManager = visible |
| 195 |
|
| 196 |
client = vim.Extension.ClientInfo() |
| 197 |
client.company = company |
| 198 |
client.version = version |
| 199 |
client.description = description |
| 200 |
client.type = client_type |
| 201 |
client.url = url |
| 202 |
extension.client = [client] |
| 203 |
|
| 204 |
server = vim.Extension.ServerInfo() |
| 205 |
server.company = company |
| 206 |
server.description = description |
| 207 |
server.type = server_type |
| 208 |
server.adminEmail = email |
| 209 |
server.serverThumbprint = thumbprint |
| 210 |
server.url = url |
| 211 |
extension.server = [server] |
| 212 |
|
| 213 |
em.RegisterExtension(extension) |
| 214 |
results['changed'] = True |
| 215 |
results['installed'] = "'%s' installed." % (extension_key) |
| 216 |
|
| 217 |
elif state == 'absent' and key_check: |
| 218 |
em.UnregisterExtension(extension_key) |
| 219 |
results['changed'] = True |
| 220 |
results['installed'] = "'%s' uninstalled." % (extension_key) |
| 221 |
|
| 222 |
elif state == 'absent' and not key_check: |
| 223 |
results['changed'] = False |
| 224 |
results['installed'] = "'%s' is not installed." % (extension_key) |
| 225 |
|
| 226 |
module.exit_json(**results) |
| 227 |
|
| 228 |
|
| 229 |
if __name__ == '__main__': |
| 230 |
main() |