From de0f9b9ebd2e2adc3e63ed304943fe7a2bcb473f Mon Sep 17 00:00:00 2001 From: Riordan Toms Date: Fri, 21 Feb 2025 09:58:20 +1000 Subject: [PATCH] xen_orchestra inventory plugin allow using vm and host names instead of UUID inventory --- ...769-xoa_allow_using_names_in_inventory.yml | 2 + plugins/inventory/xen_orchestra.py | 42 ++++++++++++++----- 2 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 changelogs/fragments/9769-xoa_allow_using_names_in_inventory.yml diff --git a/changelogs/fragments/9769-xoa_allow_using_names_in_inventory.yml b/changelogs/fragments/9769-xoa_allow_using_names_in_inventory.yml new file mode 100644 index 00000000000..3678dd5df22 --- /dev/null +++ b/changelogs/fragments/9769-xoa_allow_using_names_in_inventory.yml @@ -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). diff --git a/plugins/inventory/xen_orchestra.py b/plugins/inventory/xen_orchestra.py index 7ec8816757e..6ed6f62b1fe 100644 --- a/plugins/inventory/xen_orchestra.py +++ b/plugins/inventory/xen_orchestra.py @@ -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 ''' @@ -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 ''' @@ -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' @@ -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']) @@ -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()