diff --git a/changelogs/fragments/9787-xoa_allow_using_names_in_inventory.yml b/changelogs/fragments/9787-xoa_allow_using_names_in_inventory.yml new file mode 100644 index 00000000000..161e94f148a --- /dev/null +++ b/changelogs/fragments/9787-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/9787). diff --git a/plugins/inventory/xen_orchestra.py b/plugins/inventory/xen_orchestra.py index 5c21fb365a2..4a6d431a7df 100644 --- a/plugins/inventory/xen_orchestra.py +++ b/plugins/inventory/xen_orchestra.py @@ -57,6 +57,20 @@ description: Use wss when connecting to the Xen Orchestra API type: boolean default: true + use_vm_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 UUIDs. + type: boolean + default: true + version_added: 10.4.0 + use_host_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 UUIDs. + type: boolean + default: true + version_added: 10.4.0 ''' @@ -72,6 +86,8 @@ kube_nodes: "'kube_node' in tags" compose: ansible_port: 2222 +use_vm_uuid: false +use_host_uuid: true ''' @@ -196,10 +212,20 @@ 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 = [] for uuid, vm in vms.items(): + if self.vm_entry_name_type == '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' ip = vm.get('mainIpAddress') - entry_name = uuid power_state = vm['power_state'].lower() pool_name = self._pool_group_name_for_uuid(pools, vm['$poolId']) host_name = self._host_group_name_for_uuid(hosts, vm['$container']) @@ -246,8 +272,19 @@ 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 = [] for host in hosts.values(): - entry_name = host['uuid'] + if self.host_entry_name_type == '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']) @@ -337,5 +374,13 @@ def parse(self, inventory, loader, path, cache=True): if not self.get_option('use_ssl'): self.protocol = 'ws' + self.vm_entry_name_type = 'uuid' + 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_uuid'): + self.host_entry_name_type = 'name_label' + objects = self._get_objects() self._populate(make_unsafe(objects)) diff --git a/tests/unit/plugins/inventory/test_xen_orchestra.py b/tests/unit/plugins/inventory/test_xen_orchestra.py index d626fb988b0..f24c0727054 100644 --- a/tests/unit/plugins/inventory/test_xen_orchestra.py +++ b/tests/unit/plugins/inventory/test_xen_orchestra.py @@ -158,6 +158,8 @@ def test_verify_file_bad_config(inventory): def test_populate(inventory, mocker): + inventory.host_entry_name_type = 'uuid' + inventory.vm_entry_name_type = 'uuid' inventory.get_option = mocker.MagicMock(side_effect=get_option) inventory._populate(objects) actual = sorted(inventory.inventory.hosts.keys())