Skip to content

Commit

Permalink
Merge pull request #103 from dstoffel/feature/cidr-fixnetmask
Browse files Browse the repository at this point in the history
CIDR support, removed ipv4_netmask if dhcp
  • Loading branch information
Arman-Keyoumarsi authored Aug 11, 2021
2 parents c21233f + 9db66eb commit db63813
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 @@ -210,8 +210,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 db63813

Please sign in to comment.