mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Add VM id and VM host to opennebula inventory data
##### SUMMARY <!--- Describe the change below, including rationale and design decisions --> 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 <!--- HINT: Include "Fixes #nnn" if you are fixing an existing issue --> <!--- Please do not forget to include a changelog fragment: https://docs.ansible.com/ansible/devel/community/collection_development_process.html#creating-changelog-fragments No need to include one for docs-only or test-only PR, and for new plugin/module PRs. Read about more details in CONTRIBUTING.md. --> ##### ISSUE TYPE <!--- Pick one or more below and delete the rest. 'Test Pull Request' is for PRs that add/extend tests without code changes. --> - Feature Pull Request ##### COMPONENT NAME <!--- Write the SHORT NAME of the module, plugin, task or feature below. --> opennebula.py ##### ADDITIONAL INFORMATION <!--- Include additional information to help people understand the change here --> <!--- A step-by-step reproduction of the problem is helpful if there is no related issue --> <!--- Paste verbatim command output below, e.g. before and after your change --> ```paste below "host": "foo23.host", "id": 1234, ```
This commit is contained in:
parent
9a18963364
commit
422362471b
1 changed files with 38 additions and 0 deletions
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue