1

Been trying to solve this but can't seem to make it work. I want to create a log file that looks like $HOSTNAME-timestamp. For example I have this:

def test_servers():
    env.user = getpass.getuser()
    env.hosts = ['servernumber1', 'servernumber2']


def logname():
        timestamp = time.strftime("%b_%d_%Y_%H:%M:%S")
        'env.hosts' + timestamp

def audit():
        name = logname()
        sys.stdout = open('/home/path/to/audit/directory/%s' % name, 'w')
        run('hostname -i')
        print 'Checking the uptime of: ', env.host
        if run('uptime') < '0':
                print(red("it worked for less"))
        elif run('uptime') > '0':
                print(green("it worked for greater"))
        else:
                print "WTF?!"

When I run fabric on my fabfile.py to perform "audit" it works just fine but it's not creating the log file with the appended host name at the beginning of the file. It does create a log file for each host defined in test_servers with the timestamp though. Any help would be greatly appreciated.

Kryten
  • 595
  • 3
  • 10
  • 25
  • 2
    add `return` in `logname()` e.g., `return "%s_%s" % (env.host_string, timestamp)` – jfs Apr 25 '13 at 16:22

1 Answers1

0

Looks like env.hosts cannot be defined in function, only env.host_string does.

So I guess maybe you got some error messages like:

No hosts found. Please specify (single) host string for connection:
mcsrainbow
  • 370
  • 2
  • 12
  • Not getting that error. As stated previously, I don't receive any errors. You could be on to something as far as env.hosts not able to be defined in a function. I'll start hacking away at it and find out. – Kryten Apr 25 '13 at 15:14
  • I fixed it, see http://stackoverflow.com/questions/16209242/passing-a-fabric-env-hosts-sting-as-a-variable-is-not-work-in-function/16211112#16211112 – mcsrainbow Apr 27 '13 at 08:25