Best way to add an environment variable in fabric? Best way to add an environment variable in fabric? python python

Best way to add an environment variable in fabric?


As of fabric 1.5 (released), fabric.context_managers.shell_env does what you want.

    with shell_env(FOO1='BAR1', FOO2='BAR2', FOO3='BAR3'):        local("echo FOO1 is $FOO1")


I think your prefix-based solution is perfectly valid. However, if you want to have a shell_env context manager as the one proposed in issue#263, you can use the following alternative implementation in your fab files:

from fabric.api import run, env, prefixfrom contextlib import contextmanager@contextmanagerdef shell_env(**env_vars):    orig_shell = env['shell']    env_vars_str = ' '.join('{0}={1}'.format(key, value)                           for key, value in env_vars.items())    env['shell']='{0} {1}'.format(env_vars_str, orig_shell)    yield    env['shell']= orig_shelldef my_task():    with prefix('echo FOO1=$FOO1, FOO2=$FOO2, FOO3=$FOO3'):        with shell_env(FOO1='BAR1', FOO2='BAR2', FOO3='BAR3'):            run('env | grep BAR')

Note that this context manager modifies env['shell'] instead of env['command_prefixes'] (as prefix context manager does), so you:

  • can still use prefix (see example output below) without the interaction problems mentioned in issue#263.
  • have to apply any changes to env['shell'] before using shell_env. Otherwise, shell_env changes will be overwritten and environment variables won't be available for your commands.

When executing the fab file above, you get the following output:

$ fab -H localhost my_task[localhost] Executing task 'my_task'[localhost] run: env | grep BAR[localhost] out: FOO1=BAR1, FOO2=BAR2, FOO3=BAR3[localhost] out: FOO1=BAR1[localhost] out: FOO2=BAR2[localhost] out: FOO3=BAR3[localhost] out: Done.Disconnecting from localhost... done.


Fabric 1.5.0 (currently in Git) takes shell as local() named argument.If you pass '/bin/bash' there it passes it to executable argument of Popen.

It won't execute your .bashrc though because .bashrc is sourced on interactive invocation of bash. You can source any file you want inside local:

local('. /usr/local/bin/virtualenvwrapper.sh && workon focus_tests && bunch local output', shell='/bin/bash')