smarber
smarber

Reputation: 5084

fabfile doesn't see remote environment variables

My remote server (192.168.3.68) contains several environment variables set in my ~/.bashrc:

# For instance
export MY_DATABASE_HOST=127.0.0.1

When I put run('echo $MY_DATABASE_HOST') in fabfile.py, it shows:

[192.168.3.68] run: echo $MY_DATABASE_HOST
[192.168.3.68] output:

Done
Disconnecting from 192.168.3.68... done.

I've tried adding run('source ~/.bashrc') immediately before the echo but nothing changes.

Why isn't the set environment variables in ~/.bashrc visible to fabfile?

What do I do to fix that because fabfile must be able to read these variables?

UPDATE

from fabric.context_managers import prefix

# This didn't work
with prefix('source /home/meandme/.bashrc'):
    run('echo $MY_DATABASE_HOST')
# This didn't work either
run('source /home/meandme/.bashrc && echo $MY_DATABASE_HOST')

Upvotes: 2

Views: 252

Answers (2)

smarber
smarber

Reputation: 5084

Actually bashrc is executed. But it gets stopped because it's not running interactively through this:

case $- in
     *i*) ;;
     *) return;;
esac

Now it works after I moved my environment variables at the top of my bashrc.

More detailed answer here https://github.com/fabric/fabric/issues/1519

Upvotes: 1

2ps
2ps

Reputation: 15936

Each call of run will open up a new shell and any transient commands in the previous invocation of run are thus lost (e.g., such as setting an environment variable). To elide this issue, you can do two things:

Write your shell commands thusly:

run('source /path/to/.bashrc && echo $MY_DATABASE_HOST')

or Use the prefix context manager

from fabric.context_managers import prefix
with prefix('source /path/to/.bashrc'):
    run('echo $MY_DATABASE_HOST')

Upvotes: 0

Related Questions