下面是我的 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/')]
最佳答案
./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 设置为给定的以逗号分隔的主机字符串列表。 请查看here详细的 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)
关于python Fabric - 将主机作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17756678/