简体   繁体   中英

How to push command by python fabric to multiple hosts?

I don't uderstand how to define multiple hosts in python fabric. I have a hosts.txt file contains a connection data in this form:

user@hostname:22 password
user@otherhost:22 password
user@thirdhost:22 password

and sample code:

#!/usr/bin/python

import pprint
from fabric.api import *

for line in open('hosts.txt','r').readlines():
    host, passw = line.split()
    env.hosts.append(host)
    env.passwords[host] = passw

pprint.pprint( env.hosts )

def test():
 sudo("mkdir /a")

test();

Unfortunately I'm getting message

No hosts found. Please specify (single) host string for connection

pprint shows me a proper hosts list. What should I do to perform my tasks to all hosts inside hosts.txt file?

I doing it wrong way. Instead of calling a function from a python file, i should run it from command line this way:

fab -f my_file_with_functions function_name

so this solves the problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM