简体   繁体   English

从执行的任务中检索当前主机

[英]Retrieve current host from within an executed task

I'm using fabric to execute some remote commands on several hosts by setting: 我正在使用Fabric通过设置在几台主机上执行一些远程命令:

env.hosts = [host1, host2, ...]

There are several tasks I want to perform on some of the hosts, and some I don't. 我想在某些主机上执行一些任务,而有些则不需要。 is there's a way I can retrieve the current hostname the task is executing on? 有什么方法可以检索执行任务的当前主机名?

Any help would be great. 任何帮助都会很棒。 Thanks, Meny 谢谢,梅尼

Why not use different host roles? 为什么不使用其他主机角色?

from fabric.api import env, roles, run

env.roledefs['webservers'] = ['www1', 'www2', 'www3']

@roles('webservers')
def my_task():
    run('ls -l')

Additionally, you can get the current executing host from the env dictionary: 此外,您可以从env词典中获取当前正在执行的主机:

def my_task():
    print 'Currently executing on {0}'.format(env.host)

eclaird answer definitely helped me use a better practice when executing tasks on several hosts with different roles. eclaird的回答绝对可以帮助我在多个具有不同角色的主机上执行任务时更好地实践。

Though, while playing with it, it seems that within the task, env.host will give the name of the current hostname. 虽然,在使用它时,似乎在任务中,env.host会给出当前主机名的名称。
for example: 例如:

@parallel(pool_size=len(env.hosts))
def upload_to_s3(to):
    awscreds = 'some credentials...'
    cmd = '%s aws s3 sync /mnt/backup %s/%s/' % (awscreds, to, env.host)
    run(cmd)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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