简体   繁体   中英

Python - fabric log file creation using definitions and env.hosts

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.

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM