From 9db66ebd710665d841197018cff276be8e545459 Mon Sep 17 00:00:00 2001 From: Davy Stoffel Date: Wed, 4 Aug 2021 11:13:52 +0200 Subject: [PATCH] CIDR support, removed ipv4_netmask if dhcp --- README.md | 4 ++-- main.tf | 10 ++++++++-- variables.tf | 4 ++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b2c7b41..e9402e5 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ module "example-server-linuxvm" { vmname = "example-server-linux" vmrp = "esxi/Resources - or name of a resource pool" network = { - "Name of the Port Group in vSphere" = ["10.13.113.2", "10.13.113.3"] # To use DHCP create Empty list ["",""] + "Name of the Port Group in vSphere" = ["10.13.113.2", "10.13.113.3"] # To use DHCP create Empty list ["",""]; You can also use a CIDR annotation; } vmgateway = "10.13.113.1" dc = "Datacenter" @@ -94,7 +94,7 @@ module "example-server-windowsvm-advanced" { vmnameformat = "%03d" #To use three decimal with leading zero vmnames will be AdvancedVM001,AdvancedVM002 domain = "somedomain.com" network = { - "Name of the Port Group in vSphere" = ["10.13.113.2", "10.13.113.3"] # To use DHCP create Empty list ["",""] + "Name of the Port Group in vSphere" = ["10.13.113.2", "10.13.113.3"] # To use DHCP create Empty list ["",""]; You can also use a CIDR annotation; "Second Network Card" = ["", ""] } ipv4submask = ["24", "8"] diff --git a/main.tf b/main.tf index 5730b24..6b3625c 100644 --- a/main.tf +++ b/main.tf @@ -205,8 +205,14 @@ resource "vsphere_virtual_machine" "vm" { dynamic "network_interface" { for_each = keys(var.network) content { - ipv4_address = var.network[keys(var.network)[network_interface.key]][count.index] - ipv4_netmask = "%{if length(var.ipv4submask) == 1}${var.ipv4submask[0]}%{else}${var.ipv4submask[network_interface.key]}%{endif}" + ipv4_address = split("/", var.network[keys(var.network)[network_interface.key]][count.index])[0] + ipv4_netmask = var.network[keys(var.network)[network_interface.key]][count.index] == "" ? null : ( + length(split("/", var.network[keys(var.network)[network_interface.key]][count.index])) == 2 ? ( + split("/", var.network[keys(var.network)[network_interface.key]][count.index])[1] + ) : ( + length(var.ipv4submask) == 1 ? var.ipv4submask[0] : var.ipv4submask[network_interface.key] + ) + ) } } dns_server_list = var.dns_server_list diff --git a/variables.tf b/variables.tf index 95a0de7..b1142b7 100644 --- a/variables.tf +++ b/variables.tf @@ -1,6 +1,6 @@ #Network Section variable "network" { - description = "Define PortGroup and IPs for each VM" + description = "Define PortGroup and IPs/CIDR for each VM. If no CIDR provided, the subnet mask is taken from var.ipv4submask." type = map(list(string)) default = {} } @@ -12,7 +12,7 @@ variable "network_type" { } variable "ipv4submask" { - description = "ipv4 Subnet mask." + description = "ipv4 Subnet mask. Warning: The order must follow the alphabetic order from var.network." type = list(any) default = ["24"] }