Skip to content

Commit

Permalink
Allow Xen Host and/or Xen VM names instead of their UUIDs (#9787)
Browse files Browse the repository at this point in the history
* Allow using Xen Host and/or Xen VM names instead of their UUIDs for inventory

* xen_orchestra inventory plugin allow using vm and host names instead of UUID inventory

* Update changelog fragment with correct PR number

* Set missing inventory attributes in unit test

* Add version_added suggestion as per github comments

* Description update.

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
  • Loading branch information
rt-vnx and felixfontein authored Feb 24, 2025
1 parent 8425464 commit e8e3e5c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 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/9787).
49 changes: 47 additions & 2 deletions plugins/inventory/xen_orchestra.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
'''


Expand All @@ -72,6 +86,8 @@
kube_nodes: "'kube_node' in tags"
compose:
ansible_port: 2222
use_vm_uuid: false
use_host_uuid: true
'''

Expand Down Expand Up @@ -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'])
Expand Down Expand Up @@ -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'])

Expand Down Expand Up @@ -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))
2 changes: 2 additions & 0 deletions tests/unit/plugins/inventory/test_xen_orchestra.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit e8e3e5c

Please sign in to comment.