gpt4 book ai didi

python Fabric - 将主机作为参数传递

转载 作者:行者123 更新时间:2023-11-30 23:34:54 25 4
gpt4 key购买 nike

下面是我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com