传递结构执行任务的命令行参数

时间:2016-06-16 04:22:03

标签: python amazon-ec2 fabric

我是fabric API的新手,我试图找出传递命令行参数,这些参数对于每个主机都是不同的。那么,这就是我现在所处的位置。目前,下面的位在三台主机上的每台主机上并行正确运行脚本get_num_reviews_aws.py

hosts = [ubuntu@54.xxx.xx.xx,
         ubuntu@52.xx.xxx.xx,
         ubuntu@54.xx.xxx.xx]

#%%
from fabric.api import run, parallel
from fabric.tasks import execute


%%
@parallel
def webscraper():
    run("python get_num_reviews_aws.py")


#%% run on hosts
execute(webscraper, hosts=hosts)

我正在寻找的是能够将命令行参数传递给python脚本,这些参数对于每个主机都是不同的,但仍然可以并行运行。所以像这样:

@parallel
def webscraper(start, end):
    run("python get_num_reviews_aws.py %s %s" % (start, end))

然后基本上为每个主机设置了一组startend。让我感到高兴的是,我传递了一份主机列表,但我认为我没有为每个命令行参数传递一个列表,例如:

start = [1, 2, 3]
end = [4, 5, 6]

execute(webscraper, start, end, hosts=hosts)

1 个答案:

答案 0 :(得分:0)

在查看了其他一些similar questions on SO之后,这对我有用。

hosts = [ubuntu@54.xxx.xx.xx,
         ubuntu@52.xx.xxx.xx,
         ubuntu@54.xx.xxx.xx]


#%% get username and host
hosts = ["ubuntu@" + ip.ip_address for ip in instance_lst]


#%% Create my command line arguments and store them in a dict
# use the hostname as the key and store arguments in a tuple
host_dict = {host: (0, 10 + x) for x, host in enumerate(hosts)}


#%%
from fabric.api import run, parallel, env
from fabric.tasks import execute


#%% set environment
env.hosts = hosts


#%%
@parallel
def webscraper(host_dict):
    host = "ubuntu@" + env.host # cuts off the username, so have to re-add
    start_end = host_dict[host] # get the start and end for each host
    run("python get_num_reviews_aws.py %s %s" % start_end)


#%% run on hosts
# since I stored the hosts in an environment, don't need to pass hosts
execute(webscraper, host_dict)