python fabric - 将host作为参数传递

时间:2013-07-19 22:49:48

标签: python fabric

以下是我的fab文件。

env.hosts = ['111.111.111.111']
env.user = "root"
env.key_filename = "/home/ubuntu/.ssh/id_rsa"
def chef():
    run('mkdir -p /home/ubuntu/')]

如何将主机作为参数传递,以便我可以这样调用?

fab test host=111.111.111.111

env.hosts = [host]
env.user = "root"
env.key_filename = "/home/ubuntu/.ssh/id_rsa"
def chef():
    run('mkdir -p /home/ubuntu/')]

1 个答案:

答案 0 :(得分:3)

./fabfile.py
from fabric.api import run, env

env.user = "root"
env.key_filename = "/home/ubuntu/.ssh/id_rsa"
def test():
    run("mkdir -p /home/ubuntu/")

---
fabric:~$ fab test -H 111.111.111.111
>>>[192.168.99.20] Executing task 'test'
>>>[192.168.99.20] run: mkdir -p /home/ubuntu/
>>>
>>>Done.

[ - H] opstion将env.hosts设置为给定的逗号分隔的主机字符串列表。 请查看here以获取详细的fab命令usag

或者像这样。

./fabfile.py
def setenv(host=None):
    if host is not None:
        env.hosts= [host]
    env.user = "ubuntu"
    env.key_filename = "/home/ubuntu/.ssh/id_rsa"

def test():
    run("mkdir -p /home/ubuntu/")

---
fabric:~$ fab setenv:web01 test
(snip)