Hello,
I want to run ansible/ansible-playbook concurrent. But I don’t know if there may be an error?
I have a python script. I tried it and there weren’t an errors, but maybe I did some wrong?
`
import json
import threading
import subprocess
import time
lock = threading.Lock()
storage =
def run_cmd(cmd):
“”“run command cmd and return (stdout, stderr)”“”
handler = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
stdout, stderr = handler.communicate()
return stdout, stderr
def run_and_get_json(cmd):
sys_data = None
stdout = None
try:
pylint: disable=unused-variable
stdout, stderr = run_cmd(cmd)
pylint: disable=broad-except,invalid-name
except Exception as error:
print “Can’t run %s because %s” % (cmd, error)
try:
sys_data = json.loads(stdout)
pylint: disable=broad-except
except Exception as error:
print “Can’t parse output as json of %s because %s” % (cmd, error)
return sys_data
def run_command(cmd):
sys_data = run_and_get_json(cmd)
with lock:
global storage
storage.append(sys_data)
playbooks = [
“/opt/manager/playbooks/rde/main.yml”,
“/opt/manager/playbooks/test/main.yml”,
“/home.local/manager/playbooks/Desktop_Environment/main.yml”,
“/home.local/manager/playbooks/Desktop_Environment/main.yml”]
start_time = time.time()
threads =
for pb in playbooks:
t= threading.Thread(target=run_command, args=(“ansible-playbook -i 127.0.0.1, %s” % pb, )) #.start()
threads.append(t)
t.start()
event.set()
for worker in threads:
worker.join()
print(“— %s seconds —” % (time.time() - start_time))
storage =
start_time = time.time()
for pb in playbooks:
run_command(“/home.local/valerys/rde_2_0/rdemanager/bin/get-tags -p %s” % pb )
print(“— %s seconds —” % (time.time() - start_time))
`
Regards.