【问题标题】:python: using salt.client with multiprocessingpython:将 salt.client 与多处理一起使用
【发布时间】:2023-02-16 19:43:59
【问题描述】:

我正在尝试使用“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 一起工作。我怎样才能让它工作?

【问题讨论】:

    标签: python multiprocessing salt-stack


    【解决方案1】:

    我能够从与我一起工作的高级 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))
    

    【讨论】:

      猜你喜欢
      • 2021-05-22
      • 2012-03-04
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 2020-07-03
      • 2021-06-25
      • 1970-01-01
      • 2018-03-10
      相关资源
      最近更新 更多