From 422362471bab98f06ce5830f2f58373216e013c7 Mon Sep 17 00:00:00 2001 From: Tom Paine Date: Mon, 17 Jun 2024 17:46:01 +0100 Subject: [PATCH] Add VM id and VM host to opennebula inventory data ##### SUMMARY To enable greater use of the inventory, add the ID of the VM, and the hostname of the host the VM is running on to the inventory output ##### ISSUE TYPE - Feature Pull Request ##### COMPONENT NAME opennebula.py ##### ADDITIONAL INFORMATION ```paste below "host": "foo23.host", "id": 1234, ``` --- plugins/inventory/opennebula.py | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/plugins/inventory/opennebula.py b/plugins/inventory/opennebula.py index bf81758ef1..a803e9cf52 100644 --- a/plugins/inventory/opennebula.py +++ b/plugins/inventory/opennebula.py @@ -176,8 +176,44 @@ class InventoryModule(BaseInventoryPlugin, Constructable): return vm_pool + def _get_host_pool_info(self): + auth = self._get_connection_info() + + if not (auth.username and auth.password): + raise AnsibleError('API Credentials missing. Check OpenNebula inventory file.') + else: + one_client = pyone.OneServer(auth.url, session=auth.username + ':' + auth.password) + + # get hosts (underlying hardware hosts) + try: + host_pool_info = one_client.hostpool.info() + except Exception as e: + raise AnsibleError("Something happened during XML-RPC call: {e}".format(e=to_native(e))) + + return host_pool_info + + def _get_vm_to_host_map(self, host_pool_info): + vm_to_host_map = {} + + # Iterate over the hosts + for host in host_pool_info.HOST: + host_name = host.NAME + + # Some hosts might not have VMs, so we need to check for VMS attribute + if hasattr(host, 'VMS') and hasattr(host.VMS, 'ID'): + # VMS.ID could be a single ID or a list of IDs + if isinstance(host.VMS.ID, list): + for vm_id in host.VMS.ID: + vm_to_host_map[vm_id] = host_name + else: + vm_to_host_map[host.VMS.ID] = host_name + + return vm_to_host_map + def _retrieve_servers(self, label_filter=None): vm_pool = self._get_vm_pool() + host_pool_info = self._get_host_pool_info() + vm_to_host_map = self._get_vm_to_host_map(host_pool_info) result = [] @@ -199,6 +235,8 @@ class InventoryModule(BaseInventoryPlugin, Constructable): continue server['name'] = vm.NAME + server['id'] = vm.ID + server['host'] = vm_to_host_map.get(vm.ID, "VM ID not found") server['LABELS'] = labels server['v4_first_ip'] = self._get_vm_ipv4(vm) server['v6_first_ip'] = self._get_vm_ipv6(vm)