1

下面是我的工厂文件。

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/')]
4

1 回答 1

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] 选项将 env.hosts 设置为给定的以逗号分隔的主机字符串列表。请在此处查看详细的 fab 命令用法

或像这样。

./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)
于 2013-07-20T00:47:24.287 回答