From 2434a201b9d11793db57a091ab062e6a97d89961 Mon Sep 17 00:00:00 2001 From: Sergii Golovatiuk Date: Fri, 5 Dec 2025 13:10:23 +0100 Subject: [PATCH] [sushy_emulator] Replace regex with join filter for VM instance list - Replace complex Jinja2 for-loop with idiomatic filter chain (dict2items | selectattr | map | list) - Remove regex_replace in favor of join(', ') in template - Keep variable as list for loop/index access in tasks - Template formats output as comma-separated: uuid1, uuid2, uuid3 Co-authored-by: Cursor Signed-off-by: Sergii Golovatiuk --- roles/sushy_emulator/tasks/collect_details.yml | 16 ++++++++-------- roles/sushy_emulator/templates/config_conf.j2 | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/roles/sushy_emulator/tasks/collect_details.yml b/roles/sushy_emulator/tasks/collect_details.yml index e54aa6ccb7..a6f043583d 100644 --- a/roles/sushy_emulator/tasks/collect_details.yml +++ b/roles/sushy_emulator/tasks/collect_details.yml @@ -97,15 +97,15 @@ - name: Generate list of filtered VMs vars: _matching_vms: >- - {% set matching_vms = [] -%} - {% for host, uuid in cifmw_libvirt_manager_uuids.items() -%} - {% if host | regex_search(cifmw_sushy_emulator_vm_prefix_filter | default('') + '.*') -%} - {% set _ = matching_vms.append(uuid) -%} - {% endif -%} - {% endfor -%} - {{ matching_vms }} + {{ + cifmw_libvirt_manager_uuids | + dict2items | + selectattr('key', 'match', cifmw_sushy_emulator_vm_prefix_filter | default('') ~ '.*') | + map(attribute='value') | + list + }} ansible.builtin.set_fact: - _cifmw_sushy_emulator_instances: "{{ _matching_vms | regex_replace('\n(?!.*\n)', ', ')}}" + _cifmw_sushy_emulator_instances: "{{ _matching_vms }}" when: - _matching_vms | length > 0 diff --git a/roles/sushy_emulator/templates/config_conf.j2 b/roles/sushy_emulator/templates/config_conf.j2 index b09cd02326..f3d368fe92 100644 --- a/roles/sushy_emulator/templates/config_conf.j2 +++ b/roles/sushy_emulator/templates/config_conf.j2 @@ -35,5 +35,5 @@ SUSHY_EMULATOR_IGNORE_BOOT_DEVICE = False # This list contains the identities of instances that the driver will filter by. # It is useful in a tenant environment where only some instances represent # virtual baremetal. -SUSHY_EMULATOR_ALLOWED_INSTANCES = {{ _cifmw_sushy_emulator_instances }} +SUSHY_EMULATOR_ALLOWED_INSTANCES = {{ _cifmw_sushy_emulator_instances | join(', ') }} {% endif %}