-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a companion to cidrsubnet that allows bulk-allocation of multiple subnet addresses at once, with automatic numbering. Unlike cidrsubnet, cidrsubnets allows each of the allocations to have a different prefix length, and will pack the networks consecutively into the given address space. cidrsubnets can potentially create more complicated addressing schemes than cidrsubnet alone can, because it's able to take into account the full set of requested prefix lengths rather than just one at a time.
- Loading branch information
1 parent
54661ec
commit f84ab99
Showing
6 changed files
with
312 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
--- | ||
layout: "functions" | ||
page_title: "cidrsubnets - Functions - Configuration Language" | ||
sidebar_current: "docs-funcs-ipnet-cidrsubnets" | ||
description: |- | ||
The cidrsubnets function calculates a sequence of consecutive IP address | ||
ranges within a particular CIDR prefix. | ||
--- | ||
|
||
# `cidrsubnet` Function | ||
|
||
-> **Note:** This page is about Terraform 0.12 and later. For Terraform 0.11 and | ||
earlier, see | ||
[0.11 Configuration Language: Interpolation Syntax](../../configuration-0-11/interpolation.html). | ||
|
||
`cidrsubnet` calculates a sequence of consecutive IP address ranges within | ||
a particular CIDR prefix. | ||
|
||
```hcl | ||
cidrsubnet(prefix, newbits...) | ||
``` | ||
|
||
`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 | ||
related function [`cidrsubnet`](./cidrsubnet.html). `cidrsubnet` calculates | ||
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. | ||
|
||
## 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 | ||
[`for` expressions](/docs/configuration/expressions.html#for-expressions) | ||
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 | ||
|
||
* [`cidrhost`](./cidrhost.html) calculates the IP address for a single host | ||
within a given network address prefix. | ||
* [`cidrnetmask`](./cidrnetmask.html) converts an IPv4 network prefix in CIDR | ||
notation into netmask notation. | ||
* [`cidrsubnet`](./cidrsubnet.html) calculates a single subnet address, allowing | ||
you to specify its network number. |