简体   繁体   English

python:将 salt.client 与多处理一起使用

[英]python: using salt.client with multiprocessing

I'm trying to write a script using 'salt.client' that will run a specified action on a set of hosts.我正在尝试使用“salt.client”编写一个脚本,该脚本将在一组主机上运行指定的操作。 The documentation at https://docs.saltproject.io/en/latest/ref/clients/index.html gives the following example:https://docs.saltproject.io/en/latest/ref/clients/index.html上的文档提供了以下示例:

salt.client 示例

Using that example, the script I've written is:使用该示例,我编写的脚本是:

#!/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)

When I run the script, I get the following response:当我运行脚本时,我得到以下响应:

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

The '*' appended to each hostname is to wildcard some funky suffixation that we do with our hostnames in 'consul' -- it works with salt in the command line.附加到每个主机名的 '*' 是通配符,我们在 'consul' 中对我们的主机名做一些时髦的后缀——它在命令行中与 salt 一起使用。 How can I get this to work?我怎样才能让它工作?

I was able to get a workable solution to my problem from a senior DevOps engineer I work with:我能够从与我一起工作的高级 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.

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