| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright: (c) 2017, Dag Wieers (@dagwieers) <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = r''' |
| 12 |
module: vcenter_license |
| 13 |
short_description: Manage VMware vCenter license keys |
| 14 |
description: |
| 15 |
- Add and delete vCenter, ESXi server license keys. |
| 16 |
author: |
| 17 |
- Dag Wieers (@dagwieers) |
| 18 |
options: |
| 19 |
labels: |
| 20 |
description: |
| 21 |
- The optional labels of the license key to manage in vSphere vCenter. |
| 22 |
- This is dictionary with key/value pair. |
| 23 |
default: { |
| 24 |
'source': 'ansible' |
| 25 |
} |
| 26 |
type: dict |
| 27 |
license: |
| 28 |
description: |
| 29 |
- The license key to manage in vSphere vCenter. |
| 30 |
required: true |
| 31 |
type: str |
| 32 |
state: |
| 33 |
description: |
| 34 |
- Whether to add (V(present)) or remove (V(absent)) the license key. |
| 35 |
choices: [absent, present] |
| 36 |
default: present |
| 37 |
type: str |
| 38 |
esxi_hostname: |
| 39 |
description: |
| 40 |
- The hostname of the ESXi server to which the specified license will be assigned. |
| 41 |
- This parameter is optional. |
| 42 |
type: str |
| 43 |
datacenter: |
| 44 |
description: |
| 45 |
- The datacenter name to use for the operation. |
| 46 |
type: str |
| 47 |
cluster_name: |
| 48 |
description: |
| 49 |
- Name of the cluster to apply vSAN license. |
| 50 |
type: str |
| 51 |
notes: |
| 52 |
- This module will also auto-assign the current vCenter to the license key |
| 53 |
if the product matches the license key, and vCenter us currently assigned |
| 54 |
an evaluation license only. |
| 55 |
- The evaluation license (00000-00000-00000-00000-00000) is not listed |
| 56 |
when unused. |
| 57 |
- If O(esxi_hostname) is specified, then will assign the O(license) key to |
| 58 |
the ESXi host. |
| 59 |
- If O(esxi_hostname) is not specified, then will just register the O(license) key to |
| 60 |
vCenter inventory without assigning it to an ESXi host. |
| 61 |
extends_documentation_fragment: |
| 62 |
- vmware.vmware.base_options |
| 63 |
|
| 64 |
''' |
| 65 |
|
| 66 |
EXAMPLES = r''' |
| 67 |
- name: Add a new vCenter license |
| 68 |
community.vmware.vcenter_license: |
| 69 |
hostname: '{{ vcenter_hostname }}' |
| 70 |
username: '{{ vcenter_username }}' |
| 71 |
password: '{{ vcenter_password }}' |
| 72 |
license: f600d-21ae3-5592b-249e0-cc341 |
| 73 |
state: present |
| 74 |
delegate_to: localhost |
| 75 |
|
| 76 |
- name: Remove an (unused) vCenter license |
| 77 |
community.vmware.vcenter_license: |
| 78 |
hostname: '{{ vcenter_hostname }}' |
| 79 |
username: '{{ vcenter_username }}' |
| 80 |
password: '{{ vcenter_password }}' |
| 81 |
license: f600d-21ae3-5592b-249e0-cc341 |
| 82 |
state: absent |
| 83 |
delegate_to: localhost |
| 84 |
|
| 85 |
- name: Add ESXi license and assign to the ESXi host |
| 86 |
community.vmware.vcenter_license: |
| 87 |
hostname: '{{ vcenter_hostname }}' |
| 88 |
username: '{{ vcenter_username }}' |
| 89 |
password: '{{ vcenter_password }}' |
| 90 |
esxi_hostname: '{{ esxi_hostname }}' |
| 91 |
license: f600d-21ae3-5592b-249e0-dd502 |
| 92 |
state: present |
| 93 |
delegate_to: localhost |
| 94 |
|
| 95 |
- name: Add vSAN license and assign to the given cluster |
| 96 |
community.vmware.vcenter_license: |
| 97 |
hostname: '{{ vcenter_hostname }}' |
| 98 |
username: '{{ vcenter_username }}' |
| 99 |
password: '{{ vcenter_password }}' |
| 100 |
datacenter: '{{ datacenter_name }}' |
| 101 |
cluster_name: '{{ cluster_name }}' |
| 102 |
license: f600d-21ae3-5592b-249e0-dd502 |
| 103 |
state: present |
| 104 |
delegate_to: localhost |
| 105 |
''' |
| 106 |
|
| 107 |
RETURN = r''' |
| 108 |
licenses: |
| 109 |
description: list of license keys after module executed |
| 110 |
returned: always |
| 111 |
type: list |
| 112 |
sample: |
| 113 |
- f600d-21ae3-5592b-249e0-cc341 |
| 114 |
- 143cc-0e942-b2955-3ea12-d006f |
| 115 |
''' |
| 116 |
|
| 117 |
try: |
| 118 |
from pyVmomi import vim |
| 119 |
except ImportError: |
| 120 |
pass |
| 121 |
|
| 122 |
from ansible.module_utils.basic import AnsibleModule |
| 123 |
from ansible.module_utils.common.text.converters import to_native |
| 124 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi, find_hostsystem_by_name |
| 125 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 126 |
|
| 127 |
|
| 128 |
class VcenterLicenseMgr(PyVmomi): |
| 129 |
def __init__(self, module): |
| 130 |
super(VcenterLicenseMgr, self).__init__(module) |
| 131 |
|
| 132 |
def find_key(self, licenses, license): |
| 133 |
for item in licenses: |
| 134 |
if item.licenseKey == license: |
| 135 |
return item |
| 136 |
return None |
| 137 |
|
| 138 |
def list_keys(self, licenses): |
| 139 |
keys = [] |
| 140 |
for item in licenses: |
| 141 |
# Filter out evaluation license key |
| 142 |
if item.used is None: |
| 143 |
continue |
| 144 |
keys.append(item.licenseKey) |
| 145 |
return keys |
| 146 |
|
| 147 |
|
| 148 |
def main(): |
| 149 |
argument_spec = base_argument_spec() |
| 150 |
argument_spec.update(dict( |
| 151 |
labels=dict(type='dict', default=dict(source='ansible')), |
| 152 |
license=dict(type='str', required=True), |
| 153 |
state=dict(type='str', default='present', choices=['absent', 'present']), |
| 154 |
esxi_hostname=dict(type='str'), |
| 155 |
datacenter=dict(type='str'), |
| 156 |
cluster_name=dict(type='str'), |
| 157 |
)) |
| 158 |
|
| 159 |
module = AnsibleModule( |
| 160 |
argument_spec=argument_spec, |
| 161 |
supports_check_mode=True, |
| 162 |
) |
| 163 |
|
| 164 |
license = module.params['license'] |
| 165 |
state = module.params['state'] |
| 166 |
|
| 167 |
# FIXME: This does not seem to work on vCenter v6.0 |
| 168 |
labels = [] |
| 169 |
for k in module.params['labels']: |
| 170 |
kv = vim.KeyValue() |
| 171 |
kv.key = k |
| 172 |
kv.value = module.params['labels'][k] |
| 173 |
labels.append(kv) |
| 174 |
|
| 175 |
result = dict( |
| 176 |
changed=False, |
| 177 |
diff=dict(), |
| 178 |
) |
| 179 |
|
| 180 |
pyv = VcenterLicenseMgr(module) |
| 181 |
if not pyv.is_vcenter(): |
| 182 |
module.fail_json(msg="vcenter_license is meant for vCenter, hostname %s " |
| 183 |
"is not vCenter server." % module.params.get('hostname')) |
| 184 |
|
| 185 |
allowed_license_names = [ |
| 186 |
pyv.content.about.name, |
| 187 |
'vCenter Server', |
| 188 |
'vSphere 8 Enterprise Plus for VCF', |
| 189 |
] |
| 190 |
|
| 191 |
lm = pyv.content.licenseManager |
| 192 |
|
| 193 |
result['licenses'] = pyv.list_keys(lm.licenses) |
| 194 |
if module._diff: |
| 195 |
result['diff']['before'] = '\n'.join(result['licenses']) + '\n' |
| 196 |
|
| 197 |
if state == 'present': |
| 198 |
if license not in result['licenses']: |
| 199 |
result['changed'] = True |
| 200 |
if module.check_mode: |
| 201 |
result['licenses'].append(license) |
| 202 |
else: |
| 203 |
lm.AddLicense(license, labels) |
| 204 |
|
| 205 |
key = pyv.find_key(lm.licenses, license) |
| 206 |
entityId = None |
| 207 |
if key is not None: |
| 208 |
lam = lm.licenseAssignmentManager |
| 209 |
assigned_license = None |
| 210 |
datacenter = module.params['datacenter'] |
| 211 |
datacenter_obj = None |
| 212 |
if datacenter: |
| 213 |
datacenter_obj = pyv.find_datacenter_by_name(datacenter) |
| 214 |
if not datacenter_obj: |
| 215 |
module.fail_json(msg="Unable to find the datacenter %(datacenter)s" % module.params) |
| 216 |
|
| 217 |
cluster = module.params['cluster_name'] |
| 218 |
# if cluster_name parameter is provided then search the cluster object in vcenter |
| 219 |
if cluster: |
| 220 |
cluster_obj = pyv.find_cluster_by_name(cluster_name=cluster, datacenter_name=datacenter_obj) |
| 221 |
if not cluster_obj: |
| 222 |
msg = "Unable to find the cluster %(cluster_name)s" |
| 223 |
if datacenter: |
| 224 |
msg += " in datacenter %(datacenter)s" |
| 225 |
module.fail_json(msg=msg % module.params) |
| 226 |
entityId = cluster_obj._moId |
| 227 |
# if esxi_hostname parameter is provided then search the esxi object in vcenter |
| 228 |
elif module.params['esxi_hostname']: |
| 229 |
esxi_host = find_hostsystem_by_name(pyv.content, module.params['esxi_hostname']) |
| 230 |
if esxi_host is None: |
| 231 |
module.fail_json(msg='Cannot find the specified ESXi host "%s".' % module.params['esxi_hostname']) |
| 232 |
entityId = esxi_host._moId |
| 233 |
# e.g., key.editionKey is "esx.enterprisePlus.cpuPackage", not sure all keys are in this format |
| 234 |
if 'esx' not in key.editionKey: |
| 235 |
module.warn('License key "%s" edition "%s" is not suitable for ESXi server' % (license, key.editionKey)) |
| 236 |
# backward compatibility - check if it's is a vCenter licence key |
| 237 |
elif any(name in key.name for name in allowed_license_names): |
| 238 |
entityId = pyv.content.about.instanceUuid |
| 239 |
|
| 240 |
# if we have found a cluster, an esxi or a vCenter object we try to assign the licence |
| 241 |
if entityId: |
| 242 |
try: |
| 243 |
assigned_license = lam.QueryAssignedLicenses(entityId=entityId) |
| 244 |
except Exception as e: |
| 245 |
module.fail_json(msg='Could not query vCenter "%s" assigned license info due to %s.' % (entityId, to_native(e))) |
| 246 |
|
| 247 |
if not assigned_license or (len(assigned_license) != 0 and assigned_license[0].assignedLicense.licenseKey != license): |
| 248 |
try: |
| 249 |
lam.UpdateAssignedLicense(entity=entityId, licenseKey=license) |
| 250 |
except Exception: |
| 251 |
module.fail_json(msg='Could not assign "%s" (%s) to vCenter.' % (license, key.name)) |
| 252 |
result['changed'] = True |
| 253 |
result['licenses'] = pyv.list_keys(lm.licenses) |
| 254 |
else: |
| 255 |
module.fail_json(msg='License "%s" is not existing or can not be added' % license) |
| 256 |
if module._diff: |
| 257 |
result['diff']['after'] = '\n'.join(result['licenses']) + '\n' |
| 258 |
|
| 259 |
elif state == 'absent' and license in result['licenses']: |
| 260 |
|
| 261 |
# Check if key is in use |
| 262 |
key = pyv.find_key(lm.licenses, license) |
| 263 |
if key.used > 0: |
| 264 |
module.fail_json(msg='Cannot remove key "%s", still in use %s time(s).' % (license, key.used)) |
| 265 |
|
| 266 |
result['changed'] = True |
| 267 |
if module.check_mode: |
| 268 |
result['licenses'].remove(license) |
| 269 |
else: |
| 270 |
lm.RemoveLicense(license) |
| 271 |
result['licenses'] = pyv.list_keys(lm.licenses) |
| 272 |
if module._diff: |
| 273 |
result['diff']['after'] = '\n'.join(result['licenses']) + '\n' |
| 274 |
|
| 275 |
module.exit_json(**result) |
| 276 |
|
| 277 |
|
| 278 |
if __name__ == '__main__': |
| 279 |
main() |