diff --git a/plugins/inventory/opennebula.py b/plugins/inventory/opennebula.py index 75b40e10bc..11b5bfc297 100644 --- a/plugins/inventory/opennebula.py +++ b/plugins/inventory/opennebula.py @@ -200,7 +200,8 @@ class InventoryModule(BaseInventoryPlugin, Constructable): server['name'] = vm.NAME server['id'] = vm.ID - server['host'] = vm.HISTORY_RECORDS.HISTORY[-1].HOSTNAME + if vm.HISTORY_RECORDS.HISTORY[-1]: + server['host'] = vm.HISTORY_RECORDS.HISTORY[-1].HOSTNAME server['LABELS'] = labels server['v4_first_ip'] = self._get_vm_ipv4(vm) server['v6_first_ip'] = self._get_vm_ipv6(vm) diff --git a/tests/unit/plugins/inventory/test_opennebula.py b/tests/unit/plugins/inventory/test_opennebula.py index bbc2fe699a..e62724a9e6 100644 --- a/tests/unit/plugins/inventory/test_opennebula.py +++ b/tests/unit/plugins/inventory/test_opennebula.py @@ -58,7 +58,19 @@ def get_vm_pool(): 'ETIME': 0, 'GID': 132, 'GNAME': 'CSApparelVDC', - 'HISTORY_RECORDS': {}, + 'HISTORY_RECORDS': OrderedDict({ + 'HISTORY': OrderedDict({ + 'OID': '42', + 'SEQ': '384', + 'HOSTNAME': 'sam-691-sam', + 'HID': '10', + 'CID': '0', + 'DS_ID': '100', + 'VM_MAD': 'kvm', + 'TM_MAD': '3par', + 'ACTION': '0' + }) + }), 'ID': 7157, 'LAST_POLL': 1632762935, 'LCM_STATE': 3,