-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: allow ConfigObj to transparently read settings from environment vars #144
Comments
This change is pretty independent of any containerization setup. The same environment variables can be used in configuration management, or server orchestration setups using cloud-init + ansible or similar. |
Well, if your legacy application already reads its config from a series of files, you have a few options for containerization:
This proposal allows applications to read config from the environment without changing any application code 😃 |
If anyone is looking for a pragmatic and somewhat gritty way of doing this, here's what I do. If you're loading config like this:
but would like a config file like this:
to be:
change the Python loading code to:
And add this function:
|
Instead of the bool, this should use "envar_prefix=MYAPP" (default empty or |
This comment has been minimized.
This comment has been minimized.
I think this worth considering for a new major version |
Proposal
Add a
use_env_vars
mode in ConfigObj that allows ConfigObj to transparently read settings from environment variables, if they exist.If
use_env_vars
isFalse
– the default – then ConfigObj works the way it always has.If
use_env_vars
isTrue
then ConfigObj will attempt to read settings from the environment first, before reading settings loaded from a file.Setting names will be converted to valid environment variable names when reading from the environment:
This mode could potentially also be enabled by adding a special setting directly in a config file, thus enabling this feature without changing any existing application code:
Example
Rationale
Transparently reading settings from the environment should ease the containerization of any app that uses ConfigObj.
The
use_env_vars
setting is completely backwards compatible, so it shouldn't disrupt any apps that currently use ConfigObj.Further Thoughts
The behavior of
use_env_vars
mode could be further modified by a series of settings. Each of these settings could also potentially be configured directly in a config file using the special[ConfigObj]
section:env_vars_section_separator
The
env_vars_section_separator
parameter sets the separator used to concatenate section names and keywords (defaults to"__"
).env_vars_prefix
To prevent environment variable collisions, an
env_vars_prefix
parameter can set a global prefix used when checking the environment (defaults to empty string).env_vars_uppercase
In general, environment variable names are always uppercased, but this behavior could be turned off using an
env_vars_uppercase
parameter (defaults toTrue
).The text was updated successfully, but these errors were encountered: