2019-09-20 04:06:15 +02:00
|
|
|
---
|
2021-12-15 03:41:17 +01:00
|
|
|
page_title: cidrsubnets - Functions - Configuration Language
|
2019-09-20 04:06:15 +02:00
|
|
|
description: |-
|
|
|
|
The cidrsubnets function calculates a sequence of consecutive IP address
|
|
|
|
ranges within a particular CIDR prefix.
|
|
|
|
---
|
|
|
|
|
2019-11-26 13:50:32 +01:00
|
|
|
# `cidrsubnets` Function
|
2019-09-20 04:06:15 +02:00
|
|
|
|
2019-11-26 13:50:32 +01:00
|
|
|
`cidrsubnets` calculates a sequence of consecutive IP address ranges within
|
2019-09-20 04:06:15 +02:00
|
|
|
a particular CIDR prefix.
|
|
|
|
|
|
|
|
```hcl
|
2019-11-26 13:50:32 +01:00
|
|
|
cidrsubnets(prefix, newbits...)
|
2019-09-20 04:06:15 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
`prefix` must be given in CIDR notation, as defined in
|
|
|
|
[RFC 4632 section 3.1](https://tools.ietf.org/html/rfc4632#section-3.1).
|
|
|
|
|
|
|
|
The remaining arguments, indicated as `newbits` above, each specify the number
|
|
|
|
of additional network prefix bits for one returned address range. The return
|
|
|
|
value is therefore a list with one element per `newbits` argument, each
|
|
|
|
a string containing an address range in CIDR notation.
|
|
|
|
|
|
|
|
For more information on IP addressing concepts, see the documentation for the
|
2021-12-15 03:41:17 +01:00
|
|
|
related function [`cidrsubnet`](/language/functions/cidrsubnet). `cidrsubnet` calculates
|
2019-09-20 04:06:15 +02:00
|
|
|
a single subnet address within a prefix while allowing you to specify its
|
|
|
|
subnet number, while `cidrsubnets` can calculate many at once, potentially of
|
|
|
|
different sizes, and assigns subnet numbers automatically.
|
|
|
|
|
|
|
|
When using this function to partition an address space as part of a network
|
|
|
|
address plan, you must not change any of the existing arguments once network
|
|
|
|
addresses have been assigned to real infrastructure, or else later address
|
|
|
|
assignments will be invalidated. However, you _can_ append new arguments to
|
|
|
|
existing calls safely, as long as there is sufficient address space available.
|
|
|
|
|
|
|
|
This function accepts both IPv6 and IPv4 prefixes, and the result always uses
|
|
|
|
the same addressing scheme as the given prefix.
|
|
|
|
|
2021-08-17 20:30:18 +02:00
|
|
|
-> **Note:** As a historical accident, this function interprets IPv4 address
|
|
|
|
octets that have leading zeros as decimal numbers, which is contrary to some
|
|
|
|
other systems which interpret them as octal. We have preserved this behavior
|
|
|
|
for backward compatibility, but recommend against relying on this behavior.
|
|
|
|
|
2019-09-21 03:00:07 +02:00
|
|
|
-> **Note:** [The Terraform module `hashicorp/subnets/cidr`](https://registry.terraform.io/modules/hashicorp/subnets/cidr)
|
|
|
|
wraps `cidrsubnets` to provide additional functionality for assigning symbolic
|
|
|
|
names to your networks and skipping prefixes for obsolete allocations. Its
|
|
|
|
documentation includes usage examples for several popular cloud virtual network
|
|
|
|
platforms.
|
|
|
|
|
2019-09-20 04:06:15 +02:00
|
|
|
## Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
> cidrsubnets("10.1.0.0/16", 4, 4, 8, 4)
|
|
|
|
[
|
|
|
|
"10.1.0.0/20",
|
|
|
|
"10.1.16.0/20",
|
|
|
|
"10.1.32.0/24",
|
|
|
|
"10.1.48.0/20",
|
|
|
|
]
|
|
|
|
|
|
|
|
> cidrsubnets("fd00:fd12:3456:7890::/56", 16, 16, 16, 32)
|
|
|
|
[
|
|
|
|
"fd00:fd12:3456:7800::/72",
|
|
|
|
"fd00:fd12:3456:7800:100::/72",
|
|
|
|
"fd00:fd12:3456:7800:200::/72",
|
|
|
|
"fd00:fd12:3456:7800:300::/88",
|
|
|
|
]
|
|
|
|
```
|
|
|
|
|
|
|
|
You can use nested `cidrsubnets` calls with
|
2021-12-15 03:41:17 +01:00
|
|
|
[`for` expressions](/language/expressions/for)
|
2019-09-20 04:06:15 +02:00
|
|
|
to concisely allocate groups of network address blocks:
|
|
|
|
|
|
|
|
```
|
|
|
|
> [for cidr_block in cidrsubnets("10.0.0.0/8", 8, 8, 8, 8) : cidrsubnets(cidr_block, 4, 4)]
|
|
|
|
[
|
|
|
|
[
|
|
|
|
"10.0.0.0/20",
|
|
|
|
"10.0.16.0/20",
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"10.1.0.0/20",
|
|
|
|
"10.1.16.0/20",
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"10.2.0.0/20",
|
|
|
|
"10.2.16.0/20",
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"10.3.0.0/20",
|
|
|
|
"10.3.16.0/20",
|
|
|
|
],
|
|
|
|
]
|
|
|
|
```
|
|
|
|
|
|
|
|
## Related Functions
|
|
|
|
|
2021-12-15 03:41:17 +01:00
|
|
|
* [`cidrhost`](/language/functions/cidrhost) calculates the IP address for a single host
|
2019-09-20 04:06:15 +02:00
|
|
|
within a given network address prefix.
|
2021-12-15 03:41:17 +01:00
|
|
|
* [`cidrnetmask`](/language/functions/cidrnetmask) converts an IPv4 network prefix in CIDR
|
2019-09-20 04:06:15 +02:00
|
|
|
notation into netmask notation.
|
2021-12-15 03:41:17 +01:00
|
|
|
* [`cidrsubnet`](/language/functions/cidrsubnet) calculates a single subnet address, allowing
|
2019-09-20 04:06:15 +02:00
|
|
|
you to specify its network number.
|