It's yet another tool: direnv
direnv manages shell environments.
direnv is one of these tools that you basically setup once and after that forget that it's there. You just notice it when it does the job you set it up for and are happy it saves you a lot of hassle.
Enter a directory that you've configured with direnv and it will import things into your environment. That works well for e.g. programming languages where you need specific tools in your PATH or you just need an environment variable to point to a specific file in that environment, like the ANSIBLE_INVENTORY variable. Got two ansible environments in ~/ansible-test and ~/ansible-prod? Drop the following file as .envrc into each one:
You can now
cd ~/ansible-test/roles/sshkeys and when running ansible it will use ~/ansible-test/hosts as its inventory file.
Security is good, direnv only executes files that you have authorized by executing
direnv allow in that directory. And if the file changes, you need to authorize the file again, so nobody can sneak in bad commands.
direnv also allows importing the environments of other tools like rbenv, Nix, Guix, rvm and node.
With the Nix package manager it's even possible to install programs on demand. Add the line
use nix -p ansible to the above .envrc and direnv will ensure that ansible is installed when you enter that directory. Leave that directory and ansible is gone again. I'm assuming you don't have it installed system-wide or in your user-profile.
Another way to use direnv comes from @firstname.lastname@example.org, as he puts it:
direnv + git env vars = simple way to manage identities per customer
direnv really helps avoid cluttering your regular shell environment from single-use environment variables and you won't have to remember the names of the files to source to setup a specific environment anymore.