Debian Codegraph @

ansible_collections/community/vmware/plugins/modules/vmware_host_ssl_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_ssl_info
15 short_description: Gather info of ESXi host system about SSL
16 description:
17 - This module can be used to gather information of the SSL thumbprint information for a host.
18 author:
19 - Abhijeet Kasurde (@Akasurde)
20 options:
21 cluster_name:
22 description:
23 - Name of the cluster.
24 - SSL thumbprint information about all ESXi host system in the given cluster will be reported.
25 - If O(esxi_hostname) is not given, this parameter is required.
26 type: str
27 esxi_hostname:
28 description:
29 - ESXi hostname.
30 - SSL thumbprint information of this ESXi host system will be reported.
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 SSL thumbprint information about all ESXi Hosts in given Cluster
40 community.vmware.vmware_host_ssl_info:
41 hostname: '{{ vcenter_hostname }}'
42 username: '{{ vcenter_username }}'
43 password: '{{ vcenter_password }}'
44 cluster_name: '{{ cluster_name }}'
45 delegate_to: localhost
46 register: all_host_ssl_info
47
48 - name: Get SSL Thumbprint info about "{{ esxi_hostname }}"
49 community.vmware.vmware_host_ssl_info:
50 hostname: "{{ vcenter_hostname }}"
51 username: "{{ vcenter_username }}"
52 password: "{{ vcenter_password }}"
53 esxi_hostname: '{{ esxi_hostname }}'
54 register: ssl_info
55 - set_fact:
56 ssl_thumbprint: "{{ ssl_info['host_ssl_info'][esxi_hostname]['ssl_thumbprints'][0] }}"
57 - debug:
58 msg: "{{ ssl_thumbprint }}"
59 - name: Add ESXi Host to vCenter
60 vmware_host:
61 hostname: '{{ vcenter_hostname }}'
62 username: '{{ vcenter_username }}'
63 password: '{{ vcenter_password }}'
64 datacenter_name: '{{ datacenter_name }}'
65 cluster_name: '{{ cluster_name }}'
66 esxi_hostname: '{{ esxi_hostname }}'
67 esxi_username: '{{ esxi_username }}'
68 esxi_password: '{{ esxi_password }}'
69 esxi_ssl_thumbprint: '{{ ssl_thumbprint }}'
70 state: present
71 '''
72
73 RETURN = r'''
74 host_ssl_info:
75 description:
76 - dict with hostname as key and dict with SSL thumbprint related info
77 returned: info
78 type: dict
79 sample:
80 {
81 "10.76.33.215": {
82 "owner_tag": "",
83 "principal": "vpxuser",
84 "ssl_thumbprints": [
85 "E3:E8:A9:20:8D:32:AE:59:C6:8D:A5:91:B0:20:EF:00:A2:7C:27:EE",
86 "F1:AC:DA:6E:D8:1E:37:36:4A:5C:07:E5:04:0B:87:C8:75:FB:42:01"
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 VMwareHostSslManager(PyVmomi):
98 def __init__(self, module):
99 super(VMwareHostSslManager, 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 self.hosts_info = {}
104
105 def gather_ssl_info(self):
106 for host in self.hosts:
107 self.hosts_info[host.name] = dict(
108 principal='',
109 owner_tag='',
110 ssl_thumbprints=[])
111
112 host_ssl_info_mgr = host.config.sslThumbprintInfo
113 if host_ssl_info_mgr:
114 self.hosts_info[host.name]['principal'] = host_ssl_info_mgr.principal
115 self.hosts_info[host.name]['owner_tag'] = host_ssl_info_mgr.ownerTag
116 self.hosts_info[host.name]['ssl_thumbprints'] = list(host_ssl_info_mgr.sslThumbprints)
117
118 self.module.exit_json(changed=False, host_ssl_info=self.hosts_info)
119
120
121 def main():
122 argument_spec = base_argument_spec()
123 argument_spec.update(
124 cluster_name=dict(type='str'),
125 esxi_hostname=dict(type='str'),
126 )
127
128 module = AnsibleModule(
129 argument_spec=argument_spec,
130 required_one_of=[
131 ['cluster_name', 'esxi_hostname'],
132 ],
133 supports_check_mode=True,
134 )
135
136 vmware_host_accept_config = VMwareHostSslManager(module)
137 vmware_host_accept_config.gather_ssl_info()
138
139
140 if __name__ == "__main__":
141 main()