Skip to content

Commit 0cd8faa

Browse files
Explain the env provider and its usage (#4065)
## Summary This PR reworks the `env-provider.md` doc to add details and examples for using the env provider, including variable chaining and fallbacks. Closes #1928 and #2475 ## Generative AI disclosure 1. Did you use a generative AI (GenAI) tool to assist in creating this contribution? - [X] Yes - [ ] No 2. Tool(s) and model(s) used: - I used Cursor with the `gpt-5.1` model to create a first draft and add examples for the env provider’s usage based on the codebase. I then edited the draft to achieve the desired structure, logical flow, and clarity.
1 parent 50624e6 commit 0cd8faa

File tree

1 file changed

+81
-6
lines changed

1 file changed

+81
-6
lines changed

reference/fleet/env-provider.md

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,91 @@ products:
1010

1111
# Env provider [env-provider]
1212

13-
Provides access to the environment variables as key-value pairs.
13+
The env provider gives access to the environment variables available to the {{agent}} process as key-value pairs. You can then reference these values in the {{agent}} configuration (for standalone agents) or in agent and integration policies in {{fleet}}.
1414

15-
For example, set the variable `foo`:
15+
Using environment variables lets you keep host-specific settings and deployment-specific values out of your policies and configuration files. This is especially useful in larger setups where you want to reuse the same {{agent}} policy but allow each host or container to supply its own settings.
16+
17+
Use the env provider when you want to:
18+
19+
- Provide host-specific or environment-specific values, such as proxy settings, regions, or service URLs.
20+
- Reuse shared policies across agents while allowing each host, container, or service to define its own configuration through environment variables.
21+
22+
23+
## Using the env provider [using_env_provider]
24+
25+
The env provider is enabled by default and requires no configuration. Define environment variables in your operating system, service definition, or container platform, and reference them in the {{agent}} configuration or {{fleet}} policy using the `${env.VAR_NAME}` syntax.
26+
27+
:::::{tab-set}
28+
29+
::::{tab-item} Standalone {{agent}}
30+
31+
On standalone {{agent}}, you can reference environment variables directly in the `elastic-agent.yml` configuration file.
32+
33+
For example, to use an environment variable as the value of the {{es}} host URL:
34+
35+
```yaml
36+
outputs:
37+
default:
38+
type: elasticsearch
39+
hosts: ["${env.ELASTICSEARCH_HOST}"]
40+
```
41+
42+
Then set the environment variable before starting {{agent}}:
1643
1744
```shell
18-
foo=bar elastic-agent run
45+
ELASTICSEARCH_HOST=https://elasticsearch:9200 elastic-agent run
1946
```
2047

21-
The environment variable can be referenced as `${env.foo}`.
48+
The standalone agent resolves `${env.ELASTICSEARCH_HOST}` at runtime based on the environment of the agent process.
49+
::::
50+
51+
::::{tab-item} {{fleet}}-managed {{agent}}
52+
53+
On {{fleet}}-managed {{agent}}, you can define environment variables on each host running {{agent}} and reference them in the integration or agent policy using the `${env.VAR_NAME}` syntax.
54+
55+
For example, you can use an environment variable to set a host-specific log path in a filestream integration:
56+
57+
```yaml
58+
inputs:
59+
- type: filestream
60+
enabled: true
61+
streams:
62+
- paths:
63+
- "${env.APP_LOG_DIR}/app.log"
64+
```
2265
23-
::::{note}
24-
If you run the agent as a Linux or Windows service, you can also define the environment variables in the service manifest. Refer to the example in [Proxy Server connectivity using default host variables](/reference/fleet/host-proxy-env-vars.md).
66+
Each {{agent}} uses the env provider to resolve `${env.APP_LOG_DIR}` from the environment variables defined on the host at runtime. This allows a single policy in {{fleet}} to adapt its behavior per host without creating multiple policies.
2567
::::
68+
69+
:::::
70+
71+
:::{note}
72+
If you're running {{agent}} as a Linux or Windows service, you can define environment variables in the service manifest or environment configuration. Refer to the example in [Proxy Server connectivity using default host variables](/reference/fleet/host-proxy-env-vars.md) for more details.
73+
74+
For containerized deployments, refer to [{{agent}} environment variables](/reference/fleet/agent-environment-variables.md) for a list of supported variables.
75+
:::
76+
77+
78+
## Variable chaining and fallbacks [env_provider_fallbacks]
79+
80+
The env provider supports chaining multiple variables with fallback values. This is useful when you want to try multiple environment variables in order, with a default value when none are set.
81+
82+
Using fallbacks helps you create robust policies that work across different environments without requiring every variable to be defined on every host or container.
83+
84+
Use the following syntax:
85+
86+
```yaml
87+
${env.VAR1|env.VAR2|env.VAR3|'default-value'} <1>
88+
```
89+
90+
1. {{agent}} evaluates the expression at runtime from left to right. If none of the variables are set, it uses the final literal value (in this example, `'default-value'`).
91+
92+
You can chain as many environment variables as needed, and the final fallback can be any literal string or value that the configuration field accepts.
93+
94+
For example:
95+
96+
```yaml
97+
logging.level: ${env.LOG_LEVEL|env.DEFAULT_LOG_LEVEL|'info'}
98+
```
99+
100+
This tries the `LOG_LEVEL` environment variable first, then `DEFAULT_LOG_LEVEL`, and finally defaults to `'info'` if neither variable is set.

0 commit comments

Comments
 (0)