Skip to content

Commit

Permalink
Make interfaces a list
Browse files Browse the repository at this point in the history
  • Loading branch information
x86-39 committed Jun 12, 2023
1 parent 8d7460b commit fd3281c
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 57 deletions.
104 changes: 66 additions & 38 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
}
41 changes: 22 additions & 19 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down

0 comments on commit fd3281c

Please sign in to comment.