Environment variables
HAProxy Enterprise supports the use of environment variables to customize its configuration or behavior. These are variables that are defined outside of HAProxy Enterprise, in the operating system or container environment, and are passed to HAProxy Enterprise when it starts. Environment variables can be used to customize the behavior of HAProxy Enterprise and make it easier to manage complex configurations.
By using environment variables to define configuration values, you can reduce the amount of hard-coded values in your configuration files and make it easier to reuse configuration values across multiple instances of HAProxy Enterprise.
HAProxy Enterprise can read environment variables at startup.
Set environment variables in the service file
One way to add environment variables is to append them to the Environment
line in the file /lib/systemd/system/hapee-2.7-lb.service
. Then HAProxy Enterprise will have access to them when it starts up as a service.
We append an environment variable named PROXYNAME
to the Environment
line:
[Service]
Environment="CONFIG=/etc/hapee-2.7/hapee-lb.cfg" "PIDFILE=/run/hapee-2.7-lb.pid" "PROXYNAME=example"
You can then read the environment variable from the HAProxy Enterprise configuration by prefixing it with a dollar sign and surrounding it with quotes.
We retrieve the value of the PROXYNAME
environment variable to use in the Via
HTTP request header's value:
frontend www
mode http
bind :80
# The Via header will contain the HTTP version and the proxy name
http-request add-header Via "%[req.ver] $PROXYNAME"
Set an environment variable with sentenv and presetenv
You can also set environment variables from the HAProxy Enterprise configuration. The setenv
directive in the global
section sets a new or overrides an existing environment variable.
We use sentenv
in the global
section to override the PROXYNAME
variable:
global
setenv PROXYNAME example2
frontend www
bind :80
# Uses the overridden value
http-request add-header Via "%[req.ver] $PROXYNAME"
Alternatively, use the presetenv
directive to set an environment variable only if it has not already been set. If that variable is already set, it will not be overridden.
We use
presetenv
in theglobal
section to set thePROXYNAME
variable only if it has not already been set.
global
presetenv PROXYNAME example2
frontend www
mode http
bind :80
# Uses the value from the environment if it was set
http-request add-header Via "%[req.ver] $PROXYNAME"
Read an environment variable
To read the value of an environment variable, precede it with a dollar sign. Optionally, enclose it in curly braces. In most cases, you must surround the variable with quotes.
In this example, we read several variables in the configuration.
global
setenv IP 192.168.56.10
setenv PORT 8080
setenv USER foo
setenv PASS bar
setenv DEFAULT_BACKEND be_app
setenv LOG_ADDRESS 192.168.56.5
setenv LOG_LEVEL notice
defaults
# Set syslog logging
log "$LOG_ADDRESS" local0 "$LOG_LEVEL"
userlist credentials
# Set a Basic auth username and password
user "$USER" insecure-password "$PASS"
frontend fe_main
# Set the IP address and port using variables
bind "$IP":"$PORT"
# enforce Basic authentication
http-request auth unless { http_auth(credentials) }
default_backend "$DEFAULT_BACKEND"
In ACL expressions, which expect a fetch method, use the env
method to read the variable.
We read the environment variable MAINTENANCE_ON
when deciding whether to show a down for maintenance web page.
global
# Use 1 for true and 0 for false
setenv MAINTENANCE_ON 1
frontend fe_main
bind :80
# Show a 'down for maintenance' page
http-request return status 503 content-type text/html file /etc/hapee-2.7/maintenance.html if { env(MAINTENANCE_ON) -m bool }
default_backend be_app
See also
Next up
Reference Manual