有没有一种程序性的方法来在Terraform中进行半CIDR块?

发布于 2025-01-29 15:58:39 字数 393 浏览 2 评论 0 原文

我有一个CIDR块,我想将其完全放一半,以便我可以在Terraform项目中使用两个范围。

例如 10.10.10.0/24 ,其中包括范围内的IPS 10.10.10.0-10.10.10.255 可以拆分为 10.10.10.0/25 & 10.10.10.10.128/25

我已经尝试使用 cidrsubnet函数,但我真的不明白如何使用它从第一范围到第二范围。

对此的任何帮助将不胜感激!

I have a CIDR block, and I would like to exactly half it, so that I have two ranges that I can use within my Terraform project.

e.g. 10.10.10.0/24, which includes IPs in range 10.10.10.0 - 10.10.10.255
can be split to 10.10.10.0/25 & 10.10.10.128/25

I've tried looking at this with cidrsubnet function, but I don't really understand how to use it to get from the first range to the second range.

Any help on this would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤慢歌 2025-02-05 15:58:39

可以用 cidrsubnet 如您所指出的那样。

output "first_half" {
  value = cidrsubnet("10.10.10.0/24", 1, 0) # 10.10.10.0/25
}

output "second_half" {
  value = cidrsubnet("10.10.10.0/24", 1, 1) # 10.10.10.128/25
}

说明:

cidrsubnet 获取3个参数: cidrsubnet(前缀,newbits,netnum)

  • 前缀是您要切成两半的实际CIDR范围。
  • newbits 是您要扩展前缀的附加位数。例如,如果您具有/24 前缀,并且想要具有/25 前缀,则 newbits 是25和24之间的差异: 25-24 = 1
  • netnum 是一个整数,不超过 newbits 二进制数字。在我们的情况下,可以是 0 1 0 将是 10.10.10.0/25 的范围,而 1 将代表下半年,为 10.10.10.10.10.128/25

为了给您另一个示例,这使得它更容易理解,让我们剪切 10.10.10.0/24 如果4个范围:

我们知道,我们需要/26 范围,所以 26-24 = 2 newbits 。对于 netnum ,我们可以有0、1、2、3,其中二进制数字为 00 01 10 ,<代码> 11 。

cidrsubnet("10.10.10.0/24", 2, 0) # 10.10.10.0/26
cidrsubnet("10.10.10.0/24", 2, 1) # 10.10.10.64/26
cidrsubnet("10.10.10.0/24", 2, 2) # 10.10.10.128/26
cidrsubnet("10.10.10.0/24", 2, 3) # 10.10.10.192/26

This can be done with cidrsubnet as you pointed out.

output "first_half" {
  value = cidrsubnet("10.10.10.0/24", 1, 0) # 10.10.10.0/25
}

output "second_half" {
  value = cidrsubnet("10.10.10.0/24", 1, 1) # 10.10.10.128/25
}

Explenation:

cidrsubnet takes 3 arguments: cidrsubnet(prefix, newbits, netnum).

  • The prefix is the actual CIDR range you want to cut in half.
  • newbits is the number of additional bits which you want to extend the prefix. For example, if you have /24 prefix and you want to have /25 prefixes, newbits is the difference between 25 and 24: 25 - 24 = 1
  • netnum is a whole number which is no more than newbits binary digits. In our case it can be either 0 or 1. 0 will be the range of 10.10.10.0/25, while 1 will represent the second half, being 10.10.10.128/25.

To give you another example, which makes it more understandable, lets cut 10.10.10.0/24 if 4 ranges:

We know, that we need /26 ranges, so 26 - 24 = 2 for the newbits. For netnum, we can have 0, 1, 2, 3, for which in the binary digits are 00, 01, 10, 11.

cidrsubnet("10.10.10.0/24", 2, 0) # 10.10.10.0/26
cidrsubnet("10.10.10.0/24", 2, 1) # 10.10.10.64/26
cidrsubnet("10.10.10.0/24", 2, 2) # 10.10.10.128/26
cidrsubnet("10.10.10.0/24", 2, 3) # 10.10.10.192/26
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文