¯\_(ツ)_/¯

thunder@home:~$

This is my home blog, mostly to share some useful info or code snippets
< 1 min

Hey all,

Once upon a time, I was need to get hostmin/hostmax value from CIDR like famous http://jodies.de/ipcalc do:

Address:   10.16.0.0             00001010.00010000 .00000000.00000000
Netmask:   255.255.0.0 = 16      11111111.11111111 .00000000.00000000
Wildcard:  0.0.255.255           00000000.00000000 .11111111.11111111
=>
Network:   10.16.0.0/16          00001010.00010000 .00000000.00000000 (Class A)
Broadcast: 10.16.255.255         00001010.00010000 .11111111.11111111
HostMin:   10.16.0.1             00001010.00010000 .00000000.00000001
HostMax:   10.16.255.254         00001010.00010000 .11111111.11111110
Hosts/Net: 65534                 (Private Internet)

And here is my kludge in Terraform to achieve this:

variable "cidr_block" {
  type = string
  default = "10.100.0.0/29"
}

locals {
  netmask = tonumber(split("/", var.cidr_block)[1])
  power   = pow(2, 32 - local.netmask) - 2
  network = {
    gateway = cidrhost(var.cidr_block, 1),
    netmask = cidrnetmask(var.cidr_block),
    hostmin = cidrhost(var.cidr_block, 2),
    hostmax = cidrhost(var.cidr_block, local.power)
  }
}

That’s it! Happy terrakludging!

Thank You For Reading