Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

Extract IP from a range with Terraform

I'm trying to extract IP addresses from a range with Terraform.

For example, I defined this range 192.168.1.10-192.168.1.20 as a string and I would like to get a list like this: [192.168.1.10,192.168.1.11,…,192.168.1.20].

I checked for Terraform functions but didn’t find a way to do that.

Is this possible?

For further context, I am deploying MetalLB in a Kubernetes cluster and need to define the VIP range as a string like this 192.168.1.10-192.168.1.20. The Kubernetes cluster is deployed on OpenStack and I need to configure Neutron OpenStack port to accept all IP addresses from this range:

resource "openstack_networking_port_v2" "k8s_worker_mgmt_port" {
  name           = "k8s_worker_mgmt_port"
  network_id     = data.openstack_networking_network_v2.k8s_openstack_mgmt_network_name.id
  admin_state_up = "true"

  allowed_address_pairs {
      ip_address = "192.168.1.10"
    }

  allowed_address_pairs {
      ip_address = "192.168.1.11"
    }
  }
....
}
question from:https://stackoverflow.com/questions/65561023/extract-ip-from-a-range-with-terraform

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you can rely on the first 3 octets of the IP range being the same then you can get away with using a combination of split, slice, join, range and formatlist functions to do this natively inside Terraform with something like the following:

variable "ip_range" {
  default = "192.168.1.10-192.168.1.20"
}

locals {
  ip_range_start = split("-", var.ip_range)[0]
  ip_range_end   = split("-", var.ip_range)[1]

  # Note that this naively only works for IP ranges using the same first three octects
  ip_range_first_three_octets = join(".", slice(split(".", local.ip_range_start), 0, 3))
  ip_range_start_fourth_octet = split(".", local.ip_range_start)[3]
  ip_range_end_fourth_octet   = split(".", local.ip_range_end)[3]

  list_of_final_octet  = range(local.ip_range_start_fourth_octet, local.ip_range_end_fourth_octet)
  list_of_ips_in_range = formatlist("${local.ip_range_first_three_octets}.%s", local.list_of_final_octet)
}

output "list_of_ips_in_range" {
  value = local.list_of_ips_in_range
}

This outputs the following:

list_of_ips_in_range = [
  "192.168.1.10",
  "192.168.1.11",
  "192.168.1.12",
  "192.168.1.13",
  "192.168.1.14",
  "192.168.1.15",
  "192.168.1.16",
  "192.168.1.17",
  "192.168.1.18",
  "192.168.1.19",
]

If you need to offset that range so you end up with IP addresses from .11 to .20 from the same input then you can do that by changing the local.list_of_final_octet like so:

  list_of_final_octet  = range(local.ip_range_start_fourth_octet + 1, local.ip_range_end_fourth_octet + 1)

Unfortunately Terraform doesn't have any built in functions for doing more elaborate CIDR math beyond cidrhost, cidrnetmask, cidrsubnet, cidrsubnets functions so if you have more complex requirements then you may need to delegate this to an external script that can calculate it and be called via the external data source.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...