Skip to content

Commit

Permalink
CIDR support, removed ipv4_netmask if dhcp
Browse files Browse the repository at this point in the history
  • Loading branch information
dstoffel-de committed Aug 4, 2021
1 parent 942d563 commit 9db66eb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"]
Expand Down
10 changes: 8 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -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 = {}
}
Expand All @@ -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"]
}
Expand Down

0 comments on commit 9db66eb

Please sign in to comment.