Skip to content

Commit ab46d92

Browse files
committed
Rework the env provider doc to add details and examples
1 parent 2f45edd commit ab46d92

File tree

1 file changed

+83
-6
lines changed

1 file changed

+83
-6
lines changed

reference/fleet/env-provider.md

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,93 @@ products:
88

99
# Env Provider [env-provider]
1010

11-
Provides access to the environment variables as key-value pairs.
11+
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}}.
1212

13-
For example, set the variable `foo`:
13+
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.
14+
15+
Use the env provider when you want to:
16+
17+
- Provide host-specific or environment-specific values, such as proxy settings, regions, or service URLs.
18+
- Reuse shared policies across agents while allowing each host, container, or service to define its own configuration through environment variables.
19+
20+
21+
## Using the env provider [using_env_provider]
22+
23+
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.
24+
25+
For example:
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}}:
1443
1544
```shell
16-
foo=bar elastic-agent run
45+
ELASTICSEARCH_HOST=https://elasticsearch:9200 elastic-agent run
1746
```
1847

19-
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+
::::
2050

21-
::::{note}
22-
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).
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+
```
65+
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.
2367
::::
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)