简体   繁体   中英

How do I unset an environment variable with python tox?

I know well about passenv and setenv use on tox.ini files but I am facing a case where I want to undefine some variables so they are not included in the wildcard passenv.

Example

[testenv]
passenv = ANSIBLE_*

# I how do I assure that ANSIBLE_INVENTORY is not passed?

To quote the docs:

tox will take care of environment isolation for you: it will strip away all operating system environment variables not specified via passenv .

That is, you just do not mention the variables you don't want passed.

For passenv , docs say:

You can override these variables with the setenv option.

Since a shell variable which is unset looks like an empty string, you can try to setenv to '' all the variables you don't want to pass. It may or may not work, depending on how these variables are used by the code, because there is a difference between an empty env variable and an unset one.

I don't see any way to selectively unset variables in the tox docs. I would suggest to use a few wildcards weaker than ANSIBLE_* so that they don't let in the variables you want to stay unset.

Another solution would be to unset the variables in the shell that runs tox , so that the tox process was unable to access and pass them:

#!/bin/sh
unset ANSIBLE_KEYS_TO_KINGDOM
tox "$@"

So far it seems impossible to perform an unset like this, so I raised I raised it as a feature request at https://github.com/tox-dev/tox/issues/1387

There is an workaround but it is so inconvenient that I do not even know if it worth mentioning:

command = bash -c "unset ANSIBLE_INVENTORY; <original command>"

As you can see, this approach does not scale at all and can make maintenance of tox.ini a real burden.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM