diff --git a/changelogs/fragments/8532-expand-opennuebula-inventory-data.yml b/changelogs/fragments/8532-expand-opennuebula-inventory-data.yml new file mode 100644 index 0000000000..a1b0ffe2c0 --- /dev/null +++ b/changelogs/fragments/8532-expand-opennuebula-inventory-data.yml @@ -0,0 +1,2 @@ +minor_changes: + - opennebula.py - add VM ``id`` and VM ``host`` to inventory host data (https://github.com/ansible-collections/community.general/pull/8532). diff --git a/plugins/inventory/opennebula.py b/plugins/inventory/opennebula.py index bf81758ef1..077d3da5a3 100644 --- a/plugins/inventory/opennebula.py +++ b/plugins/inventory/opennebula.py @@ -199,6 +199,9 @@ class InventoryModule(BaseInventoryPlugin, Constructable): continue server['name'] = vm.NAME + server['id'] = vm.ID + if hasattr(vm.HISTORY_RECORDS, 'HISTORY') and vm.HISTORY_RECORDS.HISTORY: + 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..52ea934043 100644 --- a/tests/unit/plugins/inventory/test_opennebula.py +++ b/tests/unit/plugins/inventory/test_opennebula.py @@ -21,6 +21,23 @@ from ansible_collections.community.general.plugins.inventory.opennebula import I from ansible_collections.community.general.tests.unit.compat.mock import create_autospec +class HistoryEntry(object): + def __init__(self): + self.SEQ = '384' + self.HOSTNAME = 'sam-691-sam' + self.HID = '10' + self.CID = '0' + self.DS_ID = '100' + self.VM_MAD = 'kvm' + self.TM_MAD = '3par' + self.ACTION = '0' + + +class HistoryRecords(object): + def __init__(self): + self.HISTORY = [HistoryEntry()] + + @pytest.fixture def inventory(): r = InventoryModule() @@ -58,7 +75,7 @@ def get_vm_pool(): 'ETIME': 0, 'GID': 132, 'GNAME': 'CSApparelVDC', - 'HISTORY_RECORDS': {}, + 'HISTORY_RECORDS': HistoryRecords(), 'ID': 7157, 'LAST_POLL': 1632762935, 'LCM_STATE': 3, @@ -104,7 +121,7 @@ def get_vm_pool(): 'ETIME': 0, 'GID': 0, 'GNAME': 'oneadmin', - 'HISTORY_RECORDS': {}, + 'HISTORY_RECORDS': [], 'ID': 327, 'LAST_POLL': 1632763543, 'LCM_STATE': 3, @@ -167,7 +184,7 @@ def get_vm_pool(): 'ETIME': 0, 'GID': 0, 'GNAME': 'oneadmin', - 'HISTORY_RECORDS': {}, + 'HISTORY_RECORDS': [], 'ID': 107, 'LAST_POLL': 1632764186, 'LCM_STATE': 3,