Files
opentf/website/docs/cli/commands/console.mdx
Kuba Martin 4d665a0091 Update website/docs/cli. (#171)
* Initial renaming, rewriting and cleaning up wave for the CLI docs.

Signed-off-by: Jakub Martin <kubam@spacelift.io>

* More renaming.

Signed-off-by: Jakub Martin <kubam@spacelift.io>

* More renaming.

Signed-off-by: Jakub Martin <kubam@spacelift.io>

* More renaming.

Signed-off-by: Jakub Martin <kubam@spacelift.io>

* Remove tutorial references.

Signed-off-by: Jakub Martin <kubam@spacelift.io>

* Post-review fixes.

Signed-off-by: Jakub Martin <kubam@spacelift.io>

---------

Signed-off-by: Jakub Martin <kubam@spacelift.io>
2023-08-25 11:09:18 +02:00

121 lines
2.9 KiB
Plaintext

---
page_title: 'Command: console'
description: >-
The opentf console command provides an interactive console for evaluating
expressions.
---
# Command: console
The `opentf console` command provides an interactive console for
evaluating [expressions](/opentf/language/expressions).
## Usage
Usage: `opentf console [options]`
This command provides an interactive command-line console for evaluating and
experimenting with [expressions](/opentf/language/expressions).
You can use it to test interpolations before using them in configurations
and to interact with any values currently saved in
[state](/opentf/language/state). If the current state is empty or has not yet been created, you can use the console to experiment with the expression syntax and
[built-in functions](/opentf/language/functions). The console holds a [lock on the state](/opentf/language/state/locking), and you will not be able to use the console while performing other actions that modify state.
To close the console, enter the `exit` command or press Control-C
or Control-D.
For configurations using
[the `local` backend](/opentf/language/settings/backends/local) only,
`opentf console` accepts the legacy command line option
[`-state`](/opentf/language/settings/backends/local#command-line-arguments).
## Scripting
The `opentf console` command can be used in non-interactive scripts
by piping newline-separated commands to it. Only the output from the
final command is printed unless an error occurs earlier.
For example:
```shell
$ echo 'split(",", "foo,bar,baz")' | opentf console
tolist([
"foo",
"bar",
"baz",
])
```
## Remote State
If [remote state](/opentf/language/state/remote) is used by the current backend,
OpenTF will read the state for the current workspace from the backend
before evaluating any expressions.
## Examples
The `opentf console` command will read the OpenTF configuration in the
current working directory and the OpenTF state file from the configured
backend so that interpolations can be tested against both the values in the
configuration and the state file.
With the following `main.tf`:
```hcl
variable "apps" {
type = map(any)
default = {
"foo" = {
"region" = "us-east-1",
},
"bar" = {
"region" = "eu-west-1",
},
"baz" = {
"region" = "ap-south-1",
},
}
}
resource "random_pet" "example" {
for_each = var.apps
}
```
Executing `opentf console` will drop you into an interactive shell where you
can test interpolations to:
Print a value from a map:
```
> var.apps.foo
{
"region" = "us-east-1"
}
```
Filter a map based on a specific value:
```
> { for key, value in var.apps : key => value if value.region == "us-east-1" }
{
"foo" = {
"region" = "us-east-1"
}
}
```
Check if certain values may not be known until apply:
```
> random_pet.example
(known after apply)
```
Test various functions:
```
> cidrnetmask("172.16.0.0/12")
"255.240.0.0"
```