[英]python: using salt.client with multiprocessing
我正在尝试使用“salt.client”编写一个脚本,该脚本将在一组主机上运行指定的操作。 https://docs.saltproject.io/en/latest/ref/clients/index.html上的文档提供了以下示例:
使用该示例,我编写的脚本是:
#!/usr/bin/env python3
import os
import salt.client
from multiprocessing import Pool
bootstrap_cmds = []
bootstrap_hosts = ['census-01630c42ebc397280*', 'consul-0090ff8a220eb6ff1*', 'consul-066f3dc733ebf89d8*', 'consul-037c95c90d235f723*']
for host in bootstrap_hosts:
hostCmd = salt.client.LocalClient().cmd(host, 'cmd.run', ['facter hostname ipaddress'])
bootstrap_cmds.append(hostCmd)
processes = (bootstrap_cmds)
def start_process(process_start):
os.system('python {}'.format(process_start))
start_pool = Pool(processes=len(bootstrap_cmds))
start_pool.map(start_process, processes)
当我运行脚本时,我得到以下响应:
python: can't open file '{consul-0090ff8a220eb6ff1.node.usge1prod.consul:': [Errno 2] No such file or directory
python: can't open file '{consul-037c95c90d235f723.node.usge1prod.consul:': [Errno 2] No such file or directory
python: can't open file '{consul-066f3dc733ebf89d8.node.usge1prod.consul:': [Errno 2] No such file or directory
python: can't open file '{census-01630c42ebc397280.node.usge1prod.consul:': [Errno 2] No such file or directory
附加到每个主机名的 '*' 是通配符,我们在 'consul' 中对我们的主机名做一些时髦的后缀——它在命令行中与 salt 一起使用。 我怎样才能让它工作?
我能够从与我一起工作的高级 DevOps 工程师那里得到一个可行的解决方案:
#!/usr/bin/env python3
import salt.client
from multiprocessing import Pool
hosts = [
"census-01630c42ebc397280*",
"consul-0090ff8a220eb6ff1*",
"consul-066f3dc733ebf89d8*",
"consul-037c95c90d235f723*",
]
def start_process(host):
salt.client.LocalClient().cmd(host, "cmd.run", ["facter hostname ipaddress"])
start_pool = Pool(processes=len(hosts))
print(start_pool.map(start_process, hosts))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.