Skip to content

Commit

Permalink
vmware_host_vmnic_info: Fix issue with offline / disconnected / shut …
Browse files Browse the repository at this point in the history
…down hosts (#1337)

vmware_host_vmnic_info: Fix issue with offline / disconnected / shut down hosts

SUMMARY
vmware_host_vmnic_info breaks if a host is offline / disconnected / shut down.
ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME
vmware_host_vmnic_info
ADDITIONAL INFORMATION
this comment
  • Loading branch information
mariolenz authored Jun 2, 2022
1 parent baef09d commit a39d1af
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- vmware_host_vmnic_info - Fix a bug that crashes the module when a host is disconnected (https://github.com/ansible-collections/community.vmware/pull/1337).
17 changes: 10 additions & 7 deletions plugins/modules/vmware_host_vmnic_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
'''

try:
from pyVmomi import vim
from pyVmomi import vim, vmodl
except ImportError:
pass

Expand Down Expand Up @@ -231,12 +231,15 @@ def gather_host_vmnic_info(self):
pnic_info['status'] = 'Connected'
pnic_info['actual_speed'] = pnic.linkSpeed.speedMb
pnic_info['actual_duplex'] = 'Full Duplex' if pnic.linkSpeed.duplex else 'Half Duplex'
network_hint = host_nw_system.QueryNetworkHint(pnic.device)
for hint in self.to_json(network_hint):
if hint.get('lldpInfo'):
pnic_info['lldp_info'] = {x['key']: x['value'] for x in hint['lldpInfo'].get('parameter')}
else:
pnic_info['lldp_info'] = 'N/A'
try:
network_hint = host_nw_system.QueryNetworkHint(pnic.device)
for hint in self.to_json(network_hint):
if hint.get('lldpInfo'):
pnic_info['lldp_info'] = {x['key']: x['value'] for x in hint['lldpInfo'].get('parameter')}
else:
pnic_info['lldp_info'] = 'N/A'
except (vmodl.fault.HostNotConnected, vmodl.fault.HostNotReachable):
pnic_info['lldp_info'] = 'N/A'
else:
pnic_info['status'] = 'Disconnected'
pnic_info['actual_speed'] = 'N/A'
Expand Down
57 changes: 57 additions & 0 deletions tests/integration/targets/vmware_host_vmnic_info/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,60 @@
- assert:
that:
- host_vmnics_extended.hosts_vmnics_info is defined

- name: Get info from an ESXi host that is offline / disconnected / shut down
block:
- name: Disconnect ESXi host
community.vmware.vmware_host:
validate_certs: false
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter_name: '{{ dc1 }}'
cluster_name: '{{ ccr1 }}'
esxi_hostname: "{{ esxi1 }}"
state: disconnected

- name: Give the ESXi host time to disconnect
ansible.builtin.pause:
minutes: 1

- name: Gather vmnic info about a host that is offline / disconnected / shut down
vmware_host_vmnic_info:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
esxi_hostname: '{{ esxi1 }}'
validate_certs: false
register: host_vmnics_disconnected
- debug: var=host_vmnics_disconnected
- assert:
that:
- host_vmnics_disconnected.hosts_vmnics_info is defined

- name: Gather extended vmnic info about a host that is offline / disconnected / shut down
vmware_host_vmnic_info:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
esxi_hostname: '{{ esxi1 }}'
validate_certs: false
capabilities: true
directpath_io: true
sriov: true
register: host_vmnics_extended_disconnected
- debug: var=host_vmnics_extended_disconnected
- assert:
that:
- host_vmnics_extended_disconnected.hosts_vmnics_info is defined
always:
- name: Reconnect ESXi host
community.vmware.vmware_host:
validate_certs: false
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter_name: '{{ dc1 }}'
cluster_name: '{{ ccr1 }}'
esxi_hostname: "{{ esxi1 }}"
state: reconnect

0 comments on commit a39d1af

Please sign in to comment.