| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Abhijeet Kasurde <[email protected]> |
| 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: vmware_host_acceptance |
| 15 |
short_description: Manage the host acceptance level of an ESXi host |
| 16 |
description: |
| 17 |
- This module can be used to manage the host acceptance level of an ESXi host. |
| 18 |
- The host acceptance level controls the acceptance level of each VIB on a ESXi host. |
| 19 |
author: |
| 20 |
- Abhijeet Kasurde (@Akasurde) |
| 21 |
options: |
| 22 |
cluster_name: |
| 23 |
description: |
| 24 |
- Name of the cluster. |
| 25 |
- Acceptance level of all ESXi host system in the given cluster will be managed. |
| 26 |
- If O(esxi_hostname) is not given, this parameter is required. |
| 27 |
type: str |
| 28 |
esxi_hostname: |
| 29 |
description: |
| 30 |
- ESXi hostname. |
| 31 |
- Acceptance level of this ESXi host system will be managed. |
| 32 |
- If O(cluster_name) is not given, this parameter is required. |
| 33 |
type: str |
| 34 |
state: |
| 35 |
description: |
| 36 |
- If set to V(partner), then accept only partner and VMware signed and certified VIBs. |
| 37 |
- If set to V(vmware_certified), then accept only VIBs that are signed and certified by VMware. |
| 38 |
- If set to V(vmware_accepted), then accept VIBs that have been accepted by VMware. |
| 39 |
- If set to V(community), then accept all VIBs, even those that are not signed. |
| 40 |
choices: [ community, partner, vmware_accepted, vmware_certified ] |
| 41 |
required: true |
| 42 |
type: str |
| 43 |
extends_documentation_fragment: |
| 44 |
- vmware.vmware.base_options |
| 45 |
|
| 46 |
''' |
| 47 |
|
| 48 |
EXAMPLES = r''' |
| 49 |
- name: Set acceptance level to community for all ESXi Host in given Cluster |
| 50 |
community.vmware.vmware_host_acceptance: |
| 51 |
hostname: '{{ vcenter_hostname }}' |
| 52 |
username: '{{ vcenter_username }}' |
| 53 |
password: '{{ vcenter_password }}' |
| 54 |
cluster_name: cluster_name |
| 55 |
state: 'community' |
| 56 |
delegate_to: localhost |
| 57 |
register: cluster_acceptance_level |
| 58 |
|
| 59 |
- name: Set acceptance level to vmware_accepted for the given ESXi Host |
| 60 |
community.vmware.vmware_host_acceptance: |
| 61 |
hostname: '{{ vcenter_hostname }}' |
| 62 |
username: '{{ vcenter_username }}' |
| 63 |
password: '{{ vcenter_password }}' |
| 64 |
esxi_hostname: '{{ esxi_hostname }}' |
| 65 |
state: 'vmware_accepted' |
| 66 |
delegate_to: localhost |
| 67 |
register: host_acceptance_level |
| 68 |
''' |
| 69 |
|
| 70 |
RETURN = r''' |
| 71 |
facts: |
| 72 |
description: |
| 73 |
- dict with hostname as key and dict with acceptance level facts, error as value |
| 74 |
returned: facts |
| 75 |
type: dict |
| 76 |
sample: { "facts": { "localhost.localdomain": { "error": "NA", "level": "vmware_certified" }}} |
| 77 |
''' |
| 78 |
|
| 79 |
try: |
| 80 |
from pyVmomi import vim |
| 81 |
except ImportError: |
| 82 |
pass |
| 83 |
from ansible.module_utils.basic import AnsibleModule |
| 84 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 85 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 86 |
from ansible.module_utils.common.text.converters import to_native |
| 87 |
|
| 88 |
|
| 89 |
class VMwareAccpetanceManager(PyVmomi): |
| 90 |
def __init__(self, module): |
| 91 |
super(VMwareAccpetanceManager, self).__init__(module) |
| 92 |
cluster_name = self.params.get('cluster_name', None) |
| 93 |
esxi_host_name = self.params.get('esxi_hostname', None) |
| 94 |
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name) |
| 95 |
self.desired_state = self.params.get('state') |
| 96 |
self.hosts_facts = {} |
| 97 |
|
| 98 |
def set_acceptance_level(self): |
| 99 |
change = [] |
| 100 |
for host in self.hosts: |
| 101 |
self.hosts_facts[host.name] = dict(level='', error='NA') |
| 102 |
host_image_config_mgr = host.configManager.imageConfigManager |
| 103 |
if host_image_config_mgr: |
| 104 |
try: |
| 105 |
self.hosts_facts[host.name]['level'] = host_image_config_mgr.HostImageConfigGetAcceptance() |
| 106 |
except vim.fault.HostConfigFault as e: |
| 107 |
self.hosts_facts[host.name]['error'] = to_native(e.msg) |
| 108 |
host_changed = False |
| 109 |
if self.hosts_facts[host.name]['level'] != self.desired_state: |
| 110 |
try: |
| 111 |
if self.module.check_mode: |
| 112 |
self.hosts_facts[host.name]['level'] = self.desired_state |
| 113 |
else: |
| 114 |
host_image_config_mgr.UpdateHostImageAcceptanceLevel(newAcceptanceLevel=self.desired_state) |
| 115 |
self.hosts_facts[host.name]['level'] = host_image_config_mgr.HostImageConfigGetAcceptance() |
| 116 |
host_changed = True |
| 117 |
except vim.fault.HostConfigFault as e: |
| 118 |
self.hosts_facts[host.name]['error'] = to_native(e.msg) |
| 119 |
|
| 120 |
change.append(host_changed) |
| 121 |
self.module.exit_json(changed=any(change), facts=self.hosts_facts) |
| 122 |
|
| 123 |
|
| 124 |
def main(): |
| 125 |
argument_spec = base_argument_spec() |
| 126 |
argument_spec.update( |
| 127 |
cluster_name=dict(type='str', required=False), |
| 128 |
esxi_hostname=dict(type='str', required=False), |
| 129 |
state=dict(type='str', |
| 130 |
choices=['community', 'partner', 'vmware_accepted', 'vmware_certified'], |
| 131 |
required=True |
| 132 |
), |
| 133 |
) |
| 134 |
|
| 135 |
module = AnsibleModule( |
| 136 |
argument_spec=argument_spec, |
| 137 |
required_one_of=[ |
| 138 |
['cluster_name', 'esxi_hostname'], |
| 139 |
], |
| 140 |
supports_check_mode=True |
| 141 |
) |
| 142 |
|
| 143 |
vmware_host_accept_config = VMwareAccpetanceManager(module) |
| 144 |
vmware_host_accept_config.set_acceptance_level() |
| 145 |
|
| 146 |
|
| 147 |
if __name__ == "__main__": |
| 148 |
main() |