feat(network): make interfaces[] canonical, normalize flat fields as AWX compat

This commit is contained in:
2026-02-12 22:17:02 +01:00
parent 5108e46a4c
commit 66057bc9b2
17 changed files with 222 additions and 159 deletions

View File

@@ -9,8 +9,6 @@ virtualization_libvirt_disk_path: >-
{{ [virtualization_libvirt_image_dir, hostname ~ '.qcow2'] | ansible.builtin.path_join }}
virtualization_libvirt_cloudinit_path: >-
{{ [virtualization_libvirt_image_dir, hostname ~ '-cloudinit.iso'] | ansible.builtin.path_join }}
virtualization_mac_address: >-
{{ '52:54:00' | community.general.random_mac(seed=hostname) }}
virtualization_xen_disk_path: /var/lib/xen/images
virtualization_tpm2_enabled: >-

View File

@@ -8,6 +8,28 @@
{%- set _ = out.update({ 'scsi' ~ loop.index0: hypervisor_cfg.storage ~ ':' ~ (disk.size | int) }) -%}
{%- endfor -%}
{{ out }}
virtualization_proxmox_net: >-
{%- set out = {} -%}
{%- for iface in system_cfg.network.interfaces -%}
{%- set val = 'virtio,bridge=' ~ iface.bridge -%}
{%- if iface.vlan | default('') | string | length > 0 -%}
{%- set val = val ~ ',tag=' ~ iface.vlan -%}
{%- endif -%}
{%- set _ = out.update({ 'net' ~ loop.index0: val }) -%}
{%- endfor -%}
{{ out }}
virtualization_proxmox_ipconfig: >-
{%- set out = {} -%}
{%- for iface in system_cfg.network.interfaces -%}
{%- if iface.ip | default('') | string | length > 0 -%}
{%- set val = 'ip=' ~ iface.ip ~ '/' ~ iface.prefix
~ ((',gw=' ~ iface.gateway) if (iface.gateway | default('') | length > 0) else '') -%}
{%- else -%}
{%- set val = 'ip=dhcp' -%}
{%- endif -%}
{%- set _ = out.update({ 'ipconfig' ~ loop.index0: val }) -%}
{%- endfor -%}
{{ out }}
community.proxmox.proxmox_kvm:
api_host: "{{ hypervisor_cfg.url }}"
api_user: "{{ hypervisor_cfg.username }}"
@@ -46,20 +68,8 @@
ide0: "{{ boot_iso }},media=cdrom"
ide1: "{{ rhel_iso + ',media=cdrom' if rhel_iso is defined and rhel_iso | length > 0 else omit }}"
ide2: "{{ hypervisor_cfg.storage }}:cloudinit"
net:
net0: >-
virtio,bridge={{ system_cfg.network.bridge
}}{%- if system_cfg.network.vlan is defined
and system_cfg.network.vlan | string | length > 0
%},tag={{ system_cfg.network.vlan }}{% endif %}
ipconfig:
ipconfig0: >-
{{
'ip=' ~ system_cfg.network.ip ~ '/' ~ system_cfg.network.prefix
~ (',gw=' ~ system_cfg.network.gateway if system_cfg.network.gateway is defined and system_cfg.network.gateway | length else '')
if system_cfg.network.ip is defined and system_cfg.network.ip | string | length
else 'ip=dhcp'
}}
net: "{{ virtualization_proxmox_net }}"
ipconfig: "{{ virtualization_proxmox_ipconfig }}"
nameservers: "{{ system_cfg.network.dns.servers if system_cfg.network.dns.servers | length else omit }}"
searchdomains: "{{ system_cfg.network.dns.search if system_cfg.network.dns.search | length else omit }}"
onboot: true

View File

@@ -14,6 +14,17 @@
- name: Create VM in vCenter
delegate_to: localhost
vars:
virtualization_vmware_networks: >-
{%- set ns = namespace(out=[]) -%}
{%- for iface in system_cfg.network.interfaces -%}
{%- set entry = {'name': iface.bridge, 'type': 'dhcp'} -%}
{%- if (iface.vlan | default('') | string | length) > 0 -%}
{%- set entry = entry | combine({'vlan': iface.vlan | int}) -%}
{%- endif -%}
{%- set ns.out = ns.out + [entry] -%}
{%- endfor -%}
{{ ns.out }}
community.vmware.vmware_guest:
hostname: "{{ hypervisor_cfg.url }}"
username: "{{ hypervisor_cfg.username }}"
@@ -53,10 +64,7 @@
"iso_path": rhel_iso
} ] if rhel_iso is defined and rhel_iso | length > 0 else [] )
}}
networks:
- name: "{{ system_cfg.network.bridge }}"
type: dhcp
vlan: "{{ system_cfg.network.vlan if system_cfg.network.vlan is defined and system_cfg.network.vlan | string | length > 0 else omit }}"
networks: "{{ virtualization_vmware_networks }}"
register: virtualization_vmware_create_result
- name: Set VM created fact when VM was powered on during creation

View File

@@ -1,21 +1,23 @@
network:
version: 2
ethernets:
id0:
match:
macaddress: "{{ virtualization_mac_address }}"
{% set has_static = system_cfg.network.ip is defined and system_cfg.network.ip | string | length %}
{% set dns_list = system_cfg.network.dns.servers | default([]) %}
{% set search_list = system_cfg.network.dns.search | default([]) %}
{% for iface in system_cfg.network.interfaces %}
{% set iface_mac = '52:54:00' | community.general.random_mac(seed=hostname if loop.index0 == 0 else hostname ~ '-nic' ~ loop.index0) %}
{% set has_static = (iface.ip | default('') | string | length) > 0 %}
id{{ loop.index0 }}:
match:
macaddress: "{{ iface_mac }}"
{% if has_static %}
addresses:
- "{{ system_cfg.network.ip }}/{{ system_cfg.network.prefix }}"
{% if system_cfg.network.gateway is defined and system_cfg.network.gateway | string | length %}
gateway4: "{{ system_cfg.network.gateway }}"
- "{{ iface.ip }}/{{ iface.prefix }}"
{% if iface.gateway | default('') | string | length %}
gateway4: "{{ iface.gateway }}"
{% endif %}
{% else %}
dhcp4: true
{% if dns_list | length or search_list | length %}
{% if loop.index0 == 0 and (dns_list | length or search_list | length) %}
dhcp4-overrides:
{% if dns_list | length %}
use-dns: false
@@ -25,7 +27,7 @@ network:
{% endif %}
{% endif %}
{% endif %}
{% if dns_list or search_list %}
{% if loop.index0 == 0 and (dns_list or search_list) %}
nameservers:
{% if dns_list %}
addresses:
@@ -40,3 +42,4 @@ network:
{% endfor %}
{% endif %}
{% endif %}
{% endfor %}

View File

@@ -46,11 +46,13 @@
<target dev="sdc" bus="sata"/>
</disk>
{% endif %}
{% for iface in system_cfg.network.interfaces %}
<interface type='network'>
<mac address="{{ virtualization_mac_address }}"/>
<source network='{{ system_cfg.network.bridge if (system_cfg.network.bridge | default('' ) | string | length) > 0 else "default" }}'/>
<mac address="{{ '52:54:00' | community.general.random_mac(seed=hostname if loop.index0 == 0 else hostname ~ '-nic' ~ loop.index0) }}"/>
<source network='{{ iface.bridge | default("default") }}'/>
<model type='virtio'/>
</interface>
{% endfor %}
{% if virtualization_tpm2_enabled %}
<tpm model='tpm-crb'>
<backend type='emulator' version='2.0'/>