Python Fabric won't pass in variable

509 Views Asked by At

I had a script that was working. I made one small change and now it stopped working. The top version works, while the bottom one fails.

def makelocalconfig(file="TEXT"):
    host = env.host_string
    filename = file
    conf = open('/home/myuser/verify_yslog_conf/%s/%s' % (host, filename), 'r')
    comment = open('/home/myuser/verify_yslog_conf/%s/localconfig.txt' % host, 'w')
    for line in conf:
        comment.write(line)
    comment.close()   
    conf.close()


def makelocalconfig(file="TEXT"):
    host = env.host_string
    filename = file
    path = host + "/" + filename
    pwd = local("pwd")
    conf = open('%s/%s' % (pwd, path), 'r')
    comment = open('%s/%s/localconfig.txt' % (pwd, host), 'w')
    for line in conf:
        comment.write(line)
    comment.close()   
    conf.close()

For troubleshooting purposes I added a print pwd and print path line to make sure the variables were getting filled correctly. pwd comes up empty. Why isn't this variable being set correctly? I use this same format of var = sudo("cmd") all the time. Is local different than sudo and run?

1

There are 1 best solutions below

0
On

In short, you may need to add capture=True:

pwd = local("pwd", capture=True)

local runs a command locally:

a convenience wrapper around the use of the builtin Python subprocess module with shell=True activated.

run runs a command on a remote server and sudo runs a remote command as super-user.

There is also a note in the documentation:

local is not currently capable of simultaneously printing and capturing output, as run/sudo do. The capture kwarg allows you to switch between printing and capturing as necessary, and defaults to False.

When capture=False, the local subprocess’ stdout and stderr streams are hooked up directly to your terminal, though you may use the global output controls output.stdout and output.stderr to hide one or both if desired. In this mode, the return value’s stdout/stderr values are always empty.