【问题标题】:python fabric, Iterate through IP list to update serverspython fabric,遍历IP列表更新服务器
【发布时间】:2017-01-18 16:29:52
【问题描述】:

希望您能提供帮助。对面料完全陌生,了解一点蟒蛇。我正在尝试遍历 IP 的外部文件以更新 40 多个远程服务器。

这不起作用,在第一个 IP 之后停止。

终端命令: fab -p 密码主机更新

from fabric.api import env, run, sudo


def hosts():
    env.hosts = open('sat_ip_list', 'r').readlines()


def update():
     sudo('apt-get update -y')

【问题讨论】:

    标签: python linux terminal fabric


    【解决方案1】:

    我尝试了以下 IP + Fabric 脚本列表,运行 fab -p <password> hosts uname 没有问题:

    # ip_list.txt
    192.168.xxx.x
    127.0.0.1:xxxx
    174.xxx.xxx.xxx:xxxx
    
    # fabfile.py
    from fabric.api import env, run, sudo
    
    def hosts():
      # Read ip list from ip_list.txt
      env.hosts = open('ip_list.txt', 'r').readlines()
    
    def uname():
      sudo('uname -a')
    

    您的sat_ip_list 文件是什么样的 - 每行一个 IP 地址吗?

    您是否尝试过仅使用极少数主机(例如 2-3 个 IP 地址)的脚本?绝对没有理由你不应该做你想要完成的事情,你的脚本基本上对我有用。

    作为健全性检查,您可能希望打印出 env.hosts 的值,如下所示:

    def hosts():
      env.hosts = open('sat_ip_list', 'r').readlines()
      print('Hosts:', env.hosts) 
    

    在我的例子中,结果如下:

    me@machine:~$ fab hosts
    ('Hosts:', ['192.168.xxx.x\n', '127.0.0.1:xxxx\n', '174.xxx.xxx.xxx:xxxx\n'])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-08
      • 2013-02-18
      • 1970-01-01
      • 2018-03-08
      • 2015-10-07
      • 2013-07-13
      相关资源
      最近更新 更多