2016-10-28 00:36:26 +00:00
|
|
|
---
|
2020-02-06 23:45:31 +00:00
|
|
|
layout: docs
|
2023-01-30 14:48:43 +00:00
|
|
|
page_title: env Block - Job Specification
|
2016-10-28 00:36:26 +00:00
|
|
|
description: |-
|
2023-01-30 14:48:43 +00:00
|
|
|
The "env" block configures a list of environment variables to populate the
|
2016-10-28 00:36:26 +00:00
|
|
|
task's environment before starting.
|
|
|
|
---
|
|
|
|
|
2023-01-30 14:48:43 +00:00
|
|
|
# `env` Block
|
2016-10-28 00:36:26 +00:00
|
|
|
|
2020-02-06 23:45:31 +00:00
|
|
|
<Placement groups={['job', 'group', 'task', 'env']} />
|
2016-10-28 00:36:26 +00:00
|
|
|
|
2023-01-30 14:48:43 +00:00
|
|
|
The `env` block configures a list of environment variables to populate the
|
2016-10-28 00:36:26 +00:00
|
|
|
task's environment before starting.
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
job "docs" {
|
|
|
|
group "example" {
|
|
|
|
task "server" {
|
|
|
|
env {
|
2017-07-25 17:13:28 +00:00
|
|
|
my_key = "my-value"
|
2016-10-28 00:36:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## `env` Parameters
|
|
|
|
|
2023-01-30 14:48:43 +00:00
|
|
|
The "parameters" for the `env` block can be any key-value. The keys and values
|
2016-10-28 00:36:26 +00:00
|
|
|
are both of type `string`, but they can be specified as other types. They will
|
2017-07-25 17:13:28 +00:00
|
|
|
automatically be converted to strings. Invalid characters such as dashes (`-`)
|
|
|
|
will be converted to underscores.
|
2016-10-28 00:36:26 +00:00
|
|
|
|
|
|
|
## `env` Examples
|
|
|
|
|
2023-01-30 14:48:43 +00:00
|
|
|
The following examples only show the `env` blocks. Remember that the
|
|
|
|
`env` block is only valid in the placements listed above.
|
2016-10-28 00:36:26 +00:00
|
|
|
|
|
|
|
### Coercion
|
|
|
|
|
|
|
|
This example shows the different ways to specify key-value pairs. Internally,
|
|
|
|
these values will be stored as their string representation. No type information
|
|
|
|
is preserved.
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
env {
|
2018-11-15 18:12:58 +00:00
|
|
|
key = 1.4
|
|
|
|
key = "1.4"
|
2016-10-28 00:36:26 +00:00
|
|
|
|
2018-11-15 18:12:58 +00:00
|
|
|
key = true
|
|
|
|
key = "1"
|
|
|
|
key = 1
|
2016-10-28 00:36:26 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Interpolation
|
|
|
|
|
|
|
|
This example shows using [Nomad interpolation][interpolation] to populate
|
|
|
|
environment variables.
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
env {
|
2018-09-24 23:48:06 +00:00
|
|
|
NODE_CLASS = "${node.class}"
|
2016-10-28 00:36:26 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-08-29 18:35:09 +00:00
|
|
|
### Environment variables with dots
|
|
|
|
|
|
|
|
Environment variables that aren't valid HCLv2 identifiers, like ones containing `.`, require an alternative map assignment syntax.
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
env = {
|
|
|
|
"discovery.type" = "single-node"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2017-08-04 23:14:41 +00:00
|
|
|
### Dynamic Environment Variables
|
|
|
|
|
|
|
|
Nomad also supports populating dynamic environment variables from data stored in
|
|
|
|
HashiCorp Consul and Vault. To use this feature please see the documentation on
|
2023-01-30 14:48:43 +00:00
|
|
|
the [`template` block][template-env].
|
2017-08-04 23:14:41 +00:00
|
|
|
|
2023-01-25 17:31:14 +00:00
|
|
|
[interpolation]: /nomad/docs/runtime/interpolation 'Nomad interpolation'
|
2023-01-30 14:48:43 +00:00
|
|
|
[template-env]: /nomad/docs/job-specification/template#environment-variables 'Nomad template Block'
|