| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2015, Joseph Callen <jcallen () csc.com> |
| 5 |
# Copyright: (c) 2018, Ansible Project |
| 6 |
# Copyright: (c) 2018, Abhijeet Kasurde <[email protected]> |
| 7 |
# Copyright: (c) 2019, VMware Inc. |
| 8 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 9 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 10 |
|
| 11 |
from __future__ import absolute_import, division, print_function |
| 12 |
__metaclass__ = type |
| 13 |
|
| 14 |
|
| 15 |
DOCUMENTATION = r''' |
| 16 |
--- |
| 17 |
module: vmware_dvs_host |
| 18 |
short_description: Add or remove a host from distributed virtual switch |
| 19 |
description: |
| 20 |
- Manage a host system from distributed virtual switch. |
| 21 |
author: |
| 22 |
- Joseph Callen (@jcpowermac) |
| 23 |
- Abhijeet Kasurde (@Akasurde) |
| 24 |
- Joseph Andreatta (@vmwjoseph) |
| 25 |
options: |
| 26 |
esxi_hostname: |
| 27 |
description: |
| 28 |
- The ESXi hostname. |
| 29 |
required: true |
| 30 |
type: str |
| 31 |
switch_name: |
| 32 |
description: |
| 33 |
- The name of the Distributed vSwitch. |
| 34 |
required: true |
| 35 |
type: str |
| 36 |
vmnics: |
| 37 |
description: |
| 38 |
- The ESXi hosts vmnics to use with the Distributed vSwitch. |
| 39 |
- If unset, the current non-LAG uplinks will be kept. |
| 40 |
- To remove all non-LAG uplinks, use the empty list V([]). |
| 41 |
required: false |
| 42 |
type: list |
| 43 |
elements: str |
| 44 |
lag_uplinks: |
| 45 |
required: false |
| 46 |
type: list |
| 47 |
elements: dict |
| 48 |
description: |
| 49 |
- The ESXi hosts vmnics to use with specific LAGs. |
| 50 |
- If unset, the current LAG uplinks will be kept. |
| 51 |
- If set, LAG uplinks will be set to I(exactly) this list. So you always have to define the complete LAG uplink configuration; |
| 52 |
if you don't, you might loose LAG uplinks. |
| 53 |
- To remove all LAG uplinks, use the empty list V([]). |
| 54 |
suboptions: |
| 55 |
lag: |
| 56 |
description: |
| 57 |
- Name of the LAG. |
| 58 |
type: str |
| 59 |
required: true |
| 60 |
vmnics: |
| 61 |
description: |
| 62 |
- The ESXi hosts vmnics to use with the LAG. |
| 63 |
required: false |
| 64 |
type: list |
| 65 |
default: [] |
| 66 |
elements: str |
| 67 |
state: |
| 68 |
description: |
| 69 |
- If the host should be present or absent attached to the vSwitch. |
| 70 |
choices: [ present, absent ] |
| 71 |
default: 'present' |
| 72 |
type: str |
| 73 |
vendor_specific_config: |
| 74 |
description: |
| 75 |
- List of key, value dictionaries for the Vendor Specific Configuration. |
| 76 |
suboptions: |
| 77 |
key: |
| 78 |
description: |
| 79 |
- Key of setting. |
| 80 |
type: str |
| 81 |
required: true |
| 82 |
value: |
| 83 |
description: |
| 84 |
- Value of setting. |
| 85 |
type: str |
| 86 |
required: true |
| 87 |
required: false |
| 88 |
type: list |
| 89 |
elements: dict |
| 90 |
extends_documentation_fragment: |
| 91 |
- vmware.vmware.base_options |
| 92 |
|
| 93 |
''' |
| 94 |
|
| 95 |
EXAMPLES = r''' |
| 96 |
- name: Add Host to dVS |
| 97 |
community.vmware.vmware_dvs_host: |
| 98 |
hostname: '{{ vcenter_hostname }}' |
| 99 |
username: '{{ vcenter_username }}' |
| 100 |
password: '{{ vcenter_password }}' |
| 101 |
esxi_hostname: '{{ esxi_hostname }}' |
| 102 |
switch_name: dvSwitch |
| 103 |
vmnics: |
| 104 |
- vmnic0 |
| 105 |
- vmnic1 |
| 106 |
state: present |
| 107 |
delegate_to: localhost |
| 108 |
|
| 109 |
- name: Add vmnics to LAGs |
| 110 |
community.vmware.vmware_dvs_host: |
| 111 |
hostname: '{{ vcenter_hostname }}' |
| 112 |
username: '{{ vcenter_username }}' |
| 113 |
password: '{{ vcenter_password }}' |
| 114 |
esxi_hostname: '{{ esxi_hostname }}' |
| 115 |
switch_name: dvSwitch |
| 116 |
lag_uplinks: |
| 117 |
- lag: lag1 |
| 118 |
vmnics: |
| 119 |
- vmnic0 |
| 120 |
- vmnic1 |
| 121 |
- lag: lag2 |
| 122 |
vmnics: |
| 123 |
- vmnic2 |
| 124 |
- vmnic3 |
| 125 |
state: present |
| 126 |
delegate_to: localhost |
| 127 |
|
| 128 |
- name: Add Host to dVS/enable learnswitch (https://labs.vmware.com/flings/learnswitch) |
| 129 |
community.vmware.vmware_dvs_host: |
| 130 |
hostname: '{{ vcenter_hostname }}' |
| 131 |
username: '{{ vcenter_username }}' |
| 132 |
password: '{{ vcenter_password }}' |
| 133 |
esxi_hostname: '{{ esxi_hostname }}' |
| 134 |
switch_name: dvSwitch |
| 135 |
vendor_specific_config: |
| 136 |
- key: com.vmware.netoverlay.layer1 |
| 137 |
value: learnswitch |
| 138 |
vmnics: |
| 139 |
- vmnic0 |
| 140 |
- vmnic1 |
| 141 |
state: present |
| 142 |
delegate_to: localhost |
| 143 |
''' |
| 144 |
|
| 145 |
try: |
| 146 |
from pyVmomi import vim, vmodl |
| 147 |
except ImportError: |
| 148 |
pass |
| 149 |
|
| 150 |
from ansible.module_utils.basic import AnsibleModule |
| 151 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import ( |
| 152 |
PyVmomi, |
| 153 |
find_dvs_by_name, |
| 154 |
find_hostsystem_by_name, |
| 155 |
wait_for_task) |
| 156 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 157 |
from ansible.module_utils.common.text.converters import to_native |
| 158 |
|
| 159 |
|
| 160 |
class VMwareDvsHost(PyVmomi): |
| 161 |
def __init__(self, module): |
| 162 |
super(VMwareDvsHost, self).__init__(module) |
| 163 |
self.host = None |
| 164 |
self.dv_switch = None |
| 165 |
self.desired_state = {} |
| 166 |
|
| 167 |
self.state = self.module.params['state'] |
| 168 |
self.switch_name = self.module.params['switch_name'] |
| 169 |
self.esxi_hostname = self.module.params['esxi_hostname'] |
| 170 |
self.vmnics = self.module.params['vmnics'] |
| 171 |
self.lag_uplinks = self.module.params['lag_uplinks'] |
| 172 |
self.vendor_specific_config = self.module.params['vendor_specific_config'] |
| 173 |
|
| 174 |
self.dv_switch = find_dvs_by_name(self.content, self.switch_name) |
| 175 |
|
| 176 |
if self.dv_switch is None: |
| 177 |
self.module.fail_json(msg="A distributed virtual switch %s " |
| 178 |
"does not exist" % self.switch_name) |
| 179 |
|
| 180 |
self.uplink_portgroup = self.find_dvs_uplink_pg() |
| 181 |
|
| 182 |
self.lags = {} |
| 183 |
for lag in self.dv_switch.config.lacpGroupConfig: |
| 184 |
self.lags[lag.name] = lag |
| 185 |
|
| 186 |
if self.lag_uplinks is not None: |
| 187 |
for lag_uplink in self.lag_uplinks: |
| 188 |
if lag_uplink['lag'] not in self.lags: |
| 189 |
self.module.fail_json(msg="LAG %s not found" % lag_uplink['lag']) |
| 190 |
|
| 191 |
def process_state(self): |
| 192 |
dvs_host_states = { |
| 193 |
'absent': { |
| 194 |
'present': self.state_destroy_dvs_host, |
| 195 |
'absent': self.state_exit_unchanged, |
| 196 |
}, |
| 197 |
'present': { |
| 198 |
'update': self.state_update_dvs_host, |
| 199 |
'present': self.state_exit_unchanged, |
| 200 |
'absent': self.state_create_dvs_host, |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
try: |
| 205 |
dvs_host_states[self.state][self.check_dvs_host_state()]() |
| 206 |
except vmodl.RuntimeFault as runtime_fault: |
| 207 |
self.module.fail_json(msg=to_native(runtime_fault.msg)) |
| 208 |
except vmodl.MethodFault as method_fault: |
| 209 |
self.module.fail_json(msg=to_native(method_fault.msg)) |
| 210 |
except Exception as e: |
| 211 |
self.module.fail_json(msg=to_native(e)) |
| 212 |
|
| 213 |
def find_dvs_uplink_pg(self): |
| 214 |
# There should only always be a single uplink port group on |
| 215 |
# a distributed virtual switch |
| 216 |
dvs_uplink_pg = self.dv_switch.config.uplinkPortgroup[0] if len(self.dv_switch.config.uplinkPortgroup) else None |
| 217 |
return dvs_uplink_pg |
| 218 |
|
| 219 |
# operation should be edit, add and remove |
| 220 |
def modify_dvs_host(self, operation): |
| 221 |
changed, result = False, None |
| 222 |
spec = vim.DistributedVirtualSwitch.ConfigSpec() |
| 223 |
spec.configVersion = self.dv_switch.config.configVersion |
| 224 |
spec.host = [vim.dvs.HostMember.ConfigSpec()] |
| 225 |
spec.host[0].operation = operation |
| 226 |
spec.host[0].host = self.host |
| 227 |
if self.vendor_specific_config: |
| 228 |
config = list() |
| 229 |
for item in self.vendor_specific_config: |
| 230 |
config.append(vim.dvs.KeyedOpaqueBlob(key=item['key'], opaqueData=item['value'])) |
| 231 |
spec.host[0].vendorSpecificConfig = config |
| 232 |
|
| 233 |
if operation == "edit": |
| 234 |
spec.host[0].backing = vim.dvs.HostMember.PnicBacking() |
| 235 |
|
| 236 |
for nic, uplinkPortKey in self.desired_state.items(): |
| 237 |
pnicSpec = vim.dvs.HostMember.PnicSpec() |
| 238 |
pnicSpec.pnicDevice = nic |
| 239 |
pnicSpec.uplinkPortgroupKey = self.uplink_portgroup.key |
| 240 |
pnicSpec.uplinkPortKey = uplinkPortKey |
| 241 |
spec.host[0].backing.pnicSpec.append(pnicSpec) |
| 242 |
|
| 243 |
try: |
| 244 |
task = self.dv_switch.ReconfigureDvs_Task(spec) |
| 245 |
changed, result = wait_for_task(task) |
| 246 |
except vmodl.fault.NotSupported as not_supported: |
| 247 |
self.module.fail_json(msg="Failed to configure DVS host %s as it is not" |
| 248 |
" compatible with the VDS version." % self.esxi_hostname, |
| 249 |
details=to_native(not_supported.msg)) |
| 250 |
return changed, result |
| 251 |
|
| 252 |
def state_destroy_dvs_host(self): |
| 253 |
operation, changed, result = ("remove", True, None) |
| 254 |
|
| 255 |
if not self.module.check_mode: |
| 256 |
changed, result = self.modify_dvs_host(operation) |
| 257 |
self.module.exit_json(changed=changed, result=to_native(result)) |
| 258 |
|
| 259 |
def state_exit_unchanged(self): |
| 260 |
self.module.exit_json(changed=False) |
| 261 |
|
| 262 |
def state_update_dvs_host(self): |
| 263 |
operation, changed, result = ("edit", True, None) |
| 264 |
|
| 265 |
if not self.module.check_mode: |
| 266 |
changed, result = self.modify_dvs_host(operation) |
| 267 |
self.module.exit_json(changed=changed, result=to_native(result)) |
| 268 |
|
| 269 |
def state_create_dvs_host(self): |
| 270 |
operation, changed, result = ("add", True, None) |
| 271 |
|
| 272 |
if not self.module.check_mode: |
| 273 |
changed, result = self.modify_dvs_host(operation) |
| 274 |
if changed: |
| 275 |
self.set_desired_state() |
| 276 |
changed, result = self.modify_dvs_host("edit") |
| 277 |
else: |
| 278 |
self.module.exit_json(changed=changed, result=to_native(result)) |
| 279 |
|
| 280 |
self.module.exit_json(changed=changed, result=to_native(result)) |
| 281 |
|
| 282 |
def find_host_attached_dvs(self): |
| 283 |
for dvs_host_member in self.dv_switch.config.host: |
| 284 |
if dvs_host_member.config.host.name == self.esxi_hostname: |
| 285 |
return dvs_host_member.config.host |
| 286 |
|
| 287 |
return None |
| 288 |
|
| 289 |
def set_desired_state(self): |
| 290 |
lag_uplinks = [] |
| 291 |
switch_uplink_ports = {'non_lag': []} |
| 292 |
|
| 293 |
for dvs_host_member in self.dv_switch.config.host: |
| 294 |
if dvs_host_member.config.host.name == self.esxi_hostname: |
| 295 |
break |
| 296 |
|
| 297 |
portCriteria = vim.dvs.PortCriteria() |
| 298 |
portCriteria.host = [self.host] |
| 299 |
portCriteria.portgroupKey = self.uplink_portgroup.key |
| 300 |
portCriteria.uplinkPort = True |
| 301 |
ports = self.dv_switch.FetchDVPorts(portCriteria) |
| 302 |
|
| 303 |
for name, lag in self.lags.items(): |
| 304 |
switch_uplink_ports[name] = [] |
| 305 |
for uplinkName in lag.uplinkName: |
| 306 |
for port in ports: |
| 307 |
if port.config.name == uplinkName: |
| 308 |
switch_uplink_ports[name].append(port.key) |
| 309 |
lag_uplinks.append(port.key) |
| 310 |
|
| 311 |
for port in sorted(ports, key=lambda port: port.config.name): |
| 312 |
if port.key in self.uplink_portgroup.portKeys and port.key not in lag_uplinks: |
| 313 |
switch_uplink_ports['non_lag'].append(port.key) |
| 314 |
|
| 315 |
# If defined, use vmnics as non-LAG uplinks |
| 316 |
if self.vmnics is not None: |
| 317 |
count = 0 |
| 318 |
for vmnic in self.vmnics: |
| 319 |
self.desired_state[vmnic] = switch_uplink_ports['non_lag'][count] |
| 320 |
count += 1 |
| 321 |
# Otherwise keep current non-LAG uplinks |
| 322 |
else: |
| 323 |
for pnicSpec in dvs_host_member.config.backing.pnicSpec: |
| 324 |
if pnicSpec.uplinkPortKey not in lag_uplinks: |
| 325 |
self.desired_state[pnicSpec.pnicDevice] = pnicSpec.uplinkPortKey |
| 326 |
|
| 327 |
# If defined, use lag_uplinks as LAG uplinks |
| 328 |
if self.lag_uplinks is not None: |
| 329 |
for lag in self.lag_uplinks: |
| 330 |
count = 0 |
| 331 |
for vmnic in lag['vmnics']: |
| 332 |
self.desired_state[vmnic] = switch_uplink_ports[lag['lag']][count] |
| 333 |
count += 1 |
| 334 |
# Otherwise keep current LAG uplinks |
| 335 |
else: |
| 336 |
for pnicSpec in dvs_host_member.config.backing.pnicSpec: |
| 337 |
if pnicSpec.uplinkPortKey in lag_uplinks: |
| 338 |
self.desired_state[pnicSpec.pnicDevice] = pnicSpec.uplinkPortKey |
| 339 |
|
| 340 |
def check_uplinks(self): |
| 341 |
pnic_device = [] |
| 342 |
|
| 343 |
self.set_desired_state() |
| 344 |
|
| 345 |
for dvs_host_member in self.dv_switch.config.host: |
| 346 |
if dvs_host_member.config.host.name == self.esxi_hostname: |
| 347 |
break |
| 348 |
|
| 349 |
for pnicSpec in dvs_host_member.config.backing.pnicSpec: |
| 350 |
pnic_device.append(pnicSpec.pnicDevice) |
| 351 |
if pnicSpec.pnicDevice not in self.desired_state: |
| 352 |
return False |
| 353 |
if pnicSpec.uplinkPortKey != self.desired_state[pnicSpec.pnicDevice]: |
| 354 |
return False |
| 355 |
|
| 356 |
for vmnic in self.desired_state: |
| 357 |
if vmnic not in pnic_device: |
| 358 |
return False |
| 359 |
|
| 360 |
return True |
| 361 |
|
| 362 |
def check_dvs_host_state(self): |
| 363 |
if self.uplink_portgroup is None: |
| 364 |
self.module.fail_json(msg="An uplink portgroup does not exist on" |
| 365 |
" the distributed virtual switch %s" % self.switch_name) |
| 366 |
|
| 367 |
self.host = self.find_host_attached_dvs() |
| 368 |
|
| 369 |
if self.host is None: |
| 370 |
# We still need the HostSystem object to add the host |
| 371 |
# to the distributed vswitch |
| 372 |
self.host = find_hostsystem_by_name(self.content, self.esxi_hostname) |
| 373 |
if self.host is None: |
| 374 |
self.module.fail_json(msg="The esxi_hostname %s does not exist " |
| 375 |
"in vCenter" % self.esxi_hostname) |
| 376 |
return 'absent' |
| 377 |
# Skip checking uplinks if the host should be absent, anyway |
| 378 |
elif self.state == 'absent': |
| 379 |
return 'present' |
| 380 |
else: |
| 381 |
if self.check_uplinks(): |
| 382 |
return 'present' |
| 383 |
else: |
| 384 |
return 'update' |
| 385 |
|
| 386 |
|
| 387 |
def main(): |
| 388 |
argument_spec = base_argument_spec() |
| 389 |
argument_spec.update( |
| 390 |
dict( |
| 391 |
esxi_hostname=dict(required=True, type='str'), |
| 392 |
switch_name=dict(required=True, type='str'), |
| 393 |
vmnics=dict(required=False, type='list', elements='str'), |
| 394 |
state=dict(default='present', choices=['present', 'absent'], type='str'), |
| 395 |
vendor_specific_config=dict( |
| 396 |
type='list', |
| 397 |
elements='dict', |
| 398 |
required=False, |
| 399 |
options=dict( |
| 400 |
key=dict(type='str', required=True, no_log=False), |
| 401 |
value=dict(type='str', required=True), |
| 402 |
), |
| 403 |
), |
| 404 |
lag_uplinks=dict( |
| 405 |
type='list', |
| 406 |
required=False, |
| 407 |
elements='dict', |
| 408 |
options=dict( |
| 409 |
lag=dict( |
| 410 |
type='str', |
| 411 |
required=True, |
| 412 |
), |
| 413 |
vmnics=dict( |
| 414 |
type='list', |
| 415 |
required=False, |
| 416 |
elements='str', |
| 417 |
default=[], |
| 418 |
), |
| 419 |
), |
| 420 |
), |
| 421 |
) |
| 422 |
) |
| 423 |
|
| 424 |
module = AnsibleModule(argument_spec=argument_spec, |
| 425 |
supports_check_mode=True) |
| 426 |
|
| 427 |
vmware_dvs_host = VMwareDvsHost(module) |
| 428 |
vmware_dvs_host.process_state() |
| 429 |
|
| 430 |
|
| 431 |
if __name__ == '__main__': |
| 432 |
main() |