CI: adapt packet-ci role to act as a molecule provisioner

To work with molecule, we need to use the name provided by molecule_yml
in inventory.

Inject the name in the VirtualMachineInstance (with a default to handle
non-molecule scenario) and get it back as part of inventory).

Account for no ansible groups
This commit is contained in:
Max Gautier
2025-03-10 16:52:25 +01:00
parent d0f91adde4
commit e62bbe0c76
4 changed files with 17 additions and 9 deletions

View File

@@ -1,11 +1,14 @@
---
- name: Include custom vars for ci job
include_vars: "../files/{{ ci_job_name }}.yml"
when: molecule_yml is not defined
- name: Start vms for CI job
kubernetes.core.k8s:
definition: "{{ lookup('template', 'vm.yml.j2', template_vars=item) }}"
loop: "{{ cluster_layout }}"
loop_control:
index_var: index
- name: Wait for vms to have IP addresses
kubernetes.core.k8s_info:
@@ -24,17 +27,17 @@
- name: Massage VirtualMachineInstance data into an Ansible inventory structure
vars:
ips: "{{ vmis.resources | map(attribute='status.interfaces.0.ipAddress') }}"
names: "{{ vmis.resources | map(attribute='metadata.name') }}"
_groups: "{{ vmis.resources | map(attribute='metadata.annotations.ansible_groups') | map('split', ',') }}"
names: "{{ vmis.resources | map(attribute='metadata.annotations.inventory_name') }}"
_groups: "{{ (vmis.resources | map(attribute='metadata.annotations.ansible_groups') | map('split', ','))}}"
hosts: "{{ ips | zip(_groups, names)
| map('zip', ['ansible_host', 'ansible_groups', 'k8s_vmi_name'])
| map('zip', ['ansible_host', 'ansible_groups', 'inventory_name'])
| map('map', 'reverse') | map('community.general.dict') }}"
loop: "{{ hosts | map(attribute='ansible_groups') | flatten | unique }}"
set_fact:
ci_inventory: "{{ ci_inventory|d({}) | combine({
item: {
'hosts': hosts | selectattr('ansible_groups', 'contains', item)
| rekey_on_member('k8s_vmi_name')
| rekey_on_member('inventory_name')
}
})
}}"