Reference Environment Variable for User / Pass in Inventory

What is the best way for me to reference an environmental variable for a username and password?

I would just create environment variables like:

NORNIR_PASSWORD=whatever
NORNIR_USERNAME=admin
NORNIR_SECRET=foo

Then in my code use:

 os.environ['NORNIR_USERNAME']

Then you can couple that with some of the patterns shown here:

Another example where a hard-coded password could be replaced with the environment variable patterns.

Hi Kirk, any plan to add “lookup” into inventory, like in Ansible?
“ansible_user: “{{ lookup(‘env’, ‘SBX_USER’) }}””

Taking this idea from NetDevOps s02t10
Also following can be used to get var from environment, (dont forget as I did…) you need to be in the same virtualenv, as you exported the var

os.getenv(“SBX_PASS”)

No, not really.

You could already use a sentinel value and dynamically set it in your Python. For example in inventory:

username: sbx_user_dynamic

Then in your Python code if you see that ‘sbx_user_dynamic’ username string do a swap with os.environ[‘sbx_user’]

I definitely would be against embedding programming constructs into the inventory (though sentinel values and encrypted data sound somewhat interesting to me).

Regards, Kirk

We actually have a POC where we were exploring allowing env variables to the config and the inventory:

Not sure if we will end up merging it because I share @ktbyers opinion here but if we do it, it’s going to be something very simple where we only do variable substitutions from the environment. No jinja2 or strange code in the files.

1 Like

Hey All,

Thanks for the feedback. In my code, it’d still be ideal for the config file to reference an env var, as a couple of developers were describing
Did this ever get implemented?

Not yet, looks like the PR got stalled. If someone wants to takeover they are welcome :slight_smile: