Fabric + DigitalOcean: unvisible ENVIRONMENT VARIA

2019-07-20 19:28发布

问题:

I have server on DigitalOcean and I use fabric for deploying my code. And my code is dependent on environment variables (flask config). I've faced one strange trouble today: on server in /root/.bashrc I've added next line

export CONFIG=/path/to/conf

And when I executing on remote console

>>> echo $CONFIG
>>> /path/to/conf  # result, as it should be

But in fabfile I have a function

def show():
    run("echo $CONFIG")

And it prints an empty string. WHY? Thanks in advance!

回答1:

This shouldn't be the case, as by default Fabric uses the -l flag which is supposed to Make bash act as if it had been invoked as a login shell (see INVOCATION below).

I am also not able to reproduce your issue:

$ fab -H home test_bash
[home] Executing task 'test_bash'
[home] run: grep FOO $HOME/.bashrc
[home] out: export FOO="BAR"
[home] out:

[home] run: echo $FOO
[home] out: BAR
[home] out:

So my guess is that you've changed the env.shell, or that you're not connecting as the root user which is where you're setting this bash export. (Note that run() isn't the same a sudo() in Fabric usage)