| 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_package_info |
| 15 |
short_description: Gathers info about available packages on an ESXi host |
| 16 |
description: |
| 17 |
- This module can be used to gather information about available packages and their status on an ESXi host. |
| 18 |
author: |
| 19 |
- Abhijeet Kasurde (@Akasurde) |
| 20 |
options: |
| 21 |
cluster_name: |
| 22 |
description: |
| 23 |
- Name of the cluster. |
| 24 |
- Package information about each ESXi server will be returned for given cluster. |
| 25 |
- If O(esxi_hostname) is not given, this parameter is required. |
| 26 |
type: str |
| 27 |
esxi_hostname: |
| 28 |
description: |
| 29 |
- ESXi hostname. |
| 30 |
- Package information about this ESXi server will be returned. |
| 31 |
- If O(cluster_name) is not given, this parameter is required. |
| 32 |
type: str |
| 33 |
extends_documentation_fragment: |
| 34 |
- vmware.vmware.base_options |
| 35 |
|
| 36 |
''' |
| 37 |
|
| 38 |
EXAMPLES = r''' |
| 39 |
- name: Gather info about all ESXi Host in given Cluster |
| 40 |
community.vmware.vmware_host_package_info: |
| 41 |
hostname: '{{ vcenter_hostname }}' |
| 42 |
username: '{{ vcenter_username }}' |
| 43 |
password: '{{ vcenter_password }}' |
| 44 |
cluster_name: cluster_name |
| 45 |
delegate_to: localhost |
| 46 |
register: cluster_host_packages |
| 47 |
|
| 48 |
- name: Gather info about ESXi Host |
| 49 |
community.vmware.vmware_host_package_info: |
| 50 |
hostname: '{{ vcenter_hostname }}' |
| 51 |
username: '{{ vcenter_username }}' |
| 52 |
password: '{{ vcenter_password }}' |
| 53 |
esxi_hostname: '{{ esxi_hostname }}' |
| 54 |
delegate_to: localhost |
| 55 |
register: host_packages |
| 56 |
''' |
| 57 |
|
| 58 |
RETURN = r''' |
| 59 |
hosts_package_info: |
| 60 |
description: |
| 61 |
- dict with hostname as key and dict with package information as value |
| 62 |
returned: hosts_package_info |
| 63 |
type: dict |
| 64 |
sample: { "hosts_package_info": { "localhost.localdomain": []}} |
| 65 |
''' |
| 66 |
|
| 67 |
from ansible.module_utils.basic import AnsibleModule |
| 68 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 69 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 70 |
|
| 71 |
|
| 72 |
class VmwarePackageManager(PyVmomi): |
| 73 |
def __init__(self, module): |
| 74 |
super(VmwarePackageManager, self).__init__(module) |
| 75 |
cluster_name = self.params.get('cluster_name', None) |
| 76 |
esxi_host_name = self.params.get('esxi_hostname', None) |
| 77 |
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name) |
| 78 |
|
| 79 |
def gather_package_info(self): |
| 80 |
hosts_info = {} |
| 81 |
for host in self.hosts: |
| 82 |
host_package_info = [] |
| 83 |
host_pkg_mgr = host.configManager.imageConfigManager |
| 84 |
if host_pkg_mgr: |
| 85 |
pkgs = host_pkg_mgr.FetchSoftwarePackages() |
| 86 |
for pkg in pkgs: |
| 87 |
host_package_info.append(dict( |
| 88 |
name=pkg.name, |
| 89 |
version=pkg.version, |
| 90 |
vendor=pkg.vendor, |
| 91 |
summary=pkg.summary, |
| 92 |
description=pkg.description, |
| 93 |
acceptance_level=pkg.acceptanceLevel, |
| 94 |
maintenance_mode_required=pkg.maintenanceModeRequired, |
| 95 |
creation_date=pkg.creationDate, |
| 96 |
)) |
| 97 |
hosts_info[host.name] = host_package_info |
| 98 |
return hosts_info |
| 99 |
|
| 100 |
|
| 101 |
def main(): |
| 102 |
argument_spec = base_argument_spec() |
| 103 |
argument_spec.update( |
| 104 |
cluster_name=dict(type='str', required=False), |
| 105 |
esxi_hostname=dict(type='str', required=False), |
| 106 |
) |
| 107 |
|
| 108 |
module = AnsibleModule( |
| 109 |
argument_spec=argument_spec, |
| 110 |
required_one_of=[ |
| 111 |
['cluster_name', 'esxi_hostname'], |
| 112 |
], |
| 113 |
supports_check_mode=True, |
| 114 |
) |
| 115 |
|
| 116 |
vmware_host_package_config = VmwarePackageManager(module) |
| 117 |
module.exit_json(changed=False, hosts_package_info=vmware_host_package_config.gather_package_info()) |
| 118 |
|
| 119 |
|
| 120 |
if __name__ == "__main__": |
| 121 |
main() |