From fd3281ca30d0a57ce2eeaa5d5751b05e880108a5 Mon Sep 17 00:00:00 2001 From: diademiemi Date: Mon, 12 Jun 2023 11:37:34 +0200 Subject: [PATCH] Make interfaces a list --- main.tf | 104 ++++++++++++++++++++++++++++++++------------------- variables.tf | 41 ++++++++++---------- 2 files changed, 88 insertions(+), 57 deletions(-) diff --git a/main.tf b/main.tf index 7631524..4d6abd5 100644 --- a/main.tf +++ b/main.tf @@ -14,38 +14,60 @@ resource "libvirt_volume" "disk" { data "template_file" "cloudinit" { template = <<-EOT - #cloud-config - - hostname: ${var.hostname} - fqdn: ${var.hostname}.${var.domain} - - ssh_pwauth: True - ssh_deletekeys: False - - %{if length(var.ssh_keys) > 0} - ssh_authorized_keys: - %{for key in var.ssh_keys} - - "${key}" - %{endfor} - %{endif} - - network: - ethernets: - eth0: - dhcp4: ${var.dhcp} - dhcp6: false - %{if var.dhcp == false~} - addresses: ["${var.ip}"] - gateway4: ${var.gateway} - nameservers: - %{if length(var.nameservers) > 0~} - %{for nameserver in var.nameservers~} - - ${nameserver} - %{endfor~} - %{endif~} - %{endif} - - EOT +#cloud-config + +hostname: ${var.hostname} +fqdn: ${var.hostname}.${var.domain} + +ssh_pwauth: True +ssh_deletekeys: False + +%{if length(var.ssh_keys) > 0} +ssh_authorized_keys: +%{for key in var.ssh_keys} + - "${key}" +%{endfor} +%{endif} + +network: + ethernets: +%{for interface in var.network_interfaces ~} + ${interface.name~}: +%{if interface.dhcp == null~} + dhcp4: true +%{endif~} +%{if interface.dhcp != null~} + dhcp4: ${interface.dhcp} +%{endif~} + dhcp6: false +%{if interface.dhcp != true~} +%{if interface.ip != null~} + addresses: ["${interface.ip}"] +%{endif~} +%{if interface.gateway != null~} + gateway4: ${interface.gateway} +%{endif~} +%{if interface.nameservers != null~} +%{if length(interface.nameservers) > 0~} + nameservers: +%{for nameserver in interface.nameservers~} + - ${nameserver} +%{endfor~} +%{endif~} +%{endif~} +%{if interface.additional_routes != null~} +%{if length(interface.additional_routes) > 0~} + routes: +%{for route in interface.additional_routes~} + - to: ${route.network} + via: ${route.gateway} +%{endfor~} +%{endif~} +%{endif~} +%{endif~} +%{endfor~} + +EOT } resource "libvirt_cloudinit_disk" "init_disk" { @@ -75,11 +97,17 @@ resource "libvirt_domain" "domain" { } } - network_interface { - macvtap = var.libvirt_external_interface - hostname = var.hostname - wait_for_lease = false - mac = var.mac // For some providers, this is required + dynamic "network_interface" { + for_each = var.network_interfaces + + content { + macvtap = network_interface.value.macvtap + network_name = network_interface.value.network_name + network_id = network_interface.value.network_id + hostname = network_interface.value.hostname + wait_for_lease = network_interface.value.wait_for_lease + mac = network_interface.value.mac // For some providers, this is required + } } console { @@ -100,7 +128,7 @@ resource "ansible_host" "default" { groups = concat(var.ansible_groups, [lower(replace(var.domain, ".", "_"))]) variables = { - ansible_host = coalesce(var.ansible_host, var.ip, var.domain != "" ? "${var.hostname}.${var.domain}" : var.hostname) + ansible_host = coalesce(var.ansible_host, var.network_interfaces[0].ip, var.domain != "" ? "${var.hostname}.${var.domain}" : var.hostname) ansible_user = var.ansible_user } } diff --git a/variables.tf b/variables.tf index 9cad00e..439a11a 100644 --- a/variables.tf +++ b/variables.tf @@ -25,27 +25,30 @@ variable "nameservers" { variable "libvirt_external_interface" { type = string - default = "eth0" -} - -variable "dhcp" { - type = bool - default = true -} - -variable "ip" { - type = string - default = "" -} - -variable "gateway" { - type = string - default = "" # 1.2.3.4/5 + default = null } -variable "mac" { - type = string - default = null +variable "network_interfaces" { + type = list(object({ + name = optional(string) + network_id = optional(string) + network_name = optional(string) + macvtap = optional(string) + hostname = optional(string) + wait_for_lease = optional(bool) + + dhcp = optional(bool) + ip = optional(string) + gateway = optional(string) + nameservers = optional(list(string)) + mac = optional(string) + + additional_routes = optional(list(object({ + network = string + gateway = string + }))) + })) + default = [] } variable "memory" {