Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Christian Kotte <[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_ipv6
15 short_description: Enables/Disables IPv6 support for an ESXi host system
16 description:
17 - This module can be used to enable or disable IPv6 support for ESXi host systems in given vCenter infrastructure.
18 - It also checks if the host needs to be restarted.
19 author:
20 - Christian Kotte (@ckotte) <[email protected]>
21 options:
22 state:
23 description:
24 - Enable or disable IPv6 support.
25 - You need to reboot the ESXi host if you change the configuration.
26 type: str
27 choices: [ enabled, disabled ]
28 default: 'enabled'
29 esxi_hostname:
30 description:
31 - Name of the host system to work with.
32 - This is required parameter if O(cluster_name) is not specified.
33 type: str
34 cluster_name:
35 description:
36 - Name of the cluster from which all host systems will be used.
37 - This is required parameter if O(esxi_hostname) is not specified.
38 type: str
39 extends_documentation_fragment:
40 - vmware.vmware.base_options
41
42 '''
43
44 EXAMPLES = r'''
45 - name: Enable IPv6 for an host system
46 community.vmware.vmware_host_ipv6:
47 hostname: '{{ vcenter_hostname }}'
48 username: '{{ vcenter_username }}'
49 password: '{{ vcenter_password }}'
50 esxi_hostname: '{{ esxi_hostname }}'
51 state: enabled
52 delegate_to: localhost
53
54 - name: Disable IPv6 for an host system
55 community.vmware.vmware_host_ipv6:
56 hostname: '{{ vcenter_hostname }}'
57 username: '{{ vcenter_username }}'
58 password: '{{ vcenter_password }}'
59 esxi_hostname: '{{ esxi_hostname }}'
60 state: disabled
61 delegate_to: localhost
62
63 - name: Disable IPv6 for all host systems from cluster
64 community.vmware.vmware_host_ipv6:
65 hostname: '{{ vcenter_hostname }}'
66 username: '{{ vcenter_username }}'
67 password: '{{ vcenter_password }}'
68 cluster_name: '{{ cluster_name }}'
69 state: disabled
70 delegate_to: localhost
71 '''
72
73 RETURN = r'''
74 result:
75 description: metadata about host system's IPv6 configuration
76 returned: always
77 type: dict
78 sample: {
79 "esxi01": {
80 "changed": false,
81 "msg": "IPv6 is already enabled and active for host 'esxi01'",
82 },
83 }
84 '''
85
86 try:
87 from pyVmomi import vim, vmodl
88 except ImportError:
89 pass
90
91 from ansible.module_utils.basic import AnsibleModule
92 from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi
93 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
94 from ansible.module_utils.common.text.converters import to_native
95
96
97 class VmwareHostIPv6(PyVmomi):
98 """Class to manage IPv6 for an ESXi host system"""
99
100 def __init__(self, module):
101 super(VmwareHostIPv6, self).__init__(module)
102 cluster_name = self.params.get('cluster_name')
103 esxi_host_name = self.params.get('esxi_hostname')
104 self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
105 if not self.hosts:
106 self.module.fail_json(msg="Failed to find host system with given configuration.")
107
108 def ensure(self):
109 """Manage IPv6 for an ESXi host system"""
110 results = dict(changed=False, result=dict())
111 desired_state = self.module.params['state']
112
113 host_change_list = []
114 for host in self.hosts:
115 changed = False
116 results['result'][host.name] = dict(msg='')
117
118 host_network_system = host.configManager.networkSystem
119 host_network_info = host_network_system.networkInfo
120
121 if desired_state == 'enabled':
122 # Don't do anything if IPv6 is already enabled
123 if host_network_info.atBootIpV6Enabled:
124 if host_network_info.ipV6Enabled:
125 results['result'][host.name]['msg'] = "IPv6 is already enabled and active for host '%s'" % \
126 host.name
127 if not host_network_info.ipV6Enabled:
128 results['result'][host.name]['msg'] = ("IPv6 is already enabled for host '%s', but a reboot"
129 " is required!" % host.name)
130 # Enable IPv6
131 else:
132 if not self.module.check_mode:
133 try:
134 config = vim.host.NetworkConfig()
135 config.ipV6Enabled = True
136 host_network_system.UpdateNetworkConfig(config, "modify")
137 changed = True
138 results['result'][host.name]['changed'] = True
139 results['result'][host.name]['msg'] = "IPv6 enabled for host '%s'" % host.name
140 except (vim.fault.AlreadyExists, vim.fault.NotFound):
141 self.module.fail_json(msg="Network entity specified in the configuration for host '%s'"
142 " already exists" % host.name)
143 except vmodl.fault.InvalidArgument as invalid_argument:
144 self.module.fail_json(msg="Invalid parameter specified for host '%s' : %s" %
145 (host.name, to_native(invalid_argument.msg)))
146 except vim.fault.HostConfigFault as config_fault:
147 self.module.fail_json(msg="Failed to enable IPv6 for host '%s' due to : %s" %
148 (host.name, to_native(config_fault.msg)))
149 except vmodl.fault.NotSupported as not_supported:
150 self.module.fail_json(msg="Failed to enable IPv6 for host '%s' due to : %s" %
151 (host.name, to_native(not_supported.msg)))
152 except (vmodl.RuntimeFault, vmodl.MethodFault) as runtime_fault:
153 self.module.fail_json(msg="Failed to enable IPv6 for host '%s' due to : %s" %
154 (host.name, to_native(runtime_fault.msg)))
155 else:
156 changed = True
157 results['result'][host.name]['changed'] = True
158 results['result'][host.name]['msg'] = "IPv6 will be enabled for host '%s'" % host.name
159 elif desired_state == 'disabled':
160 # Don't do anything if IPv6 is already disabled
161 if not host_network_info.atBootIpV6Enabled:
162 if not host_network_info.ipV6Enabled:
163 results['result'][host.name]['msg'] = "IPv6 is already disabled for host '%s'" % host.name
164 if host_network_info.ipV6Enabled:
165 changed = True
166 results['result'][host.name]['msg'] = ("IPv6 is already disabled for host '%s',"
167 " but a reboot is required!" % host.name)
168 # Disable IPv6
169 else:
170 if not self.module.check_mode:
171 try:
172 config = vim.host.NetworkConfig()
173 config.ipV6Enabled = False
174 host_network_system.UpdateNetworkConfig(config, "modify")
175 changed = True
176 results['result'][host.name]['changed'] = True
177 results['result'][host.name]['msg'] = "IPv6 disabled for host '%s'" % host.name
178 except (vim.fault.AlreadyExists, vim.fault.NotFound):
179 self.module.fail_json(msg="Network entity specified in the configuration for host '%s'"
180 " already exists" % host.name)
181 except vmodl.fault.InvalidArgument as invalid_argument:
182 self.module.fail_json(msg="Invalid parameter specified for host '%s' : %s" %
183 (host.name, to_native(invalid_argument.msg)))
184 except vim.fault.HostConfigFault as config_fault:
185 self.module.fail_json(msg="Failed to disable IPv6 for host '%s' due to : %s" %
186 (host.name, to_native(config_fault.msg)))
187 except vmodl.fault.NotSupported as not_supported:
188 self.module.fail_json(msg="Failed to disable IPv6 for host '%s' due to : %s" %
189 (host.name, to_native(not_supported.msg)))
190 except (vmodl.RuntimeFault, vmodl.MethodFault) as runtime_fault:
191 self.module.fail_json(msg="Failed to disable IPv6 for host '%s' due to : %s" %
192 (host.name, to_native(runtime_fault.msg)))
193 else:
194 changed = True
195 results['result'][host.name]['changed'] = True
196 results['result'][host.name]['msg'] = "IPv6 will be disabled for host '%s'" % host.name
197
198 host_change_list.append(changed)
199
200 if any(host_change_list):
201 results['changed'] = True
202 self.module.exit_json(**results)
203
204
205 def main():
206 """
207 Main
208 """
209 argument_spec = base_argument_spec()
210 argument_spec.update(
211 state=dict(default='enabled', choices=['enabled', 'disabled']),
212 esxi_hostname=dict(type='str', required=False),
213 cluster_name=dict(type='str', required=False),
214 )
215
216 module = AnsibleModule(argument_spec=argument_spec,
217 required_one_of=[
218 ['cluster_name', 'esxi_hostname'],
219 ],
220 supports_check_mode=True
221 )
222
223 ipv6 = VmwareHostIPv6(module)
224 ipv6.ensure()
225
226
227 if __name__ == '__main__':
228 main()