Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to add an environment variable in fabric?

Tags:

python

fabric

I would like to pass a few values from fabric into the remote environment, and I'm not seeing a great way to do it. The best I've come up with so far is:

with prefix('export FOO=BAR'):     run('env | grep BAR') 

This does seem to work, but it seems like a bit of a hack.

I looked in the GIT repository and it looks like this is issue #263.

like image 647
David Parmenter Avatar asked Nov 29 '11 15:11

David Parmenter


People also ask

How do you add an environment variable?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable.

Should you use .ENV in production?

Using environment variables is a somewhat common practice during Development but it is actually not a healthy practice to use with Production. While there are several reasons for this, one of the main reasons is that using environment variables can cause unexpected persistence of variable values.

Where should I store environment variables?

The Global environment variables of your system are stored in /etc/environment . Any changes here will get reflected throughout the system and will affect all users of the system. Also, you need a Reboot, for any changes made here to take effect.


2 Answers

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") 
like image 158
Shanmu Avatar answered Sep 28 '22 05:09

Shanmu


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, prefix from contextlib import contextmanager  @contextmanager def 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_shell  def 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. 
like image 40
jcollado Avatar answered Sep 28 '22 03:09

jcollado