I am setting up a new infrastructure as code repository for a small to medium size project. The problem I am currently trying to solve is how to store my environment variables such that they can be read and used by all services and scripts within the repo. Some of the tools being used are Ansible, Powershell, Bash...

My current solution is a simple text file, manually created per environment. Each line has a variable that the services and scripts can look up. However, this seems clunky, as I have to create the logic to read this file in every script that is made.

So my question is, what approach should I be taking to provide all my services with environment variables? Am I on the right track, or should I be using something entirely different for this?

I'm not sure what I can use to provide this to all the different tools. For details sake, I am not talking about secrets management. I mean variables such as: DNS address of the environment's DC, log server address, etc... There are significant constraints on addition of new software/tools due to the sensitivity of the environment. For example, I was considering using JSON format for the environment file, but Bash does not parse that natively. Meaning I would have to install a third party tool like 'jq' to use.

Any feedback welcome. Thank you!

1 Answer 1


General Notes

I don't really understand this one:

However, this seems clunky, as I have to create the logic to read this file in every script that is made.

You can source all variables from a bash file in one line (source file-name.sh). If they are "exported", you can pick them up in applications in any language you like (Java, python, or whatever), and most have built in syntax for doing it (like spring boot's SPEL syntax which lets you define your config to directly populate from environment variables automatically on start-up).

You can also make this export part of your ~/.bashrc file or whatever so it's auto sourced in your shell contexts, etc.

Repositories + CI/CD

This may depend on your repository and CI implementation more than the installed code. For example, GitLab CI and GitHub Actions both support environment variables, as does Jenkins, etc.

In GitLab you can even go to your group level (above a bunch of related projects), and set per-environment environment variables there which are pushed into your deployment so you can use them in your installed code. Then, you don't even need to store them in any extra files.



Whatever your code is, you can probably bundle it into docker and pass the variables to the container at run time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.