Debian Codegraph @

ansible_collections/community/vmware/plugins/modules/vmware_host_service_info.py

raw
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_service_info
15 short_description: Gathers info about an ESXi host's services
16 description:
17 - This module can be used to gather information about an ESXi host's services.
18 author:
19 - Abhijeet Kasurde (@Akasurde)
20 notes:
21 - If source package name is not available then fact is populated as null.
22 options:
23 cluster_name:
24 description:
25 - Name of the cluster.
26 - Service information about each ESXi server will be returned for given cluster.
27 - If O(esxi_hostname) is not given, this parameter is required.
28 type: str
29 esxi_hostname:
30 description:
31 - ESXi hostname.
32 - Service information about this ESXi server will be returned.
33 - If O(cluster_name) is not given, this parameter is required.
34 type: str
35 extends_documentation_fragment:
36 - vmware.vmware.base_options
37
38 '''
39
40 EXAMPLES = r'''
41 - name: Gather info about all ESXi Host in given Cluster
42 community.vmware.vmware_host_service_info:
43 hostname: '{{ vcenter_hostname }}'
44 username: '{{ vcenter_username }}'
45 password: '{{ vcenter_password }}'
46 cluster_name: cluster_name
47 delegate_to: localhost
48 register: cluster_host_services
49
50 - name: Gather info about ESXi Host
51 community.vmware.vmware_host_service_info:
52 hostname: '{{ vcenter_hostname }}'
53 username: '{{ vcenter_username }}'
54 password: '{{ vcenter_password }}'
55 esxi_hostname: '{{ esxi_hostname }}'
56 delegate_to: localhost
57 register: host_services
58 '''
59
60 RETURN = r'''
61 host_service_info:
62 description:
63 - dict with hostname as key and dict with host service config information
64 returned: always
65 type: dict
66 sample: {
67 "10.76.33.226": [
68 {
69 "key": "DCUI",
70 "label": "Direct Console UI",
71 "policy": "on",
72 "required": false,
73 "running": true,
74 "uninstallable": false,
75 "source_package_name": "esx-base",
76 "source_package_desc": "This VIB contains all of the base functionality of vSphere ESXi."
77 },
78 {
79 "key": "TSM",
80 "label": "ESXi Shell",
81 "policy": "off",
82 "required": false,
83 "running": false,
84 "uninstallable": false,
85 "source_package_name": "esx-base",
86 "source_package_desc": "This VIB contains all of the base functionality of vSphere ESXi."
87 },
88 ]
89 }
90 '''
91
92 from ansible.module_utils.basic import AnsibleModule
93 from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi
94 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
95
96
97 class VmwareServiceManager(PyVmomi):
98 def __init__(self, module):
99 super(VmwareServiceManager, self).__init__(module)
100 cluster_name = self.params.get('cluster_name', None)
101 esxi_host_name = self.params.get('esxi_hostname', None)
102 self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
103
104 def gather_host_info(self):
105 hosts_info = {}
106 for host in self.hosts:
107 host_service_info = []
108 host_service_system = host.configManager.serviceSystem
109 if host_service_system and host_service_system.serviceInfo:
110 services = host_service_system.serviceInfo.service
111 for service in services:
112 host_service_info.append(
113 dict(
114 key=service.key,
115 label=service.label,
116 required=service.required,
117 uninstallable=service.uninstallable,
118 running=service.running,
119 policy=service.policy,
120 source_package_name=service.sourcePackage.sourcePackageName if service.sourcePackage else None,
121 source_package_desc=service.sourcePackage.description if service.sourcePackage else None,
122 )
123 )
124 hosts_info[host.name] = host_service_info
125 return hosts_info
126
127
128 def main():
129 argument_spec = base_argument_spec()
130 argument_spec.update(
131 cluster_name=dict(type='str', required=False),
132 esxi_hostname=dict(type='str', required=False),
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_service_config = VmwareServiceManager(module)
144 module.exit_json(changed=False, host_service_info=vmware_host_service_config.gather_host_info())
145
146
147 if __name__ == "__main__":
148 main()