Typesafe config does not substitude environment variables [solved]

I’m deploying a playframework app within kubernetes and want to configure some key’s based on environment variables that I set in the kubernetes deployment yaml file.


So the environment variables CASSANDRA_SERVICE_NAME and CASSANDRA_NAMESPACE are assigned to some values and I want to reference them from my application.conf:

cassandra-journal.contact-points = ["${CASSANDRA_SERVICE_NAME}.${CASSANDRA_NAMESPACE}.svc.cluster.local"]

The typesafe config documentation claims that unset variables get substituded agains environent variables, but that does not work in my case:

All following lines yield ["${CASSANDRA_SERVICE_NAME}.${CASSANDRA_NAMESPACE}.svc.cluster.local"]

// `config: play.api.Configuration` is injected into my play app
println(config.get[Seq[String]]("cassandra-journal.contact-points"))
println(config.underlying.getStringList("cassandra-journal.contact-points"))
println(config.underlying.resolve().getStringList("cassandra-journal.contact-points"))

The environment variable is definitely set:

System.getenv("CASSANDRA_SERVICE_NAME") yields the correct value.
ConfigFactory.systemEnvironment does also contain my environment variables

I just found the solution: Unlike scala, the ${...} syntax in hocon may not used within string literals.
The following worked:

contact-points = [${CASSANDRA_SERVICE_NAME}.${CASSANDRA_NAMESPACE}.svc.cluster.local]
1 Like

I have a similar problem. I want to provide the host part of the slick db url from an environment variable:

slick.dbs.default.db.url="jdbc:postgresql://${X_POSTGRES_HOST}:5432"

When I run the application, this just will not resolve the environment variable X_POSTGRES_HOST.

Slick expects a String in slick.dbs.default.db.url, but if I provide a String with quotes like shown above, the environment variable will not be resolved. I’m stuck. Any ideas?

Got this information here that solves it for me: Environment substitution does not work · Issue #633 · lightbend/config · GitHub

Substitution only happens outside quotes. This is a JSON superset, so quoted strings have the same meaning as in JSON. write it as ”text“${HOST}”moretext”

1 Like