Skip to content

Commit

Permalink
xen_orchestra inventory plugin allow using vm and host names instead …
Browse files Browse the repository at this point in the history
…of UUID inventory
  • Loading branch information
rt-vnx committed Feb 20, 2025
1 parent 452693d commit de0f9b9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- xen_orchestra inventory plugin - add ``use_vm_uuid`` and ``use_host_uuid`` boolean options to allow switching over to using VM/Xen name labels instead of UUIDs as item names (https://github.com/ansible-collections/community.general/pull/9769).
42 changes: 32 additions & 10 deletions plugins/inventory/xen_orchestra.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@
type: boolean
default: true
use_vm_uuid:
description: Import Xen VMs to inventory using their UUID as the VM entry name (If set to false use VM name labels instead of UUID)
description:
- Import Xen VMs to inventory using their UUID as the VM entry name.
- If set to V(false) use VM name labels instead of UUID's.
type: boolean
default: true
use_host_uuid:
description: Import Xen Hosts to inventory using their UUID as the Host entry name (If set to false use Host name labels instead of UUID)
description:
- Import Xen Hosts to inventory using their UUID as the Host entry name.
- If set to V(false) use Host name labels instead of UUID's.
type: boolean
default: true
'''
Expand All @@ -80,8 +84,8 @@
kube_nodes: "'kube_node' in tags"
compose:
ansible_port: 2222
use_vm_uuid: False
use_host_uuid: True
use_vm_uuid: false
use_host_uuid: true
'''

Expand Down Expand Up @@ -206,9 +210,18 @@ def _apply_constructable(self, name, variables):
self._set_composite_vars(self.get_option('compose'), variables, name, strict=strict)

def _add_vms(self, vms, hosts, pools):
vm_name_list = []
if not hasattr(self, 'vm_entry_name_type'):
self.vm_entry_name_type = 'uuid'
for uuid, vm in vms.items():
if self.vm_entry_name_type == 'name_label':
entry_name = vm['name_label']
if vm['name_label'] not in vm_name_list:
entry_name = vm['name_label']
vm_name_list.append(vm['name_label'])
else:
vm_duplicate_count = vm_name_list.count(vm['name_label'])
entry_name = vm['name_label'] + "_" + str(vm_duplicate_count)
vm_name_list.append(vm['name_label'])
else:
entry_name = uuid
group = 'with_ip'
Expand Down Expand Up @@ -259,12 +272,21 @@ def _add_vms(self, vms, hosts, pools):
self._apply_constructable(entry_name, self.inventory.get_host(entry_name).get_vars())

def _add_hosts(self, hosts, pools):
host_name_list = []
if not hasattr(self, 'host_entry_name_type'):
self.host_entry_name_type = 'uuid'
for host in hosts.values():
entry_name = host['uuid']
if self.host_entry_name_type == 'name_label':
entry_name = host['name_label']
if host['name_label'] not in host_name_list:
entry_name = host['name_label']
host_name_list.append(host['name_label'])
else:
host_duplicate_count = host_name_list.count(host['name_label'])
entry_name = host['name_label'] + "_" + str(host_duplicate_count)
host_name_list.append(host['name_label'])
else:
entry_name = host['uuid']

group_name = f"xo_host_{clean_group_name(host['name_label'])}"
pool_name = self._pool_group_name_for_uuid(pools, host['$poolId'])

Expand Down Expand Up @@ -353,13 +375,13 @@ def parse(self, inventory, loader, path, cache=True):
self.validate_certs = self.get_option('validate_certs')
if not self.get_option('use_ssl'):
self.protocol = 'ws'

self.vm_entry_name_type = 'uuid'
if not self.get_option('use_vm_uuids'):
if not self.get_option('use_vm_uuid'):
self.vm_entry_name_type = 'name_label'

self.host_entry_name_type = 'uuid'
if not self.get_option('use_host_uuids'):
if not self.get_option('use_host_uuid'):
self.host_entry_name_type = 'name_label'

objects = self._get_objects()
Expand Down

0 comments on commit de0f9b9

Please sign in to comment.