VMware: vcenter_license fixes (#50794)
* Updated document fragment * Used PyVmomi Helper class * Added check for vcenter server Fixes: #50752 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
parent
8dfdd9abf3
commit
85dd0a7e74
2 changed files with 72 additions and 26 deletions
|
@ -43,7 +43,7 @@ notes:
|
||||||
an evaluation license only.
|
an evaluation license only.
|
||||||
- The evaluation license (00000-00000-00000-00000-00000) is not listed
|
- The evaluation license (00000-00000-00000-00000-00000) is not listed
|
||||||
when unused.
|
when unused.
|
||||||
extends_documentation_fragment: vmware.documentation
|
extends_documentation_fragment: vmware.vcenter_documentation
|
||||||
'''
|
'''
|
||||||
|
|
||||||
EXAMPLES = r'''
|
EXAMPLES = r'''
|
||||||
|
@ -83,24 +83,27 @@ except ImportError:
|
||||||
HAS_PYVMOMI = False
|
HAS_PYVMOMI = False
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
from ansible.module_utils.vmware import connect_to_api, vmware_argument_spec
|
from ansible.module_utils.vmware import PyVmomi, vmware_argument_spec
|
||||||
|
|
||||||
|
|
||||||
def find_key(licenses, license):
|
class VcenterLicenseMgr(PyVmomi):
|
||||||
for item in licenses:
|
def __init__(self, module):
|
||||||
if item.licenseKey == license:
|
super(VcenterLicenseMgr, self).__init__(module)
|
||||||
return item
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
def find_key(self, licenses, license):
|
||||||
|
for item in licenses:
|
||||||
|
if item.licenseKey == license:
|
||||||
|
return item
|
||||||
|
return None
|
||||||
|
|
||||||
def list_keys(licenses):
|
def list_keys(self, licenses):
|
||||||
keys = []
|
keys = []
|
||||||
for item in licenses:
|
for item in licenses:
|
||||||
# Filter out evaluation license key
|
# Filter out evaluation license key
|
||||||
if item.used is None:
|
if item.used is None:
|
||||||
continue
|
continue
|
||||||
keys.append(item.licenseKey)
|
keys.append(item.licenseKey)
|
||||||
return keys
|
return keys
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -132,13 +135,14 @@ def main():
|
||||||
diff=dict(),
|
diff=dict(),
|
||||||
)
|
)
|
||||||
|
|
||||||
if not HAS_PYVMOMI:
|
pyv = VcenterLicenseMgr(module)
|
||||||
module.fail_json(msg='pyvmomi is required for this module')
|
if not pyv.is_vcenter():
|
||||||
|
module.fail_json(msg="vcenter_license is meant for vCenter, hostname %s "
|
||||||
|
"is not vCenter server." % module.params.get('hostname'))
|
||||||
|
|
||||||
content = connect_to_api(module)
|
lm = pyv.content.licenseManager
|
||||||
lm = content.licenseManager
|
|
||||||
|
|
||||||
result['licenses'] = list_keys(lm.licenses)
|
result['licenses'] = pyv.list_keys(lm.licenses)
|
||||||
if module._diff:
|
if module._diff:
|
||||||
result['diff']['before'] = '\n'.join(result['licenses']) + '\n'
|
result['diff']['before'] = '\n'.join(result['licenses']) + '\n'
|
||||||
|
|
||||||
|
@ -151,22 +155,22 @@ def main():
|
||||||
lm.AddLicense(license, labels)
|
lm.AddLicense(license, labels)
|
||||||
|
|
||||||
# Automatically assign to current vCenter, if needed
|
# Automatically assign to current vCenter, if needed
|
||||||
key = find_key(lm.licenses, license)
|
key = pyv.find_key(lm.licenses, license)
|
||||||
if content.about.name in key.name:
|
if pyv.content.about.name in key.name:
|
||||||
try:
|
try:
|
||||||
lam = lm.licenseAssignmentManager
|
lam = lm.licenseAssignmentManager
|
||||||
lam.UpdateAssignedLicense(entity=content.about.instanceUuid, licenseKey=license)
|
lam.UpdateAssignedLicense(entity=pyv.content.about.instanceUuid, licenseKey=license)
|
||||||
except Exception:
|
except Exception:
|
||||||
module.warn('Could not assign "%s" (%s) to vCenter.' % (license, key.name))
|
module.warn('Could not assign "%s" (%s) to vCenter.' % (license, key.name))
|
||||||
|
|
||||||
result['licenses'] = list_keys(lm.licenses)
|
result['licenses'] = pyv.list_keys(lm.licenses)
|
||||||
if module._diff:
|
if module._diff:
|
||||||
result['diff']['after'] = '\n'.join(result['licenses']) + '\n'
|
result['diff']['after'] = '\n'.join(result['licenses']) + '\n'
|
||||||
|
|
||||||
elif state == 'absent' and license in result['licenses']:
|
elif state == 'absent' and license in result['licenses']:
|
||||||
|
|
||||||
# Check if key is in use
|
# Check if key is in use
|
||||||
key = find_key(lm.licenses, license)
|
key = pyv.find_key(lm.licenses, license)
|
||||||
if key.used > 0:
|
if key.used > 0:
|
||||||
module.fail_json(msg='Cannot remove key "%s", still in use %s time(s).' % (license, key.used))
|
module.fail_json(msg='Cannot remove key "%s", still in use %s time(s).' % (license, key.used))
|
||||||
|
|
||||||
|
@ -175,7 +179,7 @@ def main():
|
||||||
result['licenses'].remove(license)
|
result['licenses'].remove(license)
|
||||||
else:
|
else:
|
||||||
lm.RemoveLicense(license)
|
lm.RemoveLicense(license)
|
||||||
result['licenses'] = list_keys(lm.licenses)
|
result['licenses'] = pyv.list_keys(lm.licenses)
|
||||||
if module._diff:
|
if module._diff:
|
||||||
result['diff']['after'] = '\n'.join(result['licenses']) + '\n'
|
result['diff']['after'] = '\n'.join(result['licenses']) + '\n'
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
# Copyright: (c) 2016, Charles Paul <cpaul@ansible.com>
|
# Copyright: (c) 2016, Charles Paul <cpaul@ansible.com>
|
||||||
# Copyright: (c) 2018, Ansible Project
|
# Copyright: (c) 2018, Ansible Project
|
||||||
|
# Copyright: (c) 2019, Abhijeet Kasurde <akasurde@redhat.com>
|
||||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
|
||||||
|
|
||||||
|
@ -44,3 +45,44 @@ options:
|
||||||
default: 443
|
default: 443
|
||||||
version_added: 2.5
|
version_added: 2.5
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
# This doc fragment is specific to vcenter modules like vcenter_license
|
||||||
|
VCENTER_DOCUMENTATION = '''
|
||||||
|
options:
|
||||||
|
hostname:
|
||||||
|
description:
|
||||||
|
- The hostname or IP address of the vSphere vCenter server.
|
||||||
|
- If the value is not specified in the task, the value of environment variable C(VMWARE_HOST) will be used instead.
|
||||||
|
- Environment variable supported added in version 2.6.
|
||||||
|
type: str
|
||||||
|
username:
|
||||||
|
description:
|
||||||
|
- The username of the vSphere vCenter server.
|
||||||
|
- If the value is not specified in the task, the value of environment variable C(VMWARE_USER) will be used instead.
|
||||||
|
- Environment variable supported added in version 2.6.
|
||||||
|
type: str
|
||||||
|
aliases: [ admin, user ]
|
||||||
|
password:
|
||||||
|
description:
|
||||||
|
- The password of the vSphere vCenter server.
|
||||||
|
- If the value is not specified in the task, the value of environment variable C(VMWARE_PASSWORD) will be used instead.
|
||||||
|
- Environment variable supported added in version 2.6.
|
||||||
|
type: str
|
||||||
|
aliases: [ pass, pwd ]
|
||||||
|
validate_certs:
|
||||||
|
description:
|
||||||
|
- Allows connection when SSL certificates are not valid. Set to C(false) when certificates are not trusted.
|
||||||
|
- If the value is not specified in the task, the value of environment variable C(VMWARE_VALIDATE_CERTS) will be used instead.
|
||||||
|
- Environment variable supported added in version 2.6.
|
||||||
|
- If set to C(yes), please make sure Python >= 2.7.9 is installed on the given machine.
|
||||||
|
type: bool
|
||||||
|
default: 'yes'
|
||||||
|
port:
|
||||||
|
description:
|
||||||
|
- The port number of the vSphere vCenter server.
|
||||||
|
- If the value is not specified in the task, the value of environment variable C(VMWARE_PORT) will be used instead.
|
||||||
|
- Environment variable supported added in version 2.6.
|
||||||
|
type: int
|
||||||
|
default: 443
|
||||||
|
version_added: 2.5
|
||||||
|
'''
|
||||||
|
|
Loading…
Reference in a new issue