How to pass Python Fabric Task Variable into another Function or Task

I hope someone could help me with this problem I am having. So it looks pretty simple, I just wanted to get the variable that was set inside a Fabric task and wanted it to be called in another function. Global Variable seems to not work and I am relatively new to python fabric so any help would be appreciated.

from fabric.api import *
@task
def function_inside(name):

    samplename = name.lower()
    print("{}".format(samplename))


def another_function():
      print "This is outside: " + samplename 

another_funtion()

So after much time working on this primer script that I have, I just have a workaround.

from fabric.api import * container={A:'',B:''}

@task
def first_func(var):
    container['A']=var
@task
def second_func(var):
    container['B']=var
@task
def main_func():
    var1=container['A']
    var2=container['B']
    print "This is from first function: " + var1
    print "This is from second function: " + var2

so when I execute the fabric it goes like this:

fab firs_func:John second_func:Angel main_func

But this is still a workaround and I would still like to know how we can call a variable from a task and pass it to a normal function that is not a tasks.

I don't have idea about fabric but i can help you for global variable

try this:-

def function_inside(name):
    global samplename
    samplename = name.lower()
    print("{}".format(samplename))


def another_function():
      print ("This is outside: " + samplename)

#execute as below
function_inside("HELLO")
another_function()  

Defining tasks, The “new” method starting in 1.1 considers instances of Task or its To see exactly what tasks in your fabfile may be executed via fab , use fab --list . can make for much more sensible code reuse than passing around simple function objects. as the task name; if omitted the instance's variable name will be used instead. How to pass a variable between a task and a called task? I have a task configured to use a trigger file, IE test.done. When a trigger file is found​, a second task is called to transfer a corresponding file with a different extension, test.nist. after the.nist file is copied, the.done file is copied.

I would suggest you to add your "global" variable in Fabric's env dictionary. This dictionary has its state kept during your Python script's lifecycle. Given that, your code would look like:

from fabric.api import *
from fabric.tasks import execute #The 'execute' function is quite useful
                                  when you want to tell Fabric to execute a
                                  function as a Fabric task

#No need to annotate your function
def function_inside(name):

    env.samplename = name.lower()
    print("{}".format(env.samplename))


def another_function():
      print "This is outside: " + env.samplename

execute(function_inside, "Foo")
execute(another_function)

And then, in your shell:

python your_script.py

fab options and arguments, The most common method for utilizing Fabric is via its command-line tool, fab, which by using the provided options and/or passing arguments to individual tasks. Most of the time you will want to just write out the task in your fabfile (​anything you Fabric will try to load on startup and use to update environment variables. If you pass immutable arguments like integers, strings or tuples to a function, the passing acts like call-by-value. The object reference is passed to the function parameters. They can't be changed within the function, because they can't be changed at all, i.e. they are immutable.

What about running tasks synchronously and wait for the result?

# @task
def first_func(var):
    # process var
    return var
# @task
def second_func(var):
    # process var
    return var
@task
def main_func():
    var1=first_func(container['A'])
    # or var1=first_func(var1)
    var2=second_func(container['B'])
    # or var2=first_func(var2)
    print "This is from first function: " + var1
    print "This is from second function: " + var2

fab options and arguments, The most common method for utilizing Fabric is via its command-line tool, fab , which by using the provided options and/or passing arguments to individual tasks. Most of the time you will want to just write out the task in your fabfile (​anything you Fabric will try to load on startup and use to update environment variables. Sets env.rcfile to the given file path, which Fabric will try to load on startup and use to update environment variables. -d COMMAND, --display =COMMAND ¶ Prints the entire docstring for the given task, if there is one. Does not currently print out the task’s function signature, so descriptive docstrings are a good idea.

An advanced method to define tasks in fabric, In addition to the method to define a task with a task decorator, the document self.post_deploy() execute('tomcat7_start') def pre_deploy(self): pass def These tasks are simple yet they can sometimes complicate a fabfile if there defined in env.roles or other variables like AWS region that you have set  I hope someone could help me with this problem I am having. So it looks pretty simple, I just wanted to get the variable that was set inside a Fabric task and wanted it to be called in another function. Global Variable seems to not work and I am relatively new to python fabric so any help would be appreciated.

Fabric does not pass the environment to a local shell like Invoke , Consider this task definition, saved as both fabfile.py and tasks.py : from invoke import task @task def make(c): c.run('env') I initially found this when trying to use fpm which checks the PATH variable to check that the executables it requires to function exist: from You signed in with another tab or window. Function in python is a block of code designed to perform a specific task. In this tutorial, we will learn about functions in Python with different examples and understand how function can be used to define a module and can be imported into another module.

parallel resets custom env variable · Issue #840 · fabric/fabric · GitHub, I'm trying to pass a custom env variable from sub to main task. It works fine when using serial mode. The values are preserve across different  You can use parameter variables anywhere in the body of the function in the same way as you use local variables. The only difference between a parameter variable and a local variable is that Python initializes the parameter variable with the corresponding argument provided by the calling code.

Comments
  • the code you gave me is good for normal functions, I tried it as well but seems like it is a different case with python fabric tasks. global variable is not working (weird). anyway thanks!