Emmanuel Emmanuel - 4 months ago 15
Python Question

fabric - is there a way to expand shell variables?

I'm trying to download a file from a remote host, with a remote path depending from a shell variable.

>>> with settings(host_string = 'user@server.com'):
get('$ORACLE_HOME/network/admin/tnsnames.ora')


It throws an error:

Fatal error: get() encountered an exception while downloading '/home/user/$ORACLE_HOME/network/admin/tnsnames.ora'

Underlying exception:
No such file


Is there a way to ask for shell variable expansion?

Answer

You can use fabric.operations.run():

from fabric.operations import run

oracle_path = run('echo $ORACLE_PATH', shell=True)

with settings(host_string = 'user@server.com'):
    get(os.path.join(oracle_path, 'network/admin/tnsnames.ora'))