Environment variables in yaml files

Is it possible to use environment variables in HA automations, secrets.yaml, etc?

I’d love to be able to not store sensitive information in secrets.yaml, but instead have that info come from /etc/environment. For example, instead of:

db_info: mysql://hass:my [email protected]/homeassistant?charset=utf8

Define MYSQL_USER and MYSQL_USER_PASSWORD in /etc/environment and use

db_info: mysql://${MYSQL_USER}:${MYSQL_USER_PASSWORD}@127.0.0.1/homeassistant?charset=utf8

This way all sensitive information could be encapsulated in one place. I already use these environment variables elsewhere, such as in my docker-compose.yaml file. It would be nice to not have to store them in multiple locations.

Use the secrets file?
HA is a multi-threaded applcation and does not use a shell environment, by default.

I don’t want to have to define my “secret” in multiple locations. I’d like to use environment variables, and ideally reference that environment variable in secrets.yaml.

Found a solution. For docker at least. I found it’s easier to just pass in the entire connection In my docker-compose, using the docker’s host environment variables

environment:
  - TZ=${TZ}
 ...
 ...
  - DB_CONNECTION=mysql://${MYSQL_USER}:${MYSQL_USER_PASSWORD}@${SERVER_IP}/homeassistant?charset=utf8

And then in secrets.yaml

db_connection: !env_var DB_CONNECTION
4 Likes