# Dynamic Server Weight from a variable

**URL:** <https://discourse.haproxy.org/t/dynamic-server-weight-from-a-variable/11484>\
**Category:** Help!\
**Created:** [December 16, 2024, 11:47am UTC](https://discourse.haproxy.org/t/dynamic-server-weight-from-a-variable/11484 "2024-12-16T11:47:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![digitalkaoz](https://avatars.discourse-cdn.com/v4/letter/d/7ba0ec/32.png) [@digitalkaoz](https://discourse.haproxy.org/u/digitalkaoz)\
**Post date:** [December 16, 2024, 11:47am UTC](https://discourse.haproxy.org/t/dynamic-server-weight-from-a-variable/11484/1 "2024-12-16T11:47:18Z")

</div>

Hey folks,

im trying to configure server weights from environment variables. I ended up with this but its not working (503) (stripped away not relevant parts of the config)

```auto
global
  set-var proc.percentage_new int("${PERCENTAGE_NEW}")
  set-var proc.percentage_old int("${PERCENTAGE_OLD}")

backend percentage_strategy
  option tcp-check

  cookie sticky_server_cookie insert indirect

  # old domain
  cookie old_domain
  server old_domain_percentage example.com:443 check ssl verify none weight var(proc.percentage_old)

  # new domain
  cookie new_domain
  server new_domain_percentage new-example.com:443 check ssl verify none weight var(proc.percentage_new)

```

googling doesnt give my any hint if thats possible. In the end i want to create an immutable docker image which i can reconfigure just by providing the weighted env vars:

```auto
docker run -e PERCENTAGE_NEW=5 -e PERCENTAGE_OLD=95 my-haproxy

```

also i thought basic arithmetics are possible so i only have to provide the PERCENTAGE\_NEW and do a simple calculation for the old weight like

```auto
server old_domain_percentage example.com:443 check ssl verify none weight 100-var(proc.percentage_new)

```

but that doesnt even produce a valid config. I didnt wanted to go the lua route, if its not possible its ok to provide both variables (the math upfront isnt that hard i guess 😉 )

---

<div class="post-metadata">

**Author:** ![adarragon](https://avatars.discourse-cdn.com/v4/letter/a/7bcc69/32.png) [@adarragon](https://discourse.haproxy.org/u/adarragon)\
**Post date:** [December 19, 2024, 12:56pm UTC](https://discourse.haproxy.org/t/dynamic-server-weight-from-a-variable/11484/2 "2024-12-19T12:56:03Z")

</div>

Indeed it is not possible to configure server weight using haproxy variables, as weight directive expects an integer

Available options are: env variables (may be used to populate haproxy config at startup), update from the cli/lua, or DNS-based discovery

---

<div class="post-metadata">

**Author:** ![adarragon](https://avatars.discourse-cdn.com/v4/letter/a/7bcc69/32.png) [@adarragon](https://discourse.haproxy.org/u/adarragon)\
**Post date:** [December 19, 2024, 1:44pm UTC](https://discourse.haproxy.org/t/dynamic-server-weight-from-a-variable/11484/3 "2024-12-19T13:44:17Z")

</div>

For env variables, the correct syntax is:

```auto
server old_domain_percentage example.com:443 check ssl verify none weight "${ENV_VAR_NAME}"

```

See: [https://docs.haproxy.org/dev/configuration.html#2.3](https://docs.haproxy.org/dev/configuration.html#2.3)

---

<div class="post-metadata">

**Author:** ![digitalkaoz](https://avatars.discourse-cdn.com/v4/letter/d/7ba0ec/32.png) [@digitalkaoz](https://discourse.haproxy.org/u/digitalkaoz)\
**Post date:** [December 23, 2024, 2:30pm UTC](https://discourse.haproxy.org/t/dynamic-server-weight-from-a-variable/11484/4 "2024-12-23T14:30:50Z")

</div>

Hey @adarragon that how i figured it out myself. Sadly by only doing that, you loose “type validation” (setting the env variable to `foo` wouldnt result in an error but just in a “backend is unhealthy”.

That why i additionally added a type casting:

```auto
  # we source them from the environment here to actually typecheck them
  set-var proc.percentage_new int("${PERCENTAGE_NEW}")
  set-var proc.percentage_old int("${PERCENTAGE_OLD}")

```

so it immediatly fails on startup and it is safe to use the env-var as a weight for servers.
