Matt
Matt

Reputation: 1632

Python - How to set env.variables in Fabric module

I'm trying to run commands in parallel on a few raspberry Pis using the Fabric module. I've read the documentation but I'm still a bit confused.

import fabric

env.hosts = [
    "[email protected]", 
    "[email protected]"
]

env.password = "Raspberry"

@parallel
def command(cmd):
    sudo(cmd)

command("touch /Desktop/new_filename.txt")

When I run that code I get the error env is not defined, how am I supposed to define env here to use those hosts? The documentation is beyond me at this stage.

Upvotes: 0

Views: 606

Answers (1)

2ps
2ps

Reputation: 15916

The below should work for you:

from fabric.context_managers import env
from fabric.decorators import task
from fabric.tasks import execute

env.hosts = [
    "[email protected]", 
    "[email protected]"
]

env.password = "Raspberry"

@task
def command(cmd):
    sudo(cmd)

execute('command', "touch /Desktop/new_filename.txt")

Note the two things we did here: (1) import what we need and (2) make sure to use the task / execute structure to pick up run-time changes to the env.

Upvotes: 1

Related Questions