Debian Codegraph @

ansible_collections/community/vmware/plugins/modules/vmware_host_ntp_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_ntp_info
15 short_description: Gathers info about NTP configuration on an ESXi host
16 description:
17 - This module can be used to gather information about NTP configurations on an ESXi host.
18 author:
19 - Abhijeet Kasurde (@Akasurde)
20 options:
21 cluster_name:
22 description:
23 - Name of the cluster.
24 - NTP config information about each ESXi server will be returned for the 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 - NTP config 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 NTP info about all ESXi Host in the given Cluster
40 community.vmware.vmware_host_ntp_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_ntp
47
48 - name: Gather NTP info about ESXi Host
49 community.vmware.vmware_host_ntp_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_ntp
56 '''
57
58 RETURN = r'''
59 hosts_ntp_info:
60 description:
61 - dict with hostname as key and dict with NTP infos as value
62 returned: hosts_ntp_info
63 type: dict
64 sample: {
65 "10.76.33.226": [
66 {
67 "ntp_servers": [],
68 "time_zone_description": "UTC",
69 "time_zone_gmt_offset": 0,
70 "time_zone_identifier": "UTC",
71 "time_zone_name": "UTC"
72 }
73 ]
74 }
75 '''
76
77 from ansible.module_utils.basic import AnsibleModule
78 from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi
79 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
80
81
82 class VmwareNtpInfoManager(PyVmomi):
83 def __init__(self, module):
84 super(VmwareNtpInfoManager, self).__init__(module)
85 cluster_name = self.params.get('cluster_name', None)
86 esxi_host_name = self.params.get('esxi_hostname', None)
87 self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
88
89 def gather_ntp_info(self):
90 hosts_info = {}
91 for host in self.hosts:
92 host_ntp_info = []
93 host_date_time_manager = host.configManager.dateTimeSystem
94 if host_date_time_manager:
95 host_ntp_info.append(
96 dict(
97 time_zone_identifier=host_date_time_manager.dateTimeInfo.timeZone.key,
98 time_zone_name=host_date_time_manager.dateTimeInfo.timeZone.name,
99 time_zone_description=host_date_time_manager.dateTimeInfo.timeZone.description,
100 time_zone_gmt_offset=host_date_time_manager.dateTimeInfo.timeZone.gmtOffset,
101 ntp_servers=list(host_date_time_manager.dateTimeInfo.ntpConfig.server)
102 )
103 )
104 hosts_info[host.name] = host_ntp_info
105 return hosts_info
106
107
108 def main():
109 argument_spec = base_argument_spec()
110 argument_spec.update(
111 cluster_name=dict(type='str', required=False),
112 esxi_hostname=dict(type='str', required=False),
113 )
114
115 module = AnsibleModule(
116 argument_spec=argument_spec,
117 required_one_of=[
118 ['cluster_name', 'esxi_hostname'],
119 ],
120 supports_check_mode=True,
121 )
122
123 vmware_host_ntp_config = VmwareNtpInfoManager(module)
124 module.exit_json(changed=False, hosts_ntp_info=vmware_host_ntp_config.gather_ntp_info())
125
126
127 if __name__ == "__main__":
128 main()