在同一主机上并行运行ansible剧本

2024-09-19 23:42:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着跑30分钟操作系统命令它在同一主机上以线程并行的方式在python脚本中运行ansible playbook。如果我以串行方式运行30个ansible剧本,它将完成所有30个任务。但当我试图以并行方式运行它们时,有些剧本执行任务,有些则不执行任务。Python正在处理playbooks的线程化运行。我认为当ansible试图在同一个主机上同时运行多个ansible playbook时,就会发生冲突。如何在同一主机上同时运行30个线程playbok。在

def runOsCommand( systemCommand ):
    print(systemCommand)
    logging.info(" "+systemCommand)
    os.system(systemCommand)

def createProfile( Profile,ProfileDesc ):
    Command= "ansible-playbook create-profile.yml  --extra-vars \"VARIABLE1=" + str(Profile) + " " +  "VARIABLE2=" + str(ProfileDesc) +"\""
    runOsCommand(Command)
    return

class Thread (threading.Thread):
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
    def run(self):
        print "Starting " + self.name
        createProfile(Profile,ProfileDesc)     
        print "Exiting " + self.name

for iter in range(30):
    mythreadx = Thread(iter, "Thread-Datapool-Row-"+str(iter), iter)
    mythreadx.start()

Tags: nameselfdef方式ansibleprofile线程thread